device.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. printk(KERN_WARNING "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. printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
  221. device->name, client->name);
  222. return -ENOMEM;
  223. }
  224. context->client = client;
  225. context->data = NULL;
  226. context->going_down = false;
  227. down_write(&lists_rwsem);
  228. spin_lock_irqsave(&device->client_data_lock, flags);
  229. list_add(&context->list, &device->client_data_list);
  230. spin_unlock_irqrestore(&device->client_data_lock, flags);
  231. up_write(&lists_rwsem);
  232. return 0;
  233. }
  234. static int verify_immutable(const struct ib_device *dev, u8 port)
  235. {
  236. return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
  237. rdma_max_mad_size(dev, port) != 0);
  238. }
  239. static int read_port_immutable(struct ib_device *device)
  240. {
  241. int ret;
  242. u8 start_port = rdma_start_port(device);
  243. u8 end_port = rdma_end_port(device);
  244. u8 port;
  245. /**
  246. * device->port_immutable is indexed directly by the port number to make
  247. * access to this data as efficient as possible.
  248. *
  249. * Therefore port_immutable is declared as a 1 based array with
  250. * potential empty slots at the beginning.
  251. */
  252. device->port_immutable = kzalloc(sizeof(*device->port_immutable)
  253. * (end_port + 1),
  254. GFP_KERNEL);
  255. if (!device->port_immutable)
  256. return -ENOMEM;
  257. for (port = start_port; port <= end_port; ++port) {
  258. ret = device->get_port_immutable(device, port,
  259. &device->port_immutable[port]);
  260. if (ret)
  261. return ret;
  262. if (verify_immutable(device, port))
  263. return -EINVAL;
  264. }
  265. return 0;
  266. }
  267. /**
  268. * ib_register_device - Register an IB device with IB core
  269. * @device:Device to register
  270. *
  271. * Low-level drivers use ib_register_device() to register their
  272. * devices with the IB core. All registered clients will receive a
  273. * callback for each device that is added. @device must be allocated
  274. * with ib_alloc_device().
  275. */
  276. int ib_register_device(struct ib_device *device,
  277. int (*port_callback)(struct ib_device *,
  278. u8, struct kobject *))
  279. {
  280. int ret;
  281. struct ib_client *client;
  282. struct ib_udata uhw = {.outlen = 0, .inlen = 0};
  283. mutex_lock(&device_mutex);
  284. if (strchr(device->name, '%')) {
  285. ret = alloc_name(device->name);
  286. if (ret)
  287. goto out;
  288. }
  289. if (ib_device_check_mandatory(device)) {
  290. ret = -EINVAL;
  291. goto out;
  292. }
  293. ret = read_port_immutable(device);
  294. if (ret) {
  295. printk(KERN_WARNING "Couldn't create per port immutable data %s\n",
  296. device->name);
  297. goto out;
  298. }
  299. ret = ib_cache_setup_one(device);
  300. if (ret) {
  301. printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
  302. goto out;
  303. }
  304. memset(&device->attrs, 0, sizeof(device->attrs));
  305. ret = device->query_device(device, &device->attrs, &uhw);
  306. if (ret) {
  307. printk(KERN_WARNING "Couldn't query the device attributes\n");
  308. goto out;
  309. }
  310. ret = ib_device_register_sysfs(device, port_callback);
  311. if (ret) {
  312. printk(KERN_WARNING "Couldn't register device %s with driver model\n",
  313. device->name);
  314. ib_cache_cleanup_one(device);
  315. goto out;
  316. }
  317. device->reg_state = IB_DEV_REGISTERED;
  318. list_for_each_entry(client, &client_list, list)
  319. if (client->add && !add_client_context(device, client))
  320. client->add(device);
  321. down_write(&lists_rwsem);
  322. list_add_tail(&device->core_list, &device_list);
  323. up_write(&lists_rwsem);
  324. out:
  325. mutex_unlock(&device_mutex);
  326. return ret;
  327. }
  328. EXPORT_SYMBOL(ib_register_device);
  329. /**
  330. * ib_unregister_device - Unregister an IB device
  331. * @device:Device to unregister
  332. *
  333. * Unregister an IB device. All clients will receive a remove callback.
  334. */
  335. void ib_unregister_device(struct ib_device *device)
  336. {
  337. struct ib_client_data *context, *tmp;
  338. unsigned long flags;
  339. mutex_lock(&device_mutex);
  340. down_write(&lists_rwsem);
  341. list_del(&device->core_list);
  342. spin_lock_irqsave(&device->client_data_lock, flags);
  343. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  344. context->going_down = true;
  345. spin_unlock_irqrestore(&device->client_data_lock, flags);
  346. downgrade_write(&lists_rwsem);
  347. list_for_each_entry_safe(context, tmp, &device->client_data_list,
  348. list) {
  349. if (context->client->remove)
  350. context->client->remove(device, context->data);
  351. }
  352. up_read(&lists_rwsem);
  353. mutex_unlock(&device_mutex);
  354. ib_device_unregister_sysfs(device);
  355. ib_cache_cleanup_one(device);
  356. down_write(&lists_rwsem);
  357. spin_lock_irqsave(&device->client_data_lock, flags);
  358. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  359. kfree(context);
  360. spin_unlock_irqrestore(&device->client_data_lock, flags);
  361. up_write(&lists_rwsem);
  362. device->reg_state = IB_DEV_UNREGISTERED;
  363. }
  364. EXPORT_SYMBOL(ib_unregister_device);
  365. /**
  366. * ib_register_client - Register an IB client
  367. * @client:Client to register
  368. *
  369. * Upper level users of the IB drivers can use ib_register_client() to
  370. * register callbacks for IB device addition and removal. When an IB
  371. * device is added, each registered client's add method will be called
  372. * (in the order the clients were registered), and when a device is
  373. * removed, each client's remove method will be called (in the reverse
  374. * order that clients were registered). In addition, when
  375. * ib_register_client() is called, the client will receive an add
  376. * callback for all devices already registered.
  377. */
  378. int ib_register_client(struct ib_client *client)
  379. {
  380. struct ib_device *device;
  381. mutex_lock(&device_mutex);
  382. list_for_each_entry(device, &device_list, core_list)
  383. if (client->add && !add_client_context(device, client))
  384. client->add(device);
  385. down_write(&lists_rwsem);
  386. list_add_tail(&client->list, &client_list);
  387. up_write(&lists_rwsem);
  388. mutex_unlock(&device_mutex);
  389. return 0;
  390. }
  391. EXPORT_SYMBOL(ib_register_client);
  392. /**
  393. * ib_unregister_client - Unregister an IB client
  394. * @client:Client to unregister
  395. *
  396. * Upper level users use ib_unregister_client() to remove their client
  397. * registration. When ib_unregister_client() is called, the client
  398. * will receive a remove callback for each IB device still registered.
  399. */
  400. void ib_unregister_client(struct ib_client *client)
  401. {
  402. struct ib_client_data *context, *tmp;
  403. struct ib_device *device;
  404. unsigned long flags;
  405. mutex_lock(&device_mutex);
  406. down_write(&lists_rwsem);
  407. list_del(&client->list);
  408. up_write(&lists_rwsem);
  409. list_for_each_entry(device, &device_list, core_list) {
  410. struct ib_client_data *found_context = NULL;
  411. down_write(&lists_rwsem);
  412. spin_lock_irqsave(&device->client_data_lock, flags);
  413. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  414. if (context->client == client) {
  415. context->going_down = true;
  416. found_context = context;
  417. break;
  418. }
  419. spin_unlock_irqrestore(&device->client_data_lock, flags);
  420. up_write(&lists_rwsem);
  421. if (client->remove)
  422. client->remove(device, found_context ?
  423. found_context->data : NULL);
  424. if (!found_context) {
  425. pr_warn("No client context found for %s/%s\n",
  426. device->name, client->name);
  427. continue;
  428. }
  429. down_write(&lists_rwsem);
  430. spin_lock_irqsave(&device->client_data_lock, flags);
  431. list_del(&found_context->list);
  432. kfree(found_context);
  433. spin_unlock_irqrestore(&device->client_data_lock, flags);
  434. up_write(&lists_rwsem);
  435. }
  436. mutex_unlock(&device_mutex);
  437. }
  438. EXPORT_SYMBOL(ib_unregister_client);
  439. /**
  440. * ib_get_client_data - Get IB client context
  441. * @device:Device to get context for
  442. * @client:Client to get context for
  443. *
  444. * ib_get_client_data() returns client context set with
  445. * ib_set_client_data().
  446. */
  447. void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
  448. {
  449. struct ib_client_data *context;
  450. void *ret = NULL;
  451. unsigned long flags;
  452. spin_lock_irqsave(&device->client_data_lock, flags);
  453. list_for_each_entry(context, &device->client_data_list, list)
  454. if (context->client == client) {
  455. ret = context->data;
  456. break;
  457. }
  458. spin_unlock_irqrestore(&device->client_data_lock, flags);
  459. return ret;
  460. }
  461. EXPORT_SYMBOL(ib_get_client_data);
  462. /**
  463. * ib_set_client_data - Set IB client context
  464. * @device:Device to set context for
  465. * @client:Client to set context for
  466. * @data:Context to set
  467. *
  468. * ib_set_client_data() sets client context that can be retrieved with
  469. * ib_get_client_data().
  470. */
  471. void ib_set_client_data(struct ib_device *device, struct ib_client *client,
  472. void *data)
  473. {
  474. struct ib_client_data *context;
  475. unsigned long flags;
  476. spin_lock_irqsave(&device->client_data_lock, flags);
  477. list_for_each_entry(context, &device->client_data_list, list)
  478. if (context->client == client) {
  479. context->data = data;
  480. goto out;
  481. }
  482. printk(KERN_WARNING "No client context found for %s/%s\n",
  483. device->name, client->name);
  484. out:
  485. spin_unlock_irqrestore(&device->client_data_lock, flags);
  486. }
  487. EXPORT_SYMBOL(ib_set_client_data);
  488. /**
  489. * ib_register_event_handler - Register an IB event handler
  490. * @event_handler:Handler to register
  491. *
  492. * ib_register_event_handler() registers an event handler that will be
  493. * called back when asynchronous IB events occur (as defined in
  494. * chapter 11 of the InfiniBand Architecture Specification). This
  495. * callback may occur in interrupt context.
  496. */
  497. int ib_register_event_handler (struct ib_event_handler *event_handler)
  498. {
  499. unsigned long flags;
  500. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  501. list_add_tail(&event_handler->list,
  502. &event_handler->device->event_handler_list);
  503. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  504. return 0;
  505. }
  506. EXPORT_SYMBOL(ib_register_event_handler);
  507. /**
  508. * ib_unregister_event_handler - Unregister an event handler
  509. * @event_handler:Handler to unregister
  510. *
  511. * Unregister an event handler registered with
  512. * ib_register_event_handler().
  513. */
  514. int ib_unregister_event_handler(struct ib_event_handler *event_handler)
  515. {
  516. unsigned long flags;
  517. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  518. list_del(&event_handler->list);
  519. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  520. return 0;
  521. }
  522. EXPORT_SYMBOL(ib_unregister_event_handler);
  523. /**
  524. * ib_dispatch_event - Dispatch an asynchronous event
  525. * @event:Event to dispatch
  526. *
  527. * Low-level drivers must call ib_dispatch_event() to dispatch the
  528. * event to all registered event handlers when an asynchronous event
  529. * occurs.
  530. */
  531. void ib_dispatch_event(struct ib_event *event)
  532. {
  533. unsigned long flags;
  534. struct ib_event_handler *handler;
  535. spin_lock_irqsave(&event->device->event_handler_lock, flags);
  536. list_for_each_entry(handler, &event->device->event_handler_list, list)
  537. handler->handler(handler, event);
  538. spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
  539. }
  540. EXPORT_SYMBOL(ib_dispatch_event);
  541. /**
  542. * ib_query_port - Query IB port attributes
  543. * @device:Device to query
  544. * @port_num:Port number to query
  545. * @port_attr:Port attributes
  546. *
  547. * ib_query_port() returns the attributes of a port through the
  548. * @port_attr pointer.
  549. */
  550. int ib_query_port(struct ib_device *device,
  551. u8 port_num,
  552. struct ib_port_attr *port_attr)
  553. {
  554. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  555. return -EINVAL;
  556. return device->query_port(device, port_num, port_attr);
  557. }
  558. EXPORT_SYMBOL(ib_query_port);
  559. /**
  560. * ib_query_gid - Get GID table entry
  561. * @device:Device to query
  562. * @port_num:Port number to query
  563. * @index:GID table index to query
  564. * @gid:Returned GID
  565. * @attr: Returned GID attributes related to this GID index (only in RoCE).
  566. * NULL means ignore.
  567. *
  568. * ib_query_gid() fetches the specified GID table entry.
  569. */
  570. int ib_query_gid(struct ib_device *device,
  571. u8 port_num, int index, union ib_gid *gid,
  572. struct ib_gid_attr *attr)
  573. {
  574. if (rdma_cap_roce_gid_table(device, port_num))
  575. return ib_get_cached_gid(device, port_num, index, gid, attr);
  576. if (attr)
  577. return -EINVAL;
  578. return device->query_gid(device, port_num, index, gid);
  579. }
  580. EXPORT_SYMBOL(ib_query_gid);
  581. /**
  582. * ib_enum_roce_netdev - enumerate all RoCE ports
  583. * @ib_dev : IB device we want to query
  584. * @filter: Should we call the callback?
  585. * @filter_cookie: Cookie passed to filter
  586. * @cb: Callback to call for each found RoCE ports
  587. * @cookie: Cookie passed back to the callback
  588. *
  589. * Enumerates all of the physical RoCE ports of ib_dev
  590. * which are related to netdevice and calls callback() on each
  591. * device for which filter() function returns non zero.
  592. */
  593. void ib_enum_roce_netdev(struct ib_device *ib_dev,
  594. roce_netdev_filter filter,
  595. void *filter_cookie,
  596. roce_netdev_callback cb,
  597. void *cookie)
  598. {
  599. u8 port;
  600. for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
  601. port++)
  602. if (rdma_protocol_roce(ib_dev, port)) {
  603. struct net_device *idev = NULL;
  604. if (ib_dev->get_netdev)
  605. idev = ib_dev->get_netdev(ib_dev, port);
  606. if (idev &&
  607. idev->reg_state >= NETREG_UNREGISTERED) {
  608. dev_put(idev);
  609. idev = NULL;
  610. }
  611. if (filter(ib_dev, port, idev, filter_cookie))
  612. cb(ib_dev, port, idev, cookie);
  613. if (idev)
  614. dev_put(idev);
  615. }
  616. }
  617. /**
  618. * ib_enum_all_roce_netdevs - enumerate all RoCE devices
  619. * @filter: Should we call the callback?
  620. * @filter_cookie: Cookie passed to filter
  621. * @cb: Callback to call for each found RoCE ports
  622. * @cookie: Cookie passed back to the callback
  623. *
  624. * Enumerates all RoCE devices' physical ports which are related
  625. * to netdevices and calls callback() on each device for which
  626. * filter() function returns non zero.
  627. */
  628. void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
  629. void *filter_cookie,
  630. roce_netdev_callback cb,
  631. void *cookie)
  632. {
  633. struct ib_device *dev;
  634. down_read(&lists_rwsem);
  635. list_for_each_entry(dev, &device_list, core_list)
  636. ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
  637. up_read(&lists_rwsem);
  638. }
  639. /**
  640. * ib_query_pkey - Get P_Key table entry
  641. * @device:Device to query
  642. * @port_num:Port number to query
  643. * @index:P_Key table index to query
  644. * @pkey:Returned P_Key
  645. *
  646. * ib_query_pkey() fetches the specified P_Key table entry.
  647. */
  648. int ib_query_pkey(struct ib_device *device,
  649. u8 port_num, u16 index, u16 *pkey)
  650. {
  651. return device->query_pkey(device, port_num, index, pkey);
  652. }
  653. EXPORT_SYMBOL(ib_query_pkey);
  654. /**
  655. * ib_modify_device - Change IB device attributes
  656. * @device:Device to modify
  657. * @device_modify_mask:Mask of attributes to change
  658. * @device_modify:New attribute values
  659. *
  660. * ib_modify_device() changes a device's attributes as specified by
  661. * the @device_modify_mask and @device_modify structure.
  662. */
  663. int ib_modify_device(struct ib_device *device,
  664. int device_modify_mask,
  665. struct ib_device_modify *device_modify)
  666. {
  667. if (!device->modify_device)
  668. return -ENOSYS;
  669. return device->modify_device(device, device_modify_mask,
  670. device_modify);
  671. }
  672. EXPORT_SYMBOL(ib_modify_device);
  673. /**
  674. * ib_modify_port - Modifies the attributes for the specified port.
  675. * @device: The device to modify.
  676. * @port_num: The number of the port to modify.
  677. * @port_modify_mask: Mask used to specify which attributes of the port
  678. * to change.
  679. * @port_modify: New attribute values for the port.
  680. *
  681. * ib_modify_port() changes a port's attributes as specified by the
  682. * @port_modify_mask and @port_modify structure.
  683. */
  684. int ib_modify_port(struct ib_device *device,
  685. u8 port_num, int port_modify_mask,
  686. struct ib_port_modify *port_modify)
  687. {
  688. if (!device->modify_port)
  689. return -ENOSYS;
  690. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  691. return -EINVAL;
  692. return device->modify_port(device, port_num, port_modify_mask,
  693. port_modify);
  694. }
  695. EXPORT_SYMBOL(ib_modify_port);
  696. /**
  697. * ib_find_gid - Returns the port number and GID table index where
  698. * a specified GID value occurs.
  699. * @device: The device to query.
  700. * @gid: The GID value to search for.
  701. * @gid_type: Type of GID.
  702. * @ndev: The ndev related to the GID to search for.
  703. * @port_num: The port number of the device where the GID value was found.
  704. * @index: The index into the GID table where the GID was found. This
  705. * parameter may be NULL.
  706. */
  707. int ib_find_gid(struct ib_device *device, union ib_gid *gid,
  708. enum ib_gid_type gid_type, struct net_device *ndev,
  709. u8 *port_num, u16 *index)
  710. {
  711. union ib_gid tmp_gid;
  712. int ret, port, i;
  713. for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
  714. if (rdma_cap_roce_gid_table(device, port)) {
  715. if (!ib_find_cached_gid_by_port(device, gid, gid_type, port,
  716. ndev, index)) {
  717. *port_num = port;
  718. return 0;
  719. }
  720. }
  721. if (gid_type != IB_GID_TYPE_IB)
  722. continue;
  723. for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
  724. ret = ib_query_gid(device, port, i, &tmp_gid, NULL);
  725. if (ret)
  726. return ret;
  727. if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
  728. *port_num = port;
  729. if (index)
  730. *index = i;
  731. return 0;
  732. }
  733. }
  734. }
  735. return -ENOENT;
  736. }
  737. EXPORT_SYMBOL(ib_find_gid);
  738. /**
  739. * ib_find_pkey - Returns the PKey table index where a specified
  740. * PKey value occurs.
  741. * @device: The device to query.
  742. * @port_num: The port number of the device to search for the PKey.
  743. * @pkey: The PKey value to search for.
  744. * @index: The index into the PKey table where the PKey was found.
  745. */
  746. int ib_find_pkey(struct ib_device *device,
  747. u8 port_num, u16 pkey, u16 *index)
  748. {
  749. int ret, i;
  750. u16 tmp_pkey;
  751. int partial_ix = -1;
  752. for (i = 0; i < device->port_immutable[port_num].pkey_tbl_len; ++i) {
  753. ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
  754. if (ret)
  755. return ret;
  756. if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) {
  757. /* if there is full-member pkey take it.*/
  758. if (tmp_pkey & 0x8000) {
  759. *index = i;
  760. return 0;
  761. }
  762. if (partial_ix < 0)
  763. partial_ix = i;
  764. }
  765. }
  766. /*no full-member, if exists take the limited*/
  767. if (partial_ix >= 0) {
  768. *index = partial_ix;
  769. return 0;
  770. }
  771. return -ENOENT;
  772. }
  773. EXPORT_SYMBOL(ib_find_pkey);
  774. /**
  775. * ib_get_net_dev_by_params() - Return the appropriate net_dev
  776. * for a received CM request
  777. * @dev: An RDMA device on which the request has been received.
  778. * @port: Port number on the RDMA device.
  779. * @pkey: The Pkey the request came on.
  780. * @gid: A GID that the net_dev uses to communicate.
  781. * @addr: Contains the IP address that the request specified as its
  782. * destination.
  783. */
  784. struct net_device *ib_get_net_dev_by_params(struct ib_device *dev,
  785. u8 port,
  786. u16 pkey,
  787. const union ib_gid *gid,
  788. const struct sockaddr *addr)
  789. {
  790. struct net_device *net_dev = NULL;
  791. struct ib_client_data *context;
  792. if (!rdma_protocol_ib(dev, port))
  793. return NULL;
  794. down_read(&lists_rwsem);
  795. list_for_each_entry(context, &dev->client_data_list, list) {
  796. struct ib_client *client = context->client;
  797. if (context->going_down)
  798. continue;
  799. if (client->get_net_dev_by_params) {
  800. net_dev = client->get_net_dev_by_params(dev, port, pkey,
  801. gid, addr,
  802. context->data);
  803. if (net_dev)
  804. break;
  805. }
  806. }
  807. up_read(&lists_rwsem);
  808. return net_dev;
  809. }
  810. EXPORT_SYMBOL(ib_get_net_dev_by_params);
  811. static int __init ib_core_init(void)
  812. {
  813. int ret;
  814. ib_wq = alloc_workqueue("infiniband", 0, 0);
  815. if (!ib_wq)
  816. return -ENOMEM;
  817. ib_comp_wq = alloc_workqueue("ib-comp-wq",
  818. WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM,
  819. WQ_UNBOUND_MAX_ACTIVE);
  820. if (!ib_comp_wq) {
  821. ret = -ENOMEM;
  822. goto err;
  823. }
  824. ret = class_register(&ib_class);
  825. if (ret) {
  826. printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
  827. goto err_comp;
  828. }
  829. ret = ibnl_init();
  830. if (ret) {
  831. printk(KERN_WARNING "Couldn't init IB netlink interface\n");
  832. goto err_sysfs;
  833. }
  834. ib_cache_setup();
  835. return 0;
  836. err_sysfs:
  837. class_unregister(&ib_class);
  838. err_comp:
  839. destroy_workqueue(ib_comp_wq);
  840. err:
  841. destroy_workqueue(ib_wq);
  842. return ret;
  843. }
  844. static void __exit ib_core_cleanup(void)
  845. {
  846. ib_cache_cleanup();
  847. ibnl_cleanup();
  848. class_unregister(&ib_class);
  849. destroy_workqueue(ib_comp_wq);
  850. /* Make sure that any pending umem accounting work is done. */
  851. destroy_workqueue(ib_wq);
  852. }
  853. module_init(ib_core_init);
  854. module_exit(ib_core_cleanup);