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