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