1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#ifndef MTHCA_MEMFREE_H
38#define MTHCA_MEMFREE_H
39
40#include <linux/list.h>
41#include <linux/pci.h>
42#include <linux/mutex.h>
43
44#define MTHCA_ICM_CHUNK_LEN \
45 ((256 - sizeof (struct list_head) - 2 * sizeof (int)) / \
46 (sizeof (struct scatterlist)))
47
48enum {
49 MTHCA_ICM_PAGE_SHIFT = 12,
50 MTHCA_ICM_PAGE_SIZE = 1 << MTHCA_ICM_PAGE_SHIFT,
51 MTHCA_DB_REC_PER_PAGE = MTHCA_ICM_PAGE_SIZE / 8
52};
53
54struct mthca_icm_chunk {
55 struct list_head list;
56 int npages;
57 int nsg;
58 struct scatterlist mem[MTHCA_ICM_CHUNK_LEN];
59};
60
61struct mthca_icm {
62 struct list_head chunk_list;
63 int refcount;
64};
65
66struct mthca_icm_table {
67 u64 virt;
68 int num_icm;
69 int num_obj;
70 int obj_size;
71 int lowmem;
72 struct mutex mutex;
73 struct mthca_icm *icm[0];
74};
75
76struct mthca_icm_iter {
77 struct mthca_icm *icm;
78 struct mthca_icm_chunk *chunk;
79 int page_idx;
80};
81
82struct mthca_dev;
83
84struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
85 gfp_t gfp_mask);
86void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm);
87
88struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
89 u64 virt, int obj_size,
90 int nobj, int reserved,
91 int use_lowmem);
92void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table);
93int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);
94void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);
95void *mthca_table_find(struct mthca_icm_table *table, int obj);
96int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,
97 int start, int end);
98void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,
99 int start, int end);
100
101static inline void mthca_icm_first(struct mthca_icm *icm,
102 struct mthca_icm_iter *iter)
103{
104 iter->icm = icm;
105 iter->chunk = list_empty(&icm->chunk_list) ?
106 NULL : list_entry(icm->chunk_list.next,
107 struct mthca_icm_chunk, list);
108 iter->page_idx = 0;
109}
110
111static inline int mthca_icm_last(struct mthca_icm_iter *iter)
112{
113 return !iter->chunk;
114}
115
116static inline void mthca_icm_next(struct mthca_icm_iter *iter)
117{
118 if (++iter->page_idx >= iter->chunk->nsg) {
119 if (iter->chunk->list.next == &iter->icm->chunk_list) {
120 iter->chunk = NULL;
121 return;
122 }
123
124 iter->chunk = list_entry(iter->chunk->list.next,
125 struct mthca_icm_chunk, list);
126 iter->page_idx = 0;
127 }
128}
129
130static inline dma_addr_t mthca_icm_addr(struct mthca_icm_iter *iter)
131{
132 return sg_dma_address(&iter->chunk->mem[iter->page_idx]);
133}
134
135static inline unsigned long mthca_icm_size(struct mthca_icm_iter *iter)
136{
137 return sg_dma_len(&iter->chunk->mem[iter->page_idx]);
138}
139
140struct mthca_db_page {
141 DECLARE_BITMAP(used, MTHCA_DB_REC_PER_PAGE);
142 __be64 *db_rec;
143 dma_addr_t mapping;
144};
145
146struct mthca_db_table {
147 int npages;
148 int max_group1;
149 int min_group2;
150 struct mthca_db_page *page;
151 struct mutex mutex;
152};
153
154enum mthca_db_type {
155 MTHCA_DB_TYPE_INVALID = 0x0,
156 MTHCA_DB_TYPE_CQ_SET_CI = 0x1,
157 MTHCA_DB_TYPE_CQ_ARM = 0x2,
158 MTHCA_DB_TYPE_SQ = 0x3,
159 MTHCA_DB_TYPE_RQ = 0x4,
160 MTHCA_DB_TYPE_SRQ = 0x5,
161 MTHCA_DB_TYPE_GROUP_SEP = 0x7
162};
163
164struct mthca_user_db_table;
165struct mthca_uar;
166
167int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
168 struct mthca_user_db_table *db_tab, int index, u64 uaddr);
169void mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
170 struct mthca_user_db_table *db_tab, int index);
171struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev);
172void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,
173 struct mthca_user_db_table *db_tab);
174
175int mthca_init_db_tab(struct mthca_dev *dev);
176void mthca_cleanup_db_tab(struct mthca_dev *dev);
177int mthca_alloc_db(struct mthca_dev *dev, enum mthca_db_type type,
178 u32 qn, __be32 **db);
179void mthca_free_db(struct mthca_dev *dev, int type, int db_index);
180
181#endif
182