hns_roce_pd.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2016 Hisilicon Limited.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/platform_device.h>
  33. #include "hns_roce_device.h"
  34. static int hns_roce_pd_alloc(struct hns_roce_dev *hr_dev, unsigned long *pdn)
  35. {
  36. return hns_roce_bitmap_alloc(&hr_dev->pd_bitmap, pdn);
  37. }
  38. static void hns_roce_pd_free(struct hns_roce_dev *hr_dev, unsigned long pdn)
  39. {
  40. hns_roce_bitmap_free(&hr_dev->pd_bitmap, pdn, BITMAP_NO_RR);
  41. }
  42. int hns_roce_init_pd_table(struct hns_roce_dev *hr_dev)
  43. {
  44. return hns_roce_bitmap_init(&hr_dev->pd_bitmap, hr_dev->caps.num_pds,
  45. hr_dev->caps.num_pds - 1,
  46. hr_dev->caps.reserved_pds, 0);
  47. }
  48. void hns_roce_cleanup_pd_table(struct hns_roce_dev *hr_dev)
  49. {
  50. hns_roce_bitmap_cleanup(&hr_dev->pd_bitmap);
  51. }
  52. struct ib_pd *hns_roce_alloc_pd(struct ib_device *ib_dev,
  53. struct ib_ucontext *context,
  54. struct ib_udata *udata)
  55. {
  56. struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
  57. struct device *dev = &hr_dev->pdev->dev;
  58. struct hns_roce_pd *pd;
  59. int ret;
  60. pd = kmalloc(sizeof(*pd), GFP_KERNEL);
  61. if (!pd)
  62. return ERR_PTR(-ENOMEM);
  63. ret = hns_roce_pd_alloc(to_hr_dev(ib_dev), &pd->pdn);
  64. if (ret) {
  65. kfree(pd);
  66. dev_err(dev, "[alloc_pd]hns_roce_pd_alloc failed!\n");
  67. return ERR_PTR(ret);
  68. }
  69. if (context) {
  70. if (ib_copy_to_udata(udata, &pd->pdn, sizeof(u64))) {
  71. hns_roce_pd_free(to_hr_dev(ib_dev), pd->pdn);
  72. dev_err(dev, "[alloc_pd]ib_copy_to_udata failed!\n");
  73. kfree(pd);
  74. return ERR_PTR(-EFAULT);
  75. }
  76. }
  77. return &pd->ibpd;
  78. }
  79. int hns_roce_dealloc_pd(struct ib_pd *pd)
  80. {
  81. hns_roce_pd_free(to_hr_dev(pd->device), to_hr_pd(pd)->pdn);
  82. kfree(to_hr_pd(pd));
  83. return 0;
  84. }
  85. int hns_roce_uar_alloc(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
  86. {
  87. struct resource *res;
  88. int ret = 0;
  89. /* Using bitmap to manager UAR index */
  90. ret = hns_roce_bitmap_alloc(&hr_dev->uar_table.bitmap, &uar->index);
  91. if (ret == -1)
  92. return -ENOMEM;
  93. if (uar->index > 0)
  94. uar->index = (uar->index - 1) %
  95. (hr_dev->caps.phy_num_uars - 1) + 1;
  96. res = platform_get_resource(hr_dev->pdev, IORESOURCE_MEM, 0);
  97. if (!res) {
  98. dev_err(&hr_dev->pdev->dev, "memory resource not found!\n");
  99. return -EINVAL;
  100. }
  101. uar->pfn = ((res->start) >> PAGE_SHIFT) + uar->index;
  102. return 0;
  103. }
  104. void hns_roce_uar_free(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
  105. {
  106. hns_roce_bitmap_free(&hr_dev->uar_table.bitmap, uar->index,
  107. BITMAP_NO_RR);
  108. }
  109. int hns_roce_init_uar_table(struct hns_roce_dev *hr_dev)
  110. {
  111. return hns_roce_bitmap_init(&hr_dev->uar_table.bitmap,
  112. hr_dev->caps.num_uars,
  113. hr_dev->caps.num_uars - 1,
  114. hr_dev->caps.reserved_uars, 0);
  115. }
  116. void hns_roce_cleanup_uar_table(struct hns_roce_dev *hr_dev)
  117. {
  118. hns_roce_bitmap_cleanup(&hr_dev->uar_table.bitmap);
  119. }