device.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  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. kobject_put(&device->dev.kobj);
  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(!parent);
  383. WARN_ON_ONCE(device->dma_device);
  384. if (device->dev.dma_ops) {
  385. /*
  386. * The caller provided custom DMA operations. Copy the
  387. * DMA-related fields that are used by e.g. dma_alloc_coherent()
  388. * into device->dev.
  389. */
  390. device->dma_device = &device->dev;
  391. if (!device->dev.dma_mask)
  392. device->dev.dma_mask = parent->dma_mask;
  393. if (!device->dev.coherent_dma_mask)
  394. device->dev.coherent_dma_mask =
  395. parent->coherent_dma_mask;
  396. } else {
  397. /*
  398. * The caller did not provide custom DMA operations. Use the
  399. * DMA mapping operations of the parent device.
  400. */
  401. device->dma_device = parent;
  402. }
  403. mutex_lock(&device_mutex);
  404. if (strchr(device->name, '%')) {
  405. ret = alloc_name(device->name);
  406. if (ret)
  407. goto out;
  408. }
  409. if (ib_device_check_mandatory(device)) {
  410. ret = -EINVAL;
  411. goto out;
  412. }
  413. ret = read_port_immutable(device);
  414. if (ret) {
  415. pr_warn("Couldn't create per port immutable data %s\n",
  416. device->name);
  417. goto out;
  418. }
  419. ret = setup_port_pkey_list(device);
  420. if (ret) {
  421. pr_warn("Couldn't create per port_pkey_list\n");
  422. goto out;
  423. }
  424. ret = ib_cache_setup_one(device);
  425. if (ret) {
  426. pr_warn("Couldn't set up InfiniBand P_Key/GID cache\n");
  427. goto port_cleanup;
  428. }
  429. ret = ib_device_register_rdmacg(device);
  430. if (ret) {
  431. pr_warn("Couldn't register device with rdma cgroup\n");
  432. goto cache_cleanup;
  433. }
  434. memset(&device->attrs, 0, sizeof(device->attrs));
  435. ret = device->query_device(device, &device->attrs, &uhw);
  436. if (ret) {
  437. pr_warn("Couldn't query the device attributes\n");
  438. goto cache_cleanup;
  439. }
  440. ret = ib_device_register_sysfs(device, port_callback);
  441. if (ret) {
  442. pr_warn("Couldn't register device %s with driver model\n",
  443. device->name);
  444. goto cache_cleanup;
  445. }
  446. device->reg_state = IB_DEV_REGISTERED;
  447. list_for_each_entry(client, &client_list, list)
  448. if (!add_client_context(device, client) && client->add)
  449. client->add(device);
  450. device->index = __dev_new_index();
  451. down_write(&lists_rwsem);
  452. list_add_tail(&device->core_list, &device_list);
  453. up_write(&lists_rwsem);
  454. mutex_unlock(&device_mutex);
  455. return 0;
  456. cache_cleanup:
  457. ib_cache_cleanup_one(device);
  458. ib_cache_release_one(device);
  459. port_cleanup:
  460. kfree(device->port_immutable);
  461. out:
  462. mutex_unlock(&device_mutex);
  463. return ret;
  464. }
  465. EXPORT_SYMBOL(ib_register_device);
  466. /**
  467. * ib_unregister_device - Unregister an IB device
  468. * @device:Device to unregister
  469. *
  470. * Unregister an IB device. All clients will receive a remove callback.
  471. */
  472. void ib_unregister_device(struct ib_device *device)
  473. {
  474. struct ib_client_data *context, *tmp;
  475. unsigned long flags;
  476. mutex_lock(&device_mutex);
  477. down_write(&lists_rwsem);
  478. list_del(&device->core_list);
  479. spin_lock_irqsave(&device->client_data_lock, flags);
  480. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  481. context->going_down = true;
  482. spin_unlock_irqrestore(&device->client_data_lock, flags);
  483. downgrade_write(&lists_rwsem);
  484. list_for_each_entry_safe(context, tmp, &device->client_data_list,
  485. list) {
  486. if (context->client->remove)
  487. context->client->remove(device, context->data);
  488. }
  489. up_read(&lists_rwsem);
  490. ib_device_unregister_rdmacg(device);
  491. ib_device_unregister_sysfs(device);
  492. mutex_unlock(&device_mutex);
  493. ib_cache_cleanup_one(device);
  494. ib_security_destroy_port_pkey_list(device);
  495. kfree(device->port_pkey_list);
  496. down_write(&lists_rwsem);
  497. spin_lock_irqsave(&device->client_data_lock, flags);
  498. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  499. kfree(context);
  500. spin_unlock_irqrestore(&device->client_data_lock, flags);
  501. up_write(&lists_rwsem);
  502. device->reg_state = IB_DEV_UNREGISTERED;
  503. }
  504. EXPORT_SYMBOL(ib_unregister_device);
  505. /**
  506. * ib_register_client - Register an IB client
  507. * @client:Client to register
  508. *
  509. * Upper level users of the IB drivers can use ib_register_client() to
  510. * register callbacks for IB device addition and removal. When an IB
  511. * device is added, each registered client's add method will be called
  512. * (in the order the clients were registered), and when a device is
  513. * removed, each client's remove method will be called (in the reverse
  514. * order that clients were registered). In addition, when
  515. * ib_register_client() is called, the client will receive an add
  516. * callback for all devices already registered.
  517. */
  518. int ib_register_client(struct ib_client *client)
  519. {
  520. struct ib_device *device;
  521. mutex_lock(&device_mutex);
  522. list_for_each_entry(device, &device_list, core_list)
  523. if (!add_client_context(device, client) && client->add)
  524. client->add(device);
  525. down_write(&lists_rwsem);
  526. list_add_tail(&client->list, &client_list);
  527. up_write(&lists_rwsem);
  528. mutex_unlock(&device_mutex);
  529. return 0;
  530. }
  531. EXPORT_SYMBOL(ib_register_client);
  532. /**
  533. * ib_unregister_client - Unregister an IB client
  534. * @client:Client to unregister
  535. *
  536. * Upper level users use ib_unregister_client() to remove their client
  537. * registration. When ib_unregister_client() is called, the client
  538. * will receive a remove callback for each IB device still registered.
  539. */
  540. void ib_unregister_client(struct ib_client *client)
  541. {
  542. struct ib_client_data *context, *tmp;
  543. struct ib_device *device;
  544. unsigned long flags;
  545. mutex_lock(&device_mutex);
  546. down_write(&lists_rwsem);
  547. list_del(&client->list);
  548. up_write(&lists_rwsem);
  549. list_for_each_entry(device, &device_list, core_list) {
  550. struct ib_client_data *found_context = NULL;
  551. down_write(&lists_rwsem);
  552. spin_lock_irqsave(&device->client_data_lock, flags);
  553. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  554. if (context->client == client) {
  555. context->going_down = true;
  556. found_context = context;
  557. break;
  558. }
  559. spin_unlock_irqrestore(&device->client_data_lock, flags);
  560. up_write(&lists_rwsem);
  561. if (client->remove)
  562. client->remove(device, found_context ?
  563. found_context->data : NULL);
  564. if (!found_context) {
  565. pr_warn("No client context found for %s/%s\n",
  566. device->name, client->name);
  567. continue;
  568. }
  569. down_write(&lists_rwsem);
  570. spin_lock_irqsave(&device->client_data_lock, flags);
  571. list_del(&found_context->list);
  572. kfree(found_context);
  573. spin_unlock_irqrestore(&device->client_data_lock, flags);
  574. up_write(&lists_rwsem);
  575. }
  576. mutex_unlock(&device_mutex);
  577. }
  578. EXPORT_SYMBOL(ib_unregister_client);
  579. /**
  580. * ib_get_client_data - Get IB client context
  581. * @device:Device to get context for
  582. * @client:Client to get context for
  583. *
  584. * ib_get_client_data() returns client context set with
  585. * ib_set_client_data().
  586. */
  587. void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
  588. {
  589. struct ib_client_data *context;
  590. void *ret = NULL;
  591. unsigned long flags;
  592. spin_lock_irqsave(&device->client_data_lock, flags);
  593. list_for_each_entry(context, &device->client_data_list, list)
  594. if (context->client == client) {
  595. ret = context->data;
  596. break;
  597. }
  598. spin_unlock_irqrestore(&device->client_data_lock, flags);
  599. return ret;
  600. }
  601. EXPORT_SYMBOL(ib_get_client_data);
  602. /**
  603. * ib_set_client_data - Set IB client context
  604. * @device:Device to set context for
  605. * @client:Client to set context for
  606. * @data:Context to set
  607. *
  608. * ib_set_client_data() sets client context that can be retrieved with
  609. * ib_get_client_data().
  610. */
  611. void ib_set_client_data(struct ib_device *device, struct ib_client *client,
  612. void *data)
  613. {
  614. struct ib_client_data *context;
  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. context->data = data;
  620. goto out;
  621. }
  622. pr_warn("No client context found for %s/%s\n",
  623. device->name, client->name);
  624. out:
  625. spin_unlock_irqrestore(&device->client_data_lock, flags);
  626. }
  627. EXPORT_SYMBOL(ib_set_client_data);
  628. /**
  629. * ib_register_event_handler - Register an IB event handler
  630. * @event_handler:Handler to register
  631. *
  632. * ib_register_event_handler() registers an event handler that will be
  633. * called back when asynchronous IB events occur (as defined in
  634. * chapter 11 of the InfiniBand Architecture Specification). This
  635. * callback may occur in interrupt context.
  636. */
  637. void ib_register_event_handler(struct ib_event_handler *event_handler)
  638. {
  639. unsigned long flags;
  640. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  641. list_add_tail(&event_handler->list,
  642. &event_handler->device->event_handler_list);
  643. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  644. }
  645. EXPORT_SYMBOL(ib_register_event_handler);
  646. /**
  647. * ib_unregister_event_handler - Unregister an event handler
  648. * @event_handler:Handler to unregister
  649. *
  650. * Unregister an event handler registered with
  651. * ib_register_event_handler().
  652. */
  653. void ib_unregister_event_handler(struct ib_event_handler *event_handler)
  654. {
  655. unsigned long flags;
  656. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  657. list_del(&event_handler->list);
  658. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  659. }
  660. EXPORT_SYMBOL(ib_unregister_event_handler);
  661. /**
  662. * ib_dispatch_event - Dispatch an asynchronous event
  663. * @event:Event to dispatch
  664. *
  665. * Low-level drivers must call ib_dispatch_event() to dispatch the
  666. * event to all registered event handlers when an asynchronous event
  667. * occurs.
  668. */
  669. void ib_dispatch_event(struct ib_event *event)
  670. {
  671. unsigned long flags;
  672. struct ib_event_handler *handler;
  673. spin_lock_irqsave(&event->device->event_handler_lock, flags);
  674. list_for_each_entry(handler, &event->device->event_handler_list, list)
  675. handler->handler(handler, event);
  676. spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
  677. }
  678. EXPORT_SYMBOL(ib_dispatch_event);
  679. /**
  680. * ib_query_port - Query IB port attributes
  681. * @device:Device to query
  682. * @port_num:Port number to query
  683. * @port_attr:Port attributes
  684. *
  685. * ib_query_port() returns the attributes of a port through the
  686. * @port_attr pointer.
  687. */
  688. int ib_query_port(struct ib_device *device,
  689. u8 port_num,
  690. struct ib_port_attr *port_attr)
  691. {
  692. union ib_gid gid;
  693. int err;
  694. if (!rdma_is_port_valid(device, port_num))
  695. return -EINVAL;
  696. memset(port_attr, 0, sizeof(*port_attr));
  697. err = device->query_port(device, port_num, port_attr);
  698. if (err || port_attr->subnet_prefix)
  699. return err;
  700. if (rdma_port_get_link_layer(device, port_num) != IB_LINK_LAYER_INFINIBAND)
  701. return 0;
  702. err = ib_query_gid(device, port_num, 0, &gid, NULL);
  703. if (err)
  704. return err;
  705. port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix);
  706. return 0;
  707. }
  708. EXPORT_SYMBOL(ib_query_port);
  709. /**
  710. * ib_query_gid - Get GID table entry
  711. * @device:Device to query
  712. * @port_num:Port number to query
  713. * @index:GID table index to query
  714. * @gid:Returned GID
  715. * @attr: Returned GID attributes related to this GID index (only in RoCE).
  716. * NULL means ignore.
  717. *
  718. * ib_query_gid() fetches the specified GID table entry.
  719. */
  720. int ib_query_gid(struct ib_device *device,
  721. u8 port_num, int index, union ib_gid *gid,
  722. struct ib_gid_attr *attr)
  723. {
  724. if (rdma_cap_roce_gid_table(device, port_num))
  725. return ib_get_cached_gid(device, port_num, index, gid, attr);
  726. if (attr)
  727. return -EINVAL;
  728. return device->query_gid(device, port_num, index, gid);
  729. }
  730. EXPORT_SYMBOL(ib_query_gid);
  731. /**
  732. * ib_enum_roce_netdev - enumerate all RoCE ports
  733. * @ib_dev : IB device we want to query
  734. * @filter: Should we call the callback?
  735. * @filter_cookie: Cookie passed to filter
  736. * @cb: Callback to call for each found RoCE ports
  737. * @cookie: Cookie passed back to the callback
  738. *
  739. * Enumerates all of the physical RoCE ports of ib_dev
  740. * which are related to netdevice and calls callback() on each
  741. * device for which filter() function returns non zero.
  742. */
  743. void ib_enum_roce_netdev(struct ib_device *ib_dev,
  744. roce_netdev_filter filter,
  745. void *filter_cookie,
  746. roce_netdev_callback cb,
  747. void *cookie)
  748. {
  749. u8 port;
  750. for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
  751. port++)
  752. if (rdma_protocol_roce(ib_dev, port)) {
  753. struct net_device *idev = NULL;
  754. if (ib_dev->get_netdev)
  755. idev = ib_dev->get_netdev(ib_dev, port);
  756. if (idev &&
  757. idev->reg_state >= NETREG_UNREGISTERED) {
  758. dev_put(idev);
  759. idev = NULL;
  760. }
  761. if (filter(ib_dev, port, idev, filter_cookie))
  762. cb(ib_dev, port, idev, cookie);
  763. if (idev)
  764. dev_put(idev);
  765. }
  766. }
  767. /**
  768. * ib_enum_all_roce_netdevs - enumerate all RoCE devices
  769. * @filter: Should we call the callback?
  770. * @filter_cookie: Cookie passed to filter
  771. * @cb: Callback to call for each found RoCE ports
  772. * @cookie: Cookie passed back to the callback
  773. *
  774. * Enumerates all RoCE devices' physical ports which are related
  775. * to netdevices and calls callback() on each device for which
  776. * filter() function returns non zero.
  777. */
  778. void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
  779. void *filter_cookie,
  780. roce_netdev_callback cb,
  781. void *cookie)
  782. {
  783. struct ib_device *dev;
  784. down_read(&lists_rwsem);
  785. list_for_each_entry(dev, &device_list, core_list)
  786. ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
  787. up_read(&lists_rwsem);
  788. }
  789. /**
  790. * ib_enum_all_devs - enumerate all ib_devices
  791. * @cb: Callback to call for each found ib_device
  792. *
  793. * Enumerates all ib_devices and calls callback() on each device.
  794. */
  795. int ib_enum_all_devs(nldev_callback nldev_cb, struct sk_buff *skb,
  796. struct netlink_callback *cb)
  797. {
  798. struct ib_device *dev;
  799. unsigned int idx = 0;
  800. int ret = 0;
  801. down_read(&lists_rwsem);
  802. list_for_each_entry(dev, &device_list, core_list) {
  803. ret = nldev_cb(dev, skb, cb, idx);
  804. if (ret)
  805. break;
  806. idx++;
  807. }
  808. up_read(&lists_rwsem);
  809. return ret;
  810. }
  811. /**
  812. * ib_query_pkey - Get P_Key table entry
  813. * @device:Device to query
  814. * @port_num:Port number to query
  815. * @index:P_Key table index to query
  816. * @pkey:Returned P_Key
  817. *
  818. * ib_query_pkey() fetches the specified P_Key table entry.
  819. */
  820. int ib_query_pkey(struct ib_device *device,
  821. u8 port_num, u16 index, u16 *pkey)
  822. {
  823. return device->query_pkey(device, port_num, index, pkey);
  824. }
  825. EXPORT_SYMBOL(ib_query_pkey);
  826. /**
  827. * ib_modify_device - Change IB device attributes
  828. * @device:Device to modify
  829. * @device_modify_mask:Mask of attributes to change
  830. * @device_modify:New attribute values
  831. *
  832. * ib_modify_device() changes a device's attributes as specified by
  833. * the @device_modify_mask and @device_modify structure.
  834. */
  835. int ib_modify_device(struct ib_device *device,
  836. int device_modify_mask,
  837. struct ib_device_modify *device_modify)
  838. {
  839. if (!device->modify_device)
  840. return -ENOSYS;
  841. return device->modify_device(device, device_modify_mask,
  842. device_modify);
  843. }
  844. EXPORT_SYMBOL(ib_modify_device);
  845. /**
  846. * ib_modify_port - Modifies the attributes for the specified port.
  847. * @device: The device to modify.
  848. * @port_num: The number of the port to modify.
  849. * @port_modify_mask: Mask used to specify which attributes of the port
  850. * to change.
  851. * @port_modify: New attribute values for the port.
  852. *
  853. * ib_modify_port() changes a port's attributes as specified by the
  854. * @port_modify_mask and @port_modify structure.
  855. */
  856. int ib_modify_port(struct ib_device *device,
  857. u8 port_num, int port_modify_mask,
  858. struct ib_port_modify *port_modify)
  859. {
  860. int rc;
  861. if (!rdma_is_port_valid(device, port_num))
  862. return -EINVAL;
  863. if (device->modify_port)
  864. rc = device->modify_port(device, port_num, port_modify_mask,
  865. port_modify);
  866. else
  867. rc = rdma_protocol_roce(device, port_num) ? 0 : -ENOSYS;
  868. return rc;
  869. }
  870. EXPORT_SYMBOL(ib_modify_port);
  871. /**
  872. * ib_find_gid - Returns the port number and GID table index where
  873. * a specified GID value occurs.
  874. * @device: The device to query.
  875. * @gid: The GID value to search for.
  876. * @gid_type: Type of GID.
  877. * @ndev: The ndev related to the GID to search for.
  878. * @port_num: The port number of the device where the GID value was found.
  879. * @index: The index into the GID table where the GID was found. This
  880. * parameter may be NULL.
  881. */
  882. int ib_find_gid(struct ib_device *device, union ib_gid *gid,
  883. enum ib_gid_type gid_type, struct net_device *ndev,
  884. u8 *port_num, u16 *index)
  885. {
  886. union ib_gid tmp_gid;
  887. int ret, port, i;
  888. for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
  889. if (rdma_cap_roce_gid_table(device, port)) {
  890. if (!ib_find_cached_gid_by_port(device, gid, gid_type, port,
  891. ndev, index)) {
  892. *port_num = port;
  893. return 0;
  894. }
  895. }
  896. if (gid_type != IB_GID_TYPE_IB)
  897. continue;
  898. for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
  899. ret = ib_query_gid(device, port, i, &tmp_gid, NULL);
  900. if (ret)
  901. return ret;
  902. if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
  903. *port_num = port;
  904. if (index)
  905. *index = i;
  906. return 0;
  907. }
  908. }
  909. }
  910. return -ENOENT;
  911. }
  912. EXPORT_SYMBOL(ib_find_gid);
  913. /**
  914. * ib_find_pkey - Returns the PKey table index where a specified
  915. * PKey value occurs.
  916. * @device: The device to query.
  917. * @port_num: The port number of the device to search for the PKey.
  918. * @pkey: The PKey value to search for.
  919. * @index: The index into the PKey table where the PKey was found.
  920. */
  921. int ib_find_pkey(struct ib_device *device,
  922. u8 port_num, u16 pkey, u16 *index)
  923. {
  924. int ret, i;
  925. u16 tmp_pkey;
  926. int partial_ix = -1;
  927. for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
  928. ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
  929. if (ret)
  930. return ret;
  931. if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
  932. /* if there is full-member pkey take it.*/
  933. if (tmp_pkey & 0x8000) {
  934. *index = i;
  935. return 0;
  936. }
  937. if (partial_ix < 0)
  938. partial_ix = i;
  939. }
  940. }
  941. /*no full-member, if exists take the limited*/
  942. if (partial_ix >= 0) {
  943. *index = partial_ix;
  944. return 0;
  945. }
  946. return -ENOENT;
  947. }
  948. EXPORT_SYMBOL(ib_find_pkey);
  949. /**
  950. * ib_get_net_dev_by_params() - Return the appropriate net_dev
  951. * for a received CM request
  952. * @dev: An RDMA device on which the request has been received.
  953. * @port: Port number on the RDMA device.
  954. * @pkey: The Pkey the request came on.
  955. * @gid: A GID that the net_dev uses to communicate.
  956. * @addr: Contains the IP address that the request specified as its
  957. * destination.
  958. */
  959. struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
  960. u8 port,
  961. u16 pkey,
  962. const union ib_gid *gid,
  963. const struct sockaddr *addr)
  964. {
  965. struct net_device *net_dev = NULL;
  966. struct ib_client_data *context;
  967. if (!rdma_protocol_ib(dev, port))
  968. return NULL;
  969. down_read(&lists_rwsem);
  970. list_for_each_entry(context, &dev->client_data_list, list) {
  971. struct ib_client *client = context->client;
  972. if (context->going_down)
  973. continue;
  974. if (client->get_net_dev_by_params) {
  975. net_dev = client->get_net_dev_by_params(dev, port, pkey,
  976. gid, addr,
  977. context->data);
  978. if (net_dev)
  979. break;
  980. }
  981. }
  982. up_read(&lists_rwsem);
  983. return net_dev;
  984. }
  985. EXPORT_SYMBOL(ib_get_net_dev_by_params);
  986. static const struct rdma_nl_cbs ibnl_ls_cb_table[RDMA_NL_LS_NUM_OPS] = {
  987. [RDMA_NL_LS_OP_RESOLVE] = {
  988. .doit = ib_nl_handle_resolve_resp,
  989. .flags = RDMA_NL_ADMIN_PERM,
  990. },
  991. [RDMA_NL_LS_OP_SET_TIMEOUT] = {
  992. .doit = ib_nl_handle_set_timeout,
  993. .flags = RDMA_NL_ADMIN_PERM,
  994. },
  995. [RDMA_NL_LS_OP_IP_RESOLVE] = {
  996. .doit = ib_nl_handle_ip_res_resp,
  997. .flags = RDMA_NL_ADMIN_PERM,
  998. },
  999. };
  1000. static int __init ib_core_init(void)
  1001. {
  1002. int ret;
  1003. ib_wq = alloc_workqueue("infiniband", 0, 0);
  1004. if (!ib_wq)
  1005. return -ENOMEM;
  1006. ib_comp_wq = alloc_workqueue("ib-comp-wq",
  1007. WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
  1008. if (!ib_comp_wq) {
  1009. ret = -ENOMEM;
  1010. goto err;
  1011. }
  1012. ret = class_register(&ib_class);
  1013. if (ret) {
  1014. pr_warn("Couldn't create InfiniBand device class\n");
  1015. goto err_comp;
  1016. }
  1017. ret = rdma_nl_init();
  1018. if (ret) {
  1019. pr_warn("Couldn't init IB netlink interface: err %d\n", ret);
  1020. goto err_sysfs;
  1021. }
  1022. ret = addr_init();
  1023. if (ret) {
  1024. pr_warn("Could't init IB address resolution\n");
  1025. goto err_ibnl;
  1026. }
  1027. ret = ib_mad_init();
  1028. if (ret) {
  1029. pr_warn("Couldn't init IB MAD\n");
  1030. goto err_addr;
  1031. }
  1032. ret = ib_sa_init();
  1033. if (ret) {
  1034. pr_warn("Couldn't init SA\n");
  1035. goto err_mad;
  1036. }
  1037. ret = register_lsm_notifier(&ibdev_lsm_nb);
  1038. if (ret) {
  1039. pr_warn("Couldn't register LSM notifier. ret %d\n", ret);
  1040. goto err_sa;
  1041. }
  1042. nldev_init();
  1043. rdma_nl_register(RDMA_NL_LS, ibnl_ls_cb_table);
  1044. ib_cache_setup();
  1045. return 0;
  1046. err_sa:
  1047. ib_sa_cleanup();
  1048. err_mad:
  1049. ib_mad_cleanup();
  1050. err_addr:
  1051. addr_cleanup();
  1052. err_ibnl:
  1053. rdma_nl_exit();
  1054. err_sysfs:
  1055. class_unregister(&ib_class);
  1056. err_comp:
  1057. destroy_workqueue(ib_comp_wq);
  1058. err:
  1059. destroy_workqueue(ib_wq);
  1060. return ret;
  1061. }
  1062. static void __exit ib_core_cleanup(void)
  1063. {
  1064. ib_cache_cleanup();
  1065. nldev_exit();
  1066. rdma_nl_unregister(RDMA_NL_LS);
  1067. unregister_lsm_notifier(&ibdev_lsm_nb);
  1068. ib_sa_cleanup();
  1069. ib_mad_cleanup();
  1070. addr_cleanup();
  1071. rdma_nl_exit();
  1072. class_unregister(&ib_class);
  1073. destroy_workqueue(ib_comp_wq);
  1074. /* Make sure that any pending umem accounting work is done. */
  1075. destroy_workqueue(ib_wq);
  1076. }
  1077. MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_LS, 4);
  1078. subsys_initcall(ib_core_init);
  1079. module_exit(ib_core_cleanup);