device.c 26 KB

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