device.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. * $Id: device.c 1349 2004-12-16 21:09:43Z roland $
  34. */
  35. #include <linux/module.h>
  36. #include <linux/string.h>
  37. #include <linux/errno.h>
  38. #include <linux/kernel.h>
  39. #include <linux/slab.h>
  40. #include <linux/init.h>
  41. #include <linux/mutex.h>
  42. #include "core_priv.h"
  43. MODULE_AUTHOR("Roland Dreier");
  44. MODULE_DESCRIPTION("core kernel InfiniBand API");
  45. MODULE_LICENSE("Dual BSD/GPL");
  46. struct ib_client_data {
  47. struct list_head list;
  48. struct ib_client *client;
  49. void * data;
  50. };
  51. static LIST_HEAD(device_list);
  52. static LIST_HEAD(client_list);
  53. /*
  54. * device_mutex protects access to both device_list and client_list.
  55. * There's no real point to using multiple locks or something fancier
  56. * like an rwsem: we always access both lists, and we're always
  57. * modifying one list or the other list. In any case this is not a
  58. * hot path so there's no point in trying to optimize.
  59. */
  60. static DEFINE_MUTEX(device_mutex);
  61. static int ib_device_check_mandatory(struct ib_device *device)
  62. {
  63. #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device, x), #x }
  64. static const struct {
  65. size_t offset;
  66. char *name;
  67. } mandatory_table[] = {
  68. IB_MANDATORY_FUNC(query_device),
  69. IB_MANDATORY_FUNC(query_port),
  70. IB_MANDATORY_FUNC(query_pkey),
  71. IB_MANDATORY_FUNC(query_gid),
  72. IB_MANDATORY_FUNC(alloc_pd),
  73. IB_MANDATORY_FUNC(dealloc_pd),
  74. IB_MANDATORY_FUNC(create_ah),
  75. IB_MANDATORY_FUNC(destroy_ah),
  76. IB_MANDATORY_FUNC(create_qp),
  77. IB_MANDATORY_FUNC(modify_qp),
  78. IB_MANDATORY_FUNC(destroy_qp),
  79. IB_MANDATORY_FUNC(post_send),
  80. IB_MANDATORY_FUNC(post_recv),
  81. IB_MANDATORY_FUNC(create_cq),
  82. IB_MANDATORY_FUNC(destroy_cq),
  83. IB_MANDATORY_FUNC(poll_cq),
  84. IB_MANDATORY_FUNC(req_notify_cq),
  85. IB_MANDATORY_FUNC(get_dma_mr),
  86. IB_MANDATORY_FUNC(dereg_mr)
  87. };
  88. int i;
  89. for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
  90. if (!*(void **) ((void *) device + mandatory_table[i].offset)) {
  91. printk(KERN_WARNING "Device %s is missing mandatory function %s\n",
  92. device->name, mandatory_table[i].name);
  93. return -EINVAL;
  94. }
  95. }
  96. return 0;
  97. }
  98. static struct ib_device *__ib_device_get_by_name(const char *name)
  99. {
  100. struct ib_device *device;
  101. list_for_each_entry(device, &device_list, core_list)
  102. if (!strncmp(name, device->name, IB_DEVICE_NAME_MAX))
  103. return device;
  104. return NULL;
  105. }
  106. static int alloc_name(char *name)
  107. {
  108. long *inuse;
  109. char buf[IB_DEVICE_NAME_MAX];
  110. struct ib_device *device;
  111. int i;
  112. inuse = (long *) get_zeroed_page(GFP_KERNEL);
  113. if (!inuse)
  114. return -ENOMEM;
  115. list_for_each_entry(device, &device_list, core_list) {
  116. if (!sscanf(device->name, name, &i))
  117. continue;
  118. if (i < 0 || i >= PAGE_SIZE * 8)
  119. continue;
  120. snprintf(buf, sizeof buf, name, i);
  121. if (!strncmp(buf, device->name, IB_DEVICE_NAME_MAX))
  122. set_bit(i, inuse);
  123. }
  124. i = find_first_zero_bit(inuse, PAGE_SIZE * 8);
  125. free_page((unsigned long) inuse);
  126. snprintf(buf, sizeof buf, name, i);
  127. if (__ib_device_get_by_name(buf))
  128. return -ENFILE;
  129. strlcpy(name, buf, IB_DEVICE_NAME_MAX);
  130. return 0;
  131. }
  132. /**
  133. * ib_alloc_device - allocate an IB device struct
  134. * @size:size of structure to allocate
  135. *
  136. * Low-level drivers should use ib_alloc_device() to allocate &struct
  137. * ib_device. @size is the size of the structure to be allocated,
  138. * including any private data used by the low-level driver.
  139. * ib_dealloc_device() must be used to free structures allocated with
  140. * ib_alloc_device().
  141. */
  142. struct ib_device *ib_alloc_device(size_t size)
  143. {
  144. BUG_ON(size < sizeof (struct ib_device));
  145. return kzalloc(size, GFP_KERNEL);
  146. }
  147. EXPORT_SYMBOL(ib_alloc_device);
  148. /**
  149. * ib_dealloc_device - free an IB device struct
  150. * @device:structure to free
  151. *
  152. * Free a structure allocated with ib_alloc_device().
  153. */
  154. void ib_dealloc_device(struct ib_device *device)
  155. {
  156. if (device->reg_state == IB_DEV_UNINITIALIZED) {
  157. kfree(device);
  158. return;
  159. }
  160. BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
  161. ib_device_unregister_sysfs(device);
  162. }
  163. EXPORT_SYMBOL(ib_dealloc_device);
  164. static int add_client_context(struct ib_device *device, struct ib_client *client)
  165. {
  166. struct ib_client_data *context;
  167. unsigned long flags;
  168. context = kmalloc(sizeof *context, GFP_KERNEL);
  169. if (!context) {
  170. printk(KERN_WARNING "Couldn't allocate client context for %s/%s\n",
  171. device->name, client->name);
  172. return -ENOMEM;
  173. }
  174. context->client = client;
  175. context->data = NULL;
  176. spin_lock_irqsave(&device->client_data_lock, flags);
  177. list_add(&context->list, &device->client_data_list);
  178. spin_unlock_irqrestore(&device->client_data_lock, flags);
  179. return 0;
  180. }
  181. /**
  182. * ib_register_device - Register an IB device with IB core
  183. * @device:Device to register
  184. *
  185. * Low-level drivers use ib_register_device() to register their
  186. * devices with the IB core. All registered clients will receive a
  187. * callback for each device that is added. @device must be allocated
  188. * with ib_alloc_device().
  189. */
  190. int ib_register_device(struct ib_device *device)
  191. {
  192. int ret;
  193. mutex_lock(&device_mutex);
  194. if (strchr(device->name, '%')) {
  195. ret = alloc_name(device->name);
  196. if (ret)
  197. goto out;
  198. }
  199. if (ib_device_check_mandatory(device)) {
  200. ret = -EINVAL;
  201. goto out;
  202. }
  203. INIT_LIST_HEAD(&device->event_handler_list);
  204. INIT_LIST_HEAD(&device->client_data_list);
  205. spin_lock_init(&device->event_handler_lock);
  206. spin_lock_init(&device->client_data_lock);
  207. ret = ib_device_register_sysfs(device);
  208. if (ret) {
  209. printk(KERN_WARNING "Couldn't register device %s with driver model\n",
  210. device->name);
  211. goto out;
  212. }
  213. list_add_tail(&device->core_list, &device_list);
  214. device->reg_state = IB_DEV_REGISTERED;
  215. {
  216. struct ib_client *client;
  217. list_for_each_entry(client, &client_list, list)
  218. if (client->add && !add_client_context(device, client))
  219. client->add(device);
  220. }
  221. out:
  222. mutex_unlock(&device_mutex);
  223. return ret;
  224. }
  225. EXPORT_SYMBOL(ib_register_device);
  226. /**
  227. * ib_unregister_device - Unregister an IB device
  228. * @device:Device to unregister
  229. *
  230. * Unregister an IB device. All clients will receive a remove callback.
  231. */
  232. void ib_unregister_device(struct ib_device *device)
  233. {
  234. struct ib_client *client;
  235. struct ib_client_data *context, *tmp;
  236. unsigned long flags;
  237. mutex_lock(&device_mutex);
  238. list_for_each_entry_reverse(client, &client_list, list)
  239. if (client->remove)
  240. client->remove(device);
  241. list_del(&device->core_list);
  242. mutex_unlock(&device_mutex);
  243. spin_lock_irqsave(&device->client_data_lock, flags);
  244. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  245. kfree(context);
  246. spin_unlock_irqrestore(&device->client_data_lock, flags);
  247. device->reg_state = IB_DEV_UNREGISTERED;
  248. }
  249. EXPORT_SYMBOL(ib_unregister_device);
  250. /**
  251. * ib_register_client - Register an IB client
  252. * @client:Client to register
  253. *
  254. * Upper level users of the IB drivers can use ib_register_client() to
  255. * register callbacks for IB device addition and removal. When an IB
  256. * device is added, each registered client's add method will be called
  257. * (in the order the clients were registered), and when a device is
  258. * removed, each client's remove method will be called (in the reverse
  259. * order that clients were registered). In addition, when
  260. * ib_register_client() is called, the client will receive an add
  261. * callback for all devices already registered.
  262. */
  263. int ib_register_client(struct ib_client *client)
  264. {
  265. struct ib_device *device;
  266. mutex_lock(&device_mutex);
  267. list_add_tail(&client->list, &client_list);
  268. list_for_each_entry(device, &device_list, core_list)
  269. if (client->add && !add_client_context(device, client))
  270. client->add(device);
  271. mutex_unlock(&device_mutex);
  272. return 0;
  273. }
  274. EXPORT_SYMBOL(ib_register_client);
  275. /**
  276. * ib_unregister_client - Unregister an IB client
  277. * @client:Client to unregister
  278. *
  279. * Upper level users use ib_unregister_client() to remove their client
  280. * registration. When ib_unregister_client() is called, the client
  281. * will receive a remove callback for each IB device still registered.
  282. */
  283. void ib_unregister_client(struct ib_client *client)
  284. {
  285. struct ib_client_data *context, *tmp;
  286. struct ib_device *device;
  287. unsigned long flags;
  288. mutex_lock(&device_mutex);
  289. list_for_each_entry(device, &device_list, core_list) {
  290. if (client->remove)
  291. client->remove(device);
  292. spin_lock_irqsave(&device->client_data_lock, flags);
  293. list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
  294. if (context->client == client) {
  295. list_del(&context->list);
  296. kfree(context);
  297. }
  298. spin_unlock_irqrestore(&device->client_data_lock, flags);
  299. }
  300. list_del(&client->list);
  301. mutex_unlock(&device_mutex);
  302. }
  303. EXPORT_SYMBOL(ib_unregister_client);
  304. /**
  305. * ib_get_client_data - Get IB client context
  306. * @device:Device to get context for
  307. * @client:Client to get context for
  308. *
  309. * ib_get_client_data() returns client context set with
  310. * ib_set_client_data().
  311. */
  312. void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
  313. {
  314. struct ib_client_data *context;
  315. void *ret = NULL;
  316. unsigned long flags;
  317. spin_lock_irqsave(&device->client_data_lock, flags);
  318. list_for_each_entry(context, &device->client_data_list, list)
  319. if (context->client == client) {
  320. ret = context->data;
  321. break;
  322. }
  323. spin_unlock_irqrestore(&device->client_data_lock, flags);
  324. return ret;
  325. }
  326. EXPORT_SYMBOL(ib_get_client_data);
  327. /**
  328. * ib_set_client_data - Set IB client context
  329. * @device:Device to set context for
  330. * @client:Client to set context for
  331. * @data:Context to set
  332. *
  333. * ib_set_client_data() sets client context that can be retrieved with
  334. * ib_get_client_data().
  335. */
  336. void ib_set_client_data(struct ib_device *device, struct ib_client *client,
  337. void *data)
  338. {
  339. struct ib_client_data *context;
  340. unsigned long flags;
  341. spin_lock_irqsave(&device->client_data_lock, flags);
  342. list_for_each_entry(context, &device->client_data_list, list)
  343. if (context->client == client) {
  344. context->data = data;
  345. goto out;
  346. }
  347. printk(KERN_WARNING "No client context found for %s/%s\n",
  348. device->name, client->name);
  349. out:
  350. spin_unlock_irqrestore(&device->client_data_lock, flags);
  351. }
  352. EXPORT_SYMBOL(ib_set_client_data);
  353. /**
  354. * ib_register_event_handler - Register an IB event handler
  355. * @event_handler:Handler to register
  356. *
  357. * ib_register_event_handler() registers an event handler that will be
  358. * called back when asynchronous IB events occur (as defined in
  359. * chapter 11 of the InfiniBand Architecture Specification). This
  360. * callback may occur in interrupt context.
  361. */
  362. int ib_register_event_handler (struct ib_event_handler *event_handler)
  363. {
  364. unsigned long flags;
  365. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  366. list_add_tail(&event_handler->list,
  367. &event_handler->device->event_handler_list);
  368. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  369. return 0;
  370. }
  371. EXPORT_SYMBOL(ib_register_event_handler);
  372. /**
  373. * ib_unregister_event_handler - Unregister an event handler
  374. * @event_handler:Handler to unregister
  375. *
  376. * Unregister an event handler registered with
  377. * ib_register_event_handler().
  378. */
  379. int ib_unregister_event_handler(struct ib_event_handler *event_handler)
  380. {
  381. unsigned long flags;
  382. spin_lock_irqsave(&event_handler->device->event_handler_lock, flags);
  383. list_del(&event_handler->list);
  384. spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags);
  385. return 0;
  386. }
  387. EXPORT_SYMBOL(ib_unregister_event_handler);
  388. /**
  389. * ib_dispatch_event - Dispatch an asynchronous event
  390. * @event:Event to dispatch
  391. *
  392. * Low-level drivers must call ib_dispatch_event() to dispatch the
  393. * event to all registered event handlers when an asynchronous event
  394. * occurs.
  395. */
  396. void ib_dispatch_event(struct ib_event *event)
  397. {
  398. unsigned long flags;
  399. struct ib_event_handler *handler;
  400. spin_lock_irqsave(&event->device->event_handler_lock, flags);
  401. list_for_each_entry(handler, &event->device->event_handler_list, list)
  402. handler->handler(handler, event);
  403. spin_unlock_irqrestore(&event->device->event_handler_lock, flags);
  404. }
  405. EXPORT_SYMBOL(ib_dispatch_event);
  406. /**
  407. * ib_query_device - Query IB device attributes
  408. * @device:Device to query
  409. * @device_attr:Device attributes
  410. *
  411. * ib_query_device() returns the attributes of a device through the
  412. * @device_attr pointer.
  413. */
  414. int ib_query_device(struct ib_device *device,
  415. struct ib_device_attr *device_attr)
  416. {
  417. return device->query_device(device, device_attr);
  418. }
  419. EXPORT_SYMBOL(ib_query_device);
  420. /**
  421. * ib_query_port - Query IB port attributes
  422. * @device:Device to query
  423. * @port_num:Port number to query
  424. * @port_attr:Port attributes
  425. *
  426. * ib_query_port() returns the attributes of a port through the
  427. * @port_attr pointer.
  428. */
  429. int ib_query_port(struct ib_device *device,
  430. u8 port_num,
  431. struct ib_port_attr *port_attr)
  432. {
  433. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  434. if (port_num)
  435. return -EINVAL;
  436. } else if (port_num < 1 || port_num > device->phys_port_cnt)
  437. return -EINVAL;
  438. return device->query_port(device, port_num, port_attr);
  439. }
  440. EXPORT_SYMBOL(ib_query_port);
  441. /**
  442. * ib_query_gid - Get GID table entry
  443. * @device:Device to query
  444. * @port_num:Port number to query
  445. * @index:GID table index to query
  446. * @gid:Returned GID
  447. *
  448. * ib_query_gid() fetches the specified GID table entry.
  449. */
  450. int ib_query_gid(struct ib_device *device,
  451. u8 port_num, int index, union ib_gid *gid)
  452. {
  453. return device->query_gid(device, port_num, index, gid);
  454. }
  455. EXPORT_SYMBOL(ib_query_gid);
  456. /**
  457. * ib_query_pkey - Get P_Key table entry
  458. * @device:Device to query
  459. * @port_num:Port number to query
  460. * @index:P_Key table index to query
  461. * @pkey:Returned P_Key
  462. *
  463. * ib_query_pkey() fetches the specified P_Key table entry.
  464. */
  465. int ib_query_pkey(struct ib_device *device,
  466. u8 port_num, u16 index, u16 *pkey)
  467. {
  468. return device->query_pkey(device, port_num, index, pkey);
  469. }
  470. EXPORT_SYMBOL(ib_query_pkey);
  471. /**
  472. * ib_modify_device - Change IB device attributes
  473. * @device:Device to modify
  474. * @device_modify_mask:Mask of attributes to change
  475. * @device_modify:New attribute values
  476. *
  477. * ib_modify_device() changes a device's attributes as specified by
  478. * the @device_modify_mask and @device_modify structure.
  479. */
  480. int ib_modify_device(struct ib_device *device,
  481. int device_modify_mask,
  482. struct ib_device_modify *device_modify)
  483. {
  484. return device->modify_device(device, device_modify_mask,
  485. device_modify);
  486. }
  487. EXPORT_SYMBOL(ib_modify_device);
  488. /**
  489. * ib_modify_port - Modifies the attributes for the specified port.
  490. * @device: The device to modify.
  491. * @port_num: The number of the port to modify.
  492. * @port_modify_mask: Mask used to specify which attributes of the port
  493. * to change.
  494. * @port_modify: New attribute values for the port.
  495. *
  496. * ib_modify_port() changes a port's attributes as specified by the
  497. * @port_modify_mask and @port_modify structure.
  498. */
  499. int ib_modify_port(struct ib_device *device,
  500. u8 port_num, int port_modify_mask,
  501. struct ib_port_modify *port_modify)
  502. {
  503. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  504. if (port_num)
  505. return -EINVAL;
  506. } else if (port_num < 1 || port_num > device->phys_port_cnt)
  507. return -EINVAL;
  508. return device->modify_port(device, port_num, port_modify_mask,
  509. port_modify);
  510. }
  511. EXPORT_SYMBOL(ib_modify_port);
  512. static int __init ib_core_init(void)
  513. {
  514. int ret;
  515. ret = ib_sysfs_setup();
  516. if (ret)
  517. printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
  518. ret = ib_cache_setup();
  519. if (ret) {
  520. printk(KERN_WARNING "Couldn't set up InfiniBand P_Key/GID cache\n");
  521. ib_sysfs_cleanup();
  522. }
  523. return ret;
  524. }
  525. static void __exit ib_core_cleanup(void)
  526. {
  527. ib_cache_cleanup();
  528. ib_sysfs_cleanup();
  529. /* Make sure that any pending umem accounting work is done. */
  530. flush_scheduled_work();
  531. }
  532. module_init(ib_core_init);
  533. module_exit(ib_core_cleanup);