mr_pool.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2016 HGST, a Western Digital Company.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. */
  13. #include <rdma/ib_verbs.h>
  14. #include <rdma/mr_pool.h>
  15. struct ib_mr *ib_mr_pool_get(struct ib_qp *qp, struct list_head *list)
  16. {
  17. struct ib_mr *mr;
  18. unsigned long flags;
  19. spin_lock_irqsave(&qp->mr_lock, flags);
  20. mr = list_first_entry_or_null(list, struct ib_mr, qp_entry);
  21. if (mr) {
  22. list_del(&mr->qp_entry);
  23. qp->mrs_used++;
  24. }
  25. spin_unlock_irqrestore(&qp->mr_lock, flags);
  26. return mr;
  27. }
  28. EXPORT_SYMBOL(ib_mr_pool_get);
  29. void ib_mr_pool_put(struct ib_qp *qp, struct list_head *list, struct ib_mr *mr)
  30. {
  31. unsigned long flags;
  32. spin_lock_irqsave(&qp->mr_lock, flags);
  33. list_add(&mr->qp_entry, list);
  34. qp->mrs_used--;
  35. spin_unlock_irqrestore(&qp->mr_lock, flags);
  36. }
  37. EXPORT_SYMBOL(ib_mr_pool_put);
  38. int ib_mr_pool_init(struct ib_qp *qp, struct list_head *list, int nr,
  39. enum ib_mr_type type, u32 max_num_sg)
  40. {
  41. struct ib_mr *mr;
  42. unsigned long flags;
  43. int ret, i;
  44. for (i = 0; i < nr; i++) {
  45. mr = ib_alloc_mr(qp->pd, type, max_num_sg);
  46. if (IS_ERR(mr)) {
  47. ret = PTR_ERR(mr);
  48. goto out;
  49. }
  50. spin_lock_irqsave(&qp->mr_lock, flags);
  51. list_add_tail(&mr->qp_entry, list);
  52. spin_unlock_irqrestore(&qp->mr_lock, flags);
  53. }
  54. return 0;
  55. out:
  56. ib_mr_pool_destroy(qp, list);
  57. return ret;
  58. }
  59. EXPORT_SYMBOL(ib_mr_pool_init);
  60. void ib_mr_pool_destroy(struct ib_qp *qp, struct list_head *list)
  61. {
  62. struct ib_mr *mr;
  63. unsigned long flags;
  64. spin_lock_irqsave(&qp->mr_lock, flags);
  65. while (!list_empty(list)) {
  66. mr = list_first_entry(list, struct ib_mr, qp_entry);
  67. list_del(&mr->qp_entry);
  68. spin_unlock_irqrestore(&qp->mr_lock, flags);
  69. ib_dereg_mr(mr);
  70. spin_lock_irqsave(&qp->mr_lock, flags);
  71. }
  72. spin_unlock_irqrestore(&qp->mr_lock, flags);
  73. }
  74. EXPORT_SYMBOL(ib_mr_pool_destroy);