mthca_memfree.h 5.4 KB

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