device.c 29 KB

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