device.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. mutex_lock(&device_mutex);
  281. if (strchr(device->name, '%')) {
  282. ret = alloc_name(device->name);
  283. if (ret)
  284. goto out;
  285. }
  286. if (ib_device_check_mandatory(device)) {
  287. ret = -EINVAL;
  288. goto out;
  289. }
  290. ret = read_port_immutable(device);
  291. if (ret) {
  292. printk(KERN_WARNING "Couldn't create per port immutable data %s\n",
  293. device->name);
  294. goto out;
  295. }
  296. ret = ib_cache_setup_one(device);
  297. if (ret) {
  298. printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
  299. goto out;
  300. }
  301. ret = ib_device_register_sysfs(device, port_callback);
  302. if (ret) {
  303. printk(KERN_WARNING "Couldn't register device %s with driver model\n",
  304. device->name);
  305. ib_cache_cleanup_one(device);
  306. goto out;
  307. }
  308. device->reg_state = IB_DEV_REGISTERED;
  309. {
  310. struct ib_client *client;
  311. list_for_each_entry(client, &client_list, list)
  312. if (client->add && !add_client_context(device, client))
  313. client->add(device);
  314. }
  315. down_write(&lists_rwsem);
  316. list_add_tail(&device->core_list, &device_list);
  317. up_write(&lists_rwsem);
  318. out:
  319. mutex_unlock(&device_mutex);
  320. return ret;
  321. }
  322. EXPORT_SYMBOL(ib_register_device);
  323. /**
  324. * ib_unregister_device - Unregister an IB device
  325. * @device:Device to unregister
  326. *
  327. * Unregister an IB device. All clients will receive a remove callback.
  328. */
  329. void ib_unregister_device(struct ib_device *device)
  330. {
  331. struct ib_client_data *context, *tmp;
  332. unsigned long flags;
  333. mutex_lock(&device_mutex);
  334. down_write(&lists_rwsem);
  335. list_del(&device->core_list);
  336. spin_lock_irqsave(&device->client_data_lock, flags);
  337. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  338. context->going_down = true;
  339. spin_unlock_irqrestore(&device->client_data_lock, flags);
  340. downgrade_write(&lists_rwsem);
  341. list_for_each_entry_safe(context, tmp, &device->client_data_list,
  342. list) {
  343. if (context->client->remove)
  344. context->client->remove(device, context->data);
  345. }
  346. up_read(&lists_rwsem);
  347. mutex_unlock(&device_mutex);
  348. ib_device_unregister_sysfs(device);
  349. ib_cache_cleanup_one(device);
  350. down_write(&lists_rwsem);
  351. spin_lock_irqsave(&device->client_data_lock, flags);
  352. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  353. kfree(context);
  354. spin_unlock_irqrestore(&device->client_data_lock, flags);
  355. up_write(&lists_rwsem);
  356. device->reg_state = IB_DEV_UNREGISTERED;
  357. }
  358. EXPORT_SYMBOL(ib_unregister_device);
  359. /**
  360. * ib_register_client - Register an IB client
  361. * @client:Client to register
  362. *
  363. * Upper level users of the IB drivers can use ib_register_client() to
  364. * register callbacks for IB device addition and removal. When an IB
  365. * device is added, each registered client's add method will be called
  366. * (in the order the clients were registered), and when a device is
  367. * removed, each client's remove method will be called (in the reverse
  368. * order that clients were registered). In addition, when
  369. * ib_register_client() is called, the client will receive an add
  370. * callback for all devices already registered.
  371. */
  372. int ib_register_client(struct ib_client *client)
  373. {
  374. struct ib_device *device;
  375. mutex_lock(&device_mutex);
  376. list_for_each_entry(device, &device_list, core_list)
  377. if (client->add && !add_client_context(device, client))
  378. client->add(device);
  379. down_write(&lists_rwsem);
  380. list_add_tail(&client->list, &client_list);
  381. up_write(&lists_rwsem);
  382. mutex_unlock(&device_mutex);
  383. return 0;
  384. }
  385. EXPORT_SYMBOL(ib_register_client);
  386. /**
  387. * ib_unregister_client - Unregister an IB client
  388. * @client:Client to unregister
  389. *
  390. * Upper level users use ib_unregister_client() to remove their client
  391. * registration. When ib_unregister_client() is called, the client
  392. * will receive a remove callback for each IB device still registered.
  393. */
  394. void ib_unregister_client(struct ib_client *client)
  395. {
  396. struct ib_client_data *context, *tmp;
  397. struct ib_device *device;
  398. unsigned long flags;
  399. mutex_lock(&device_mutex);
  400. down_write(&lists_rwsem);
  401. list_del(&client->list);
  402. up_write(&lists_rwsem);
  403. list_for_each_entry(device, &device_list, core_list) {
  404. struct ib_client_data *found_context = NULL;
  405. down_write(&lists_rwsem);
  406. spin_lock_irqsave(&device->client_data_lock, flags);
  407. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  408. if (context->client == client) {
  409. context->going_down = true;
  410. found_context = context;
  411. break;
  412. }
  413. spin_unlock_irqrestore(&device->client_data_lock, flags);
  414. up_write(&lists_rwsem);
  415. if (client->remove)
  416. client->remove(device, found_context ?
  417. found_context->data : NULL);
  418. if (!found_context) {
  419. pr_warn("No client context found for %s/%s\n",
  420. device->name, client->name);
  421. continue;
  422. }
  423. down_write(&lists_rwsem);
  424. spin_lock_irqsave(&device->client_data_lock, flags);
  425. list_del(&found_context->list);
  426. kfree(found_context);
  427. spin_unlock_irqrestore(&device->client_data_lock, flags);
  428. up_write(&lists_rwsem);
  429. }
  430. mutex_unlock(&device_mutex);
  431. }
  432. EXPORT_SYMBOL(ib_unregister_client);
  433. /**
  434. * ib_get_client_data - Get IB client context
  435. * @device:Device to get context for
  436. * @client:Client to get context for
  437. *
  438. * ib_get_client_data() returns client context set with
  439. * ib_set_client_data().
  440. */
  441. void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
  442. {
  443. struct ib_client_data *context;
  444. void *ret = NULL;
  445. unsigned long flags;
  446. spin_lock_irqsave(&device->client_data_lock, flags);
  447. list_for_each_entry(context, &device->client_data_list, list)
  448. if (context->client == client) {
  449. ret = context->data;
  450. break;
  451. }
  452. spin_unlock_irqrestore(&device->client_data_lock, flags);
  453. return ret;
  454. }
  455. EXPORT_SYMBOL(ib_get_client_data);
  456. /**
  457. * ib_set_client_data - Set IB client context
  458. * @device:Device to set context for
  459. * @client:Client to set context for
  460. * @data:Context to set
  461. *
  462. * ib_set_client_data() sets client context that can be retrieved with
  463. * ib_get_client_data().
  464. */
  465. void ib_set_client_data(struct ib_device *device, struct ib_client *client,
  466. void *data)
  467. {
  468. struct ib_client_data *context;
  469. unsigned long flags;
  470. spin_lock_irqsave(&device->client_data_lock, flags);
  471. list_for_each_entry(context, &device->client_data_list, list)
  472. if (context->client == client) {
  473. context->data = data;
  474. goto out;
  475. }
  476. printk(KERN_WARNING "No client context found for %s/%s\n",
  477. device->name, client->name);
  478. out:
  479. spin_unlock_irqrestore(&device->client_data_lock, flags);
  480. }
  481. EXPORT_SYMBOL(ib_set_client_data);
  482. /**
  483. * ib_register_event_handler - Register an IB event handler
  484. * @event_handler:Handler to register
  485. *
  486. * ib_register_event_handler() registers an event handler that will be
  487. * called back when asynchronous IB events occur (as defined in
  488. * chapter 11 of the InfiniBand Architecture Specification). This
  489. * callback may occur in interrupt context.
  490. */
  491. int ib_register_event_handler (struct ib_event_handler *event_handler)
  492. {
  493. unsigned long flags;
  494. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  495. list_add_tail(&event_handler->list,
  496. &event_handler->device->event_handler_list);
  497. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  498. return 0;
  499. }
  500. EXPORT_SYMBOL(ib_register_event_handler);
  501. /**
  502. * ib_unregister_event_handler - Unregister an event handler
  503. * @event_handler:Handler to unregister
  504. *
  505. * Unregister an event handler registered with
  506. * ib_register_event_handler().
  507. */
  508. int ib_unregister_event_handler(struct ib_event_handler *event_handler)
  509. {
  510. unsigned long flags;
  511. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  512. list_del(&event_handler->list);
  513. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  514. return 0;
  515. }
  516. EXPORT_SYMBOL(ib_unregister_event_handler);
  517. /**
  518. * ib_dispatch_event - Dispatch an asynchronous event
  519. * @event:Event to dispatch
  520. *
  521. * Low-level drivers must call ib_dispatch_event() to dispatch the
  522. * event to all registered event handlers when an asynchronous event
  523. * occurs.
  524. */
  525. void ib_dispatch_event(struct ib_event *event)
  526. {
  527. unsigned long flags;
  528. struct ib_event_handler *handler;
  529. spin_lock_irqsave(&event->device->event_handler_lock, flags);
  530. list_for_each_entry(handler, &event->device->event_handler_list, list)
  531. handler->handler(handler, event);
  532. spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
  533. }
  534. EXPORT_SYMBOL(ib_dispatch_event);
  535. /**
  536. * ib_query_device - Query IB device attributes
  537. * @device:Device to query
  538. * @device_attr:Device attributes
  539. *
  540. * ib_query_device() returns the attributes of a device through the
  541. * @device_attr pointer.
  542. */
  543. int ib_query_device(struct ib_device *device,
  544. struct ib_device_attr *device_attr)
  545. {
  546. struct ib_udata uhw = {.outlen = 0, .inlen = 0};
  547. memset(device_attr, 0, sizeof(*device_attr));
  548. return device->query_device(device, device_attr, &uhw);
  549. }
  550. EXPORT_SYMBOL(ib_query_device);
  551. /**
  552. * ib_query_port - Query IB port attributes
  553. * @device:Device to query
  554. * @port_num:Port number to query
  555. * @port_attr:Port attributes
  556. *
  557. * ib_query_port() returns the attributes of a port through the
  558. * @port_attr pointer.
  559. */
  560. int ib_query_port(struct ib_device *device,
  561. u8 port_num,
  562. struct ib_port_attr *port_attr)
  563. {
  564. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  565. return -EINVAL;
  566. return device->query_port(device, port_num, port_attr);
  567. }
  568. EXPORT_SYMBOL(ib_query_port);
  569. /**
  570. * ib_query_gid - Get GID table entry
  571. * @device:Device to query
  572. * @port_num:Port number to query
  573. * @index:GID table index to query
  574. * @gid:Returned GID
  575. *
  576. * ib_query_gid() fetches the specified GID table entry.
  577. */
  578. int ib_query_gid(struct ib_device *device,
  579. u8 port_num, int index, union ib_gid *gid)
  580. {
  581. if (rdma_cap_roce_gid_table(device, port_num))
  582. return ib_get_cached_gid(device, port_num, index, gid);
  583. return device->query_gid(device, port_num, index, gid);
  584. }
  585. EXPORT_SYMBOL(ib_query_gid);
  586. /**
  587. * ib_enum_roce_netdev - enumerate all RoCE ports
  588. * @ib_dev : IB device we want to query
  589. * @filter: Should we call the callback?
  590. * @filter_cookie: Cookie passed to filter
  591. * @cb: Callback to call for each found RoCE ports
  592. * @cookie: Cookie passed back to the callback
  593. *
  594. * Enumerates all of the physical RoCE ports of ib_dev
  595. * which are related to netdevice and calls callback() on each
  596. * device for which filter() function returns non zero.
  597. */
  598. void ib_enum_roce_netdev(struct ib_device *ib_dev,
  599. roce_netdev_filter filter,
  600. void *filter_cookie,
  601. roce_netdev_callback cb,
  602. void *cookie)
  603. {
  604. u8 port;
  605. for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
  606. port++)
  607. if (rdma_protocol_roce(ib_dev, port)) {
  608. struct net_device *idev = NULL;
  609. if (ib_dev->get_netdev)
  610. idev = ib_dev->get_netdev(ib_dev, port);
  611. if (idev &&
  612. idev->reg_state >= NETREG_UNREGISTERED) {
  613. dev_put(idev);
  614. idev = NULL;
  615. }
  616. if (filter(ib_dev, port, idev, filter_cookie))
  617. cb(ib_dev, port, idev, cookie);
  618. if (idev)
  619. dev_put(idev);
  620. }
  621. }
  622. /**
  623. * ib_enum_all_roce_netdevs - enumerate all RoCE devices
  624. * @filter: Should we call the callback?
  625. * @filter_cookie: Cookie passed to filter
  626. * @cb: Callback to call for each found RoCE ports
  627. * @cookie: Cookie passed back to the callback
  628. *
  629. * Enumerates all RoCE devices' physical ports which are related
  630. * to netdevices and calls callback() on each device for which
  631. * filter() function returns non zero.
  632. */
  633. void ib_enum_all_roce_netdevs(roce_netdev_filter filter,
  634. void *filter_cookie,
  635. roce_netdev_callback cb,
  636. void *cookie)
  637. {
  638. struct ib_device *dev;
  639. down_read(&lists_rwsem);
  640. list_for_each_entry(dev, &device_list, core_list)
  641. ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie);
  642. up_read(&lists_rwsem);
  643. }
  644. /**
  645. * ib_query_pkey - Get P_Key table entry
  646. * @device:Device to query
  647. * @port_num:Port number to query
  648. * @index:P_Key table index to query
  649. * @pkey:Returned P_Key
  650. *
  651. * ib_query_pkey() fetches the specified P_Key table entry.
  652. */
  653. int ib_query_pkey(struct ib_device *device,
  654. u8 port_num, u16 index, u16 *pkey)
  655. {
  656. return device->query_pkey(device, port_num, index, pkey);
  657. }
  658. EXPORT_SYMBOL(ib_query_pkey);
  659. /**
  660. * ib_modify_device - Change IB device attributes
  661. * @device:Device to modify
  662. * @device_modify_mask:Mask of attributes to change
  663. * @device_modify:New attribute values
  664. *
  665. * ib_modify_device() changes a device's attributes as specified by
  666. * the @device_modify_mask and @device_modify structure.
  667. */
  668. int ib_modify_device(struct ib_device *device,
  669. int device_modify_mask,
  670. struct ib_device_modify *device_modify)
  671. {
  672. if (!device->modify_device)
  673. return -ENOSYS;
  674. return device->modify_device(device, device_modify_mask,
  675. device_modify);
  676. }
  677. EXPORT_SYMBOL(ib_modify_device);
  678. /**
  679. * ib_modify_port - Modifies the attributes for the specified port.
  680. * @device: The device to modify.
  681. * @port_num: The number of the port to modify.
  682. * @port_modify_mask: Mask used to specify which attributes of the port
  683. * to change.
  684. * @port_modify: New attribute values for the port.
  685. *
  686. * ib_modify_port() changes a port's attributes as specified by the
  687. * @port_modify_mask and @port_modify structure.
  688. */
  689. int ib_modify_port(struct ib_device *device,
  690. u8 port_num, int port_modify_mask,
  691. struct ib_port_modify *port_modify)
  692. {
  693. if (!device->modify_port)
  694. return -ENOSYS;
  695. if (port_num < rdma_start_port(device) || port_num > rdma_end_port(device))
  696. return -EINVAL;
  697. return device->modify_port(device, port_num, port_modify_mask,
  698. port_modify);
  699. }
  700. EXPORT_SYMBOL(ib_modify_port);
  701. /**
  702. * ib_find_gid - Returns the port number and GID table index where
  703. * a specified GID value occurs.
  704. * @device: The device to query.
  705. * @gid: The GID value to search for.
  706. * @port_num: The port number of the device where the GID value was found.
  707. * @index: The index into the GID table where the GID was found. This
  708. * parameter may be NULL.
  709. */
  710. int ib_find_gid(struct ib_device *device, union ib_gid *gid,
  711. u8 *port_num, u16 *index)
  712. {
  713. union ib_gid tmp_gid;
  714. int ret, port, i;
  715. for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
  716. if (rdma_cap_roce_gid_table(device, port)) {
  717. if (!ib_cache_gid_find_by_port(device, gid, port,
  718. NULL, index)) {
  719. *port_num = port;
  720. return 0;
  721. }
  722. }
  723. for (i = 0; i < device->port_immutable[port].gid_tbl_len; ++i) {
  724. ret = ib_query_gid(device, port, i, &tmp_gid);
  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. ret = class_register(&ib_class);
  818. if (ret) {
  819. printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
  820. goto err;
  821. }
  822. ret = ibnl_init();
  823. if (ret) {
  824. printk(KERN_WARNING "Couldn't init IB netlink interface\n");
  825. goto err_sysfs;
  826. }
  827. ib_cache_setup();
  828. return 0;
  829. err_sysfs:
  830. class_unregister(&ib_class);
  831. err:
  832. destroy_workqueue(ib_wq);
  833. return ret;
  834. }
  835. static void __exit ib_core_cleanup(void)
  836. {
  837. ib_cache_cleanup();
  838. ibnl_cleanup();
  839. class_unregister(&ib_class);
  840. /* Make sure that any pending umem accounting work is done. */
  841. destroy_workqueue(ib_wq);
  842. }
  843. module_init(ib_core_init);
  844. module_exit(ib_core_cleanup);