device.c 16 KB

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