mr_pool.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016 HGST, a Western Digital Company.
  4. */
  5. #include <rdma/ib_verbs.h>
  6. #include <rdma/mr_pool.h>
  7. struct ib_mr *ib_mr_pool_get(struct ib_qp *qp, struct list_head *list)
  8. {
  9. struct ib_mr *mr;
  10. unsigned long flags;
  11. spin_lock_irqsave(&qp->mr_lock, flags);
  12. mr = list_first_entry_or_null(list, struct ib_mr, qp_entry);
  13. if (mr) {
  14. list_del(&mr->qp_entry);
  15. qp->mrs_used++;
  16. }
  17. spin_unlock_irqrestore(&qp->mr_lock, flags);
  18. return mr;
  19. }
  20. EXPORT_SYMBOL(ib_mr_pool_get);
  21. void ib_mr_pool_put(struct ib_qp *qp, struct list_head *list, struct ib_mr *mr)
  22. {
  23. unsigned long flags;
  24. spin_lock_irqsave(&qp->mr_lock, flags);
  25. list_add(&mr->qp_entry, list);
  26. qp->mrs_used--;
  27. spin_unlock_irqrestore(&qp->mr_lock, flags);
  28. }
  29. EXPORT_SYMBOL(ib_mr_pool_put);
  30. int ib_mr_pool_init(struct ib_qp *qp, struct list_head *list, int nr,
  31. enum ib_mr_type type, u32 max_num_sg, u32 max_num_meta_sg)
  32. {
  33. struct ib_mr *mr;
  34. unsigned long flags;
  35. int ret, i;
  36. for (i = 0; i < nr; i++) {
  37. if (type == IB_MR_TYPE_INTEGRITY)
  38. mr = ib_alloc_mr_integrity(qp->pd, max_num_sg,
  39. max_num_meta_sg);
  40. else
  41. mr = ib_alloc_mr(qp->pd, type, max_num_sg);
  42. if (IS_ERR(mr)) {
  43. ret = PTR_ERR(mr);
  44. goto out;
  45. }
  46. spin_lock_irqsave(&qp->mr_lock, flags);
  47. list_add_tail(&mr->qp_entry, list);
  48. spin_unlock_irqrestore(&qp->mr_lock, flags);
  49. }
  50. return 0;
  51. out:
  52. ib_mr_pool_destroy(qp, list);
  53. return ret;
  54. }
  55. EXPORT_SYMBOL(ib_mr_pool_init);
  56. void ib_mr_pool_destroy(struct ib_qp *qp, struct list_head *list)
  57. {
  58. struct ib_mr *mr;
  59. unsigned long flags;
  60. spin_lock_irqsave(&qp->mr_lock, flags);
  61. while (!list_empty(list)) {
  62. mr = list_first_entry(list, struct ib_mr, qp_entry);
  63. list_del(&mr->qp_entry);
  64. spin_unlock_irqrestore(&qp->mr_lock, flags);
  65. ib_dereg_mr(mr);
  66. spin_lock_irqsave(&qp->mr_lock, flags);
  67. }
  68. spin_unlock_irqrestore(&qp->mr_lock, flags);
  69. }
  70. EXPORT_SYMBOL(ib_mr_pool_destroy);