mthca_memfree.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  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. * $Id$
  33. */
  34. #ifndef MTHCA_MEMFREE_H
  35. #define MTHCA_MEMFREE_H
  36. #include <linux/list.h>
  37. #include <linux/pci.h>
  38. #include <asm/semaphore.h>
  39. #define MTHCA_ICM_CHUNK_LEN \
  40. ((256 - sizeof (struct list_head) - 2 * sizeof (int)) / \
  41. (sizeof (struct scatterlist)))
  42. struct mthca_icm_chunk {
  43. struct list_head list;
  44. int npages;
  45. int nsg;
  46. struct scatterlist mem[MTHCA_ICM_CHUNK_LEN];
  47. };
  48. struct mthca_icm {
  49. struct list_head chunk_list;
  50. int refcount;
  51. };
  52. struct mthca_icm_table {
  53. u64 virt;
  54. int num_icm;
  55. int num_obj;
  56. int obj_size;
  57. int lowmem;
  58. struct semaphore mutex;
  59. struct mthca_icm *icm[0];
  60. };
  61. struct mthca_icm_iter {
  62. struct mthca_icm *icm;
  63. struct mthca_icm_chunk *chunk;
  64. int page_idx;
  65. };
  66. struct mthca_dev;
  67. struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
  68. unsigned int gfp_mask);
  69. void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm);
  70. struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
  71. u64 virt, int obj_size,
  72. int nobj, int reserved,
  73. int use_lowmem);
  74. void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table);
  75. int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);
  76. void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);
  77. void *mthca_table_find(struct mthca_icm_table *table, int obj);
  78. int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,
  79. int start, int end);
  80. void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,
  81. int start, int end);
  82. static inline void mthca_icm_first(struct mthca_icm *icm,
  83. struct mthca_icm_iter *iter)
  84. {
  85. iter->icm = icm;
  86. iter->chunk = list_empty(&icm->chunk_list) ?
  87. NULL : list_entry(icm->chunk_list.next,
  88. struct mthca_icm_chunk, list);
  89. iter->page_idx = 0;
  90. }
  91. static inline int mthca_icm_last(struct mthca_icm_iter *iter)
  92. {
  93. return !iter->chunk;
  94. }
  95. static inline void mthca_icm_next(struct mthca_icm_iter *iter)
  96. {
  97. if (++iter->page_idx >= iter->chunk->nsg) {
  98. if (iter->chunk->list.next == &iter->icm->chunk_list) {
  99. iter->chunk = NULL;
  100. return;
  101. }
  102. iter->chunk = list_entry(iter->chunk->list.next,
  103. struct mthca_icm_chunk, list);
  104. iter->page_idx = 0;
  105. }
  106. }
  107. static inline dma_addr_t mthca_icm_addr(struct mthca_icm_iter *iter)
  108. {
  109. return sg_dma_address(&iter->chunk->mem[iter->page_idx]);
  110. }
  111. static inline unsigned long mthca_icm_size(struct mthca_icm_iter *iter)
  112. {
  113. return sg_dma_len(&iter->chunk->mem[iter->page_idx]);
  114. }
  115. enum {
  116. MTHCA_DB_REC_PER_PAGE = 4096 / 8
  117. };
  118. struct mthca_db_page {
  119. DECLARE_BITMAP(used, MTHCA_DB_REC_PER_PAGE);
  120. u64 *db_rec;
  121. dma_addr_t mapping;
  122. };
  123. struct mthca_db_table {
  124. int npages;
  125. int max_group1;
  126. int min_group2;
  127. struct mthca_db_page *page;
  128. struct semaphore mutex;
  129. };
  130. enum {
  131. MTHCA_DB_TYPE_INVALID = 0x0,
  132. MTHCA_DB_TYPE_CQ_SET_CI = 0x1,
  133. MTHCA_DB_TYPE_CQ_ARM = 0x2,
  134. MTHCA_DB_TYPE_SQ = 0x3,
  135. MTHCA_DB_TYPE_RQ = 0x4,
  136. MTHCA_DB_TYPE_SRQ = 0x5,
  137. MTHCA_DB_TYPE_GROUP_SEP = 0x7
  138. };
  139. int mthca_init_db_tab(struct mthca_dev *dev);
  140. void mthca_cleanup_db_tab(struct mthca_dev *dev);
  141. int mthca_alloc_db(struct mthca_dev *dev, int type, u32 qn, u32 **db);
  142. void mthca_free_db(struct mthca_dev *dev, int type, int db_index);
  143. #endif /* MTHCA_MEMFREE_H */