c2_pd.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  5. * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/init.h>
  36. #include <linux/slab.h>
  37. #include <linux/errno.h>
  38. #include "c2.h"
  39. #include "c2_provider.h"
  40. int c2_pd_alloc(struct c2_dev *c2dev, int privileged, struct c2_pd *pd)
  41. {
  42. u32 obj;
  43. int ret = 0;
  44. spin_lock(&c2dev->pd_table.lock);
  45. obj = find_next_zero_bit(c2dev->pd_table.table, c2dev->pd_table.max,
  46. c2dev->pd_table.last);
  47. if (obj >= c2dev->pd_table.max)
  48. obj = find_first_zero_bit(c2dev->pd_table.table,
  49. c2dev->pd_table.max);
  50. if (obj < c2dev->pd_table.max) {
  51. pd->pd_id = obj;
  52. __set_bit(obj, c2dev->pd_table.table);
  53. c2dev->pd_table.last = obj+1;
  54. if (c2dev->pd_table.last >= c2dev->pd_table.max)
  55. c2dev->pd_table.last = 0;
  56. } else
  57. ret = -ENOMEM;
  58. spin_unlock(&c2dev->pd_table.lock);
  59. return ret;
  60. }
  61. void c2_pd_free(struct c2_dev *c2dev, struct c2_pd *pd)
  62. {
  63. spin_lock(&c2dev->pd_table.lock);
  64. __clear_bit(pd->pd_id, c2dev->pd_table.table);
  65. spin_unlock(&c2dev->pd_table.lock);
  66. }
  67. int __devinit c2_init_pd_table(struct c2_dev *c2dev)
  68. {
  69. c2dev->pd_table.last = 0;
  70. c2dev->pd_table.max = c2dev->props.max_pd;
  71. spin_lock_init(&c2dev->pd_table.lock);
  72. c2dev->pd_table.table = kmalloc(BITS_TO_LONGS(c2dev->props.max_pd) *
  73. sizeof(long), GFP_KERNEL);
  74. if (!c2dev->pd_table.table)
  75. return -ENOMEM;
  76. bitmap_zero(c2dev->pd_table.table, c2dev->props.max_pd);
  77. return 0;
  78. }
  79. void __devexit c2_cleanup_pd_table(struct c2_dev *c2dev)
  80. {
  81. kfree(c2dev->pd_table.table);
  82. }