mthca_memfree.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #ifndef MTHCA_MEMFREE_H
  35. #define MTHCA_MEMFREE_H
  36. #include <linux/list.h>
  37. #include <linux/mutex.h>
  38. #define MTHCA_ICM_CHUNK_LEN \
  39. ((256 - sizeof (struct list_head) - 2 * sizeof (int)) / \
  40. (sizeof (struct scatterlist)))
  41. enum {
  42. MTHCA_ICM_PAGE_SHIFT = 12,
  43. MTHCA_ICM_PAGE_SIZE = 1 << MTHCA_ICM_PAGE_SHIFT,
  44. MTHCA_DB_REC_PER_PAGE = MTHCA_ICM_PAGE_SIZE / 8
  45. };
  46. struct mthca_icm_chunk {
  47. struct list_head list;
  48. int npages;
  49. int nsg;
  50. struct scatterlist mem[MTHCA_ICM_CHUNK_LEN];
  51. };
  52. struct mthca_icm {
  53. struct list_head chunk_list;
  54. int refcount;
  55. };
  56. struct mthca_icm_table {
  57. u64 virt;
  58. int num_icm;
  59. int num_obj;
  60. int obj_size;
  61. int lowmem;
  62. int coherent;
  63. struct mutex mutex;
  64. struct mthca_icm *icm[0];
  65. };
  66. struct mthca_icm_iter {
  67. struct mthca_icm *icm;
  68. struct mthca_icm_chunk *chunk;
  69. int page_idx;
  70. };
  71. struct mthca_dev;
  72. struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
  73. gfp_t gfp_mask, int coherent);
  74. void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm, int coherent);
  75. struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
  76. u64 virt, int obj_size,
  77. int nobj, int reserved,
  78. int use_lowmem, int use_coherent);
  79. void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table);
  80. int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);
  81. void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);
  82. void *mthca_table_find(struct mthca_icm_table *table, int obj, dma_addr_t *dma_handle);
  83. int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,
  84. int start, int end);
  85. void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,
  86. int start, int end);
  87. static inline void mthca_icm_first(struct mthca_icm *icm,
  88. struct mthca_icm_iter *iter)
  89. {
  90. iter->icm = icm;
  91. iter->chunk = list_empty(&icm->chunk_list) ?
  92. NULL : list_entry(icm->chunk_list.next,
  93. struct mthca_icm_chunk, list);
  94. iter->page_idx = 0;
  95. }
  96. static inline int mthca_icm_last(struct mthca_icm_iter *iter)
  97. {
  98. return !iter->chunk;
  99. }
  100. static inline void mthca_icm_next(struct mthca_icm_iter *iter)
  101. {
  102. if (++iter->page_idx >= iter->chunk->nsg) {
  103. if (iter->chunk->list.next == &iter->icm->chunk_list) {
  104. iter->chunk = NULL;
  105. return;
  106. }
  107. iter->chunk = list_entry(iter->chunk->list.next,
  108. struct mthca_icm_chunk, list);
  109. iter->page_idx = 0;
  110. }
  111. }
  112. static inline dma_addr_t mthca_icm_addr(struct mthca_icm_iter *iter)
  113. {
  114. return sg_dma_address(&iter->chunk->mem[iter->page_idx]);
  115. }
  116. static inline unsigned long mthca_icm_size(struct mthca_icm_iter *iter)
  117. {
  118. return sg_dma_len(&iter->chunk->mem[iter->page_idx]);
  119. }
  120. struct mthca_db_page {
  121. DECLARE_BITMAP(used, MTHCA_DB_REC_PER_PAGE);
  122. __be64 *db_rec;
  123. dma_addr_t mapping;
  124. };
  125. struct mthca_db_table {
  126. int npages;
  127. int max_group1;
  128. int min_group2;
  129. struct mthca_db_page *page;
  130. struct mutex mutex;
  131. };
  132. enum mthca_db_type {
  133. MTHCA_DB_TYPE_INVALID = 0x0,
  134. MTHCA_DB_TYPE_CQ_SET_CI = 0x1,
  135. MTHCA_DB_TYPE_CQ_ARM = 0x2,
  136. MTHCA_DB_TYPE_SQ = 0x3,
  137. MTHCA_DB_TYPE_RQ = 0x4,
  138. MTHCA_DB_TYPE_SRQ = 0x5,
  139. MTHCA_DB_TYPE_GROUP_SEP = 0x7
  140. };
  141. struct mthca_user_db_table;
  142. struct mthca_uar;
  143. int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
  144. struct mthca_user_db_table *db_tab, int index, u64 uaddr);
  145. void mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
  146. struct mthca_user_db_table *db_tab, int index);
  147. struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev);
  148. void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,
  149. struct mthca_user_db_table *db_tab);
  150. int mthca_init_db_tab(struct mthca_dev *dev);
  151. void mthca_cleanup_db_tab(struct mthca_dev *dev);
  152. int mthca_alloc_db(struct mthca_dev *dev, enum mthca_db_type type,
  153. u32 qn, __be32 **db);
  154. void mthca_free_db(struct mthca_dev *dev, int type, int db_index);
  155. #endif /* MTHCA_MEMFREE_H */