ehca_pd.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * IBM eServer eHCA Infiniband device driver for Linux on POWER
  3. *
  4. * PD functions
  5. *
  6. * Authors: Christoph Raisch <raisch@de.ibm.com>
  7. *
  8. * Copyright (c) 2005 IBM Corporation
  9. *
  10. * All rights reserved.
  11. *
  12. * This source code is distributed under a dual license of GPL v2.0 and OpenIB
  13. * BSD.
  14. *
  15. * OpenIB BSD License
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions are met:
  19. *
  20. * Redistributions of source code must retain the above copyright notice, this
  21. * list of conditions and the following disclaimer.
  22. *
  23. * Redistributions in binary form must reproduce the above copyright notice,
  24. * this list of conditions and the following disclaimer in the documentation
  25. * and/or other materials
  26. * provided with the distribution.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  35. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  36. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. */
  40. #include <linux/slab.h>
  41. #include "ehca_tools.h"
  42. #include "ehca_iverbs.h"
  43. static struct kmem_cache *pd_cache;
  44. struct ib_pd *ehca_alloc_pd(struct ib_device *device,
  45. struct ib_ucontext *context, struct ib_udata *udata)
  46. {
  47. struct ehca_pd *pd;
  48. int i;
  49. pd = kmem_cache_zalloc(pd_cache, GFP_KERNEL);
  50. if (!pd) {
  51. ehca_err(device, "device=%p context=%p out of memory",
  52. device, context);
  53. return ERR_PTR(-ENOMEM);
  54. }
  55. for (i = 0; i < 2; i++) {
  56. INIT_LIST_HEAD(&pd->free[i]);
  57. INIT_LIST_HEAD(&pd->full[i]);
  58. }
  59. mutex_init(&pd->lock);
  60. /*
  61. * Kernel PD: when device = -1, 0
  62. * User PD: when context != -1
  63. */
  64. if (!context) {
  65. /*
  66. * Kernel PDs after init reuses always
  67. * the one created in ehca_shca_reopen()
  68. */
  69. struct ehca_shca *shca = container_of(device, struct ehca_shca,
  70. ib_device);
  71. pd->fw_pd.value = shca->pd->fw_pd.value;
  72. } else
  73. pd->fw_pd.value = (u64)pd;
  74. return &pd->ib_pd;
  75. }
  76. int ehca_dealloc_pd(struct ib_pd *pd)
  77. {
  78. struct ehca_pd *my_pd = container_of(pd, struct ehca_pd, ib_pd);
  79. int i, leftovers = 0;
  80. struct ipz_small_queue_page *page, *tmp;
  81. for (i = 0; i < 2; i++) {
  82. list_splice(&my_pd->full[i], &my_pd->free[i]);
  83. list_for_each_entry_safe(page, tmp, &my_pd->free[i], list) {
  84. leftovers = 1;
  85. free_page(page->page);
  86. kmem_cache_free(small_qp_cache, page);
  87. }
  88. }
  89. if (leftovers)
  90. ehca_warn(pd->device,
  91. "Some small queue pages were not freed");
  92. kmem_cache_free(pd_cache, my_pd);
  93. return 0;
  94. }
  95. int ehca_init_pd_cache(void)
  96. {
  97. pd_cache = kmem_cache_create("ehca_cache_pd",
  98. sizeof(struct ehca_pd), 0,
  99. SLAB_HWCACHE_ALIGN,
  100. NULL);
  101. if (!pd_cache)
  102. return -ENOMEM;
  103. return 0;
  104. }
  105. void ehca_cleanup_pd_cache(void)
  106. {
  107. if (pd_cache)
  108. kmem_cache_destroy(pd_cache);
  109. }