blob: 4761d844cb5f93bbacb9f960b34e70d8cad8402c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Roland Dreier56483ec2005-07-07 17:57:16 -07003 * Copyright (c) 2005 Cisco Systems. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
36#ifndef MTHCA_MEMFREE_H
37#define MTHCA_MEMFREE_H
38
39#include <linux/list.h>
40#include <linux/pci.h>
41
42#include <asm/semaphore.h>
43
44#define MTHCA_ICM_CHUNK_LEN \
45 ((256 - sizeof (struct list_head) - 2 * sizeof (int)) / \
46 (sizeof (struct scatterlist)))
47
48struct mthca_icm_chunk {
49 struct list_head list;
50 int npages;
51 int nsg;
52 struct scatterlist mem[MTHCA_ICM_CHUNK_LEN];
53};
54
55struct mthca_icm {
56 struct list_head chunk_list;
57 int refcount;
58};
59
60struct mthca_icm_table {
61 u64 virt;
62 int num_icm;
63 int num_obj;
64 int obj_size;
65 int lowmem;
66 struct semaphore mutex;
67 struct mthca_icm *icm[0];
68};
69
70struct mthca_icm_iter {
71 struct mthca_icm *icm;
72 struct mthca_icm_chunk *chunk;
73 int page_idx;
74};
75
76struct mthca_dev;
77
78struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
79 unsigned int gfp_mask);
80void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm);
81
82struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
83 u64 virt, int obj_size,
84 int nobj, int reserved,
85 int use_lowmem);
86void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table);
87int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);
88void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);
Michael S. Tsirkin0fabd9f2005-04-16 15:26:29 -070089void *mthca_table_find(struct mthca_icm_table *table, int obj);
Roland Dreier86562a12005-04-16 15:26:13 -070090int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,
91 int start, int end);
92void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,
93 int start, int end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95static inline void mthca_icm_first(struct mthca_icm *icm,
96 struct mthca_icm_iter *iter)
97{
98 iter->icm = icm;
99 iter->chunk = list_empty(&icm->chunk_list) ?
100 NULL : list_entry(icm->chunk_list.next,
101 struct mthca_icm_chunk, list);
102 iter->page_idx = 0;
103}
104
105static inline int mthca_icm_last(struct mthca_icm_iter *iter)
106{
107 return !iter->chunk;
108}
109
110static inline void mthca_icm_next(struct mthca_icm_iter *iter)
111{
112 if (++iter->page_idx >= iter->chunk->nsg) {
113 if (iter->chunk->list.next == &iter->icm->chunk_list) {
114 iter->chunk = NULL;
115 return;
116 }
117
118 iter->chunk = list_entry(iter->chunk->list.next,
119 struct mthca_icm_chunk, list);
120 iter->page_idx = 0;
121 }
122}
123
124static inline dma_addr_t mthca_icm_addr(struct mthca_icm_iter *iter)
125{
126 return sg_dma_address(&iter->chunk->mem[iter->page_idx]);
127}
128
129static inline unsigned long mthca_icm_size(struct mthca_icm_iter *iter)
130{
131 return sg_dma_len(&iter->chunk->mem[iter->page_idx]);
132}
133
134enum {
135 MTHCA_DB_REC_PER_PAGE = 4096 / 8
136};
137
138struct mthca_db_page {
139 DECLARE_BITMAP(used, MTHCA_DB_REC_PER_PAGE);
140 u64 *db_rec;
141 dma_addr_t mapping;
142};
143
144struct mthca_db_table {
145 int npages;
146 int max_group1;
147 int min_group2;
148 struct mthca_db_page *page;
149 struct semaphore mutex;
150};
151
Roland Dreier56483ec2005-07-07 17:57:16 -0700152enum mthca_db_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 MTHCA_DB_TYPE_INVALID = 0x0,
154 MTHCA_DB_TYPE_CQ_SET_CI = 0x1,
155 MTHCA_DB_TYPE_CQ_ARM = 0x2,
156 MTHCA_DB_TYPE_SQ = 0x3,
157 MTHCA_DB_TYPE_RQ = 0x4,
158 MTHCA_DB_TYPE_SRQ = 0x5,
159 MTHCA_DB_TYPE_GROUP_SEP = 0x7
160};
161
Roland Dreier56483ec2005-07-07 17:57:16 -0700162struct mthca_user_db_table;
163struct mthca_uar;
164
165int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
166 struct mthca_user_db_table *db_tab, int index, u64 uaddr);
167void mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
168 struct mthca_user_db_table *db_tab, int index);
169struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev);
170void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,
171 struct mthca_user_db_table *db_tab);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173int mthca_init_db_tab(struct mthca_dev *dev);
174void mthca_cleanup_db_tab(struct mthca_dev *dev);
175int mthca_alloc_db(struct mthca_dev *dev, int type, u32 qn, u32 **db);
176void mthca_free_db(struct mthca_dev *dev, int type, int db_index);
177
178#endif /* MTHCA_MEMFREE_H */