This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / infiniband / hw / mthca / mthca_memfree.c
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  * $Id$
33  */
34
35 #include "mthca_memfree.h"
36 #include "mthca_dev.h"
37 #include "mthca_cmd.h"
38
39 /*
40  * We allocate in as big chunks as we can, up to a maximum of 256 KB
41  * per chunk.
42  */
43 enum {
44         MTHCA_ICM_ALLOC_SIZE   = 1 << 18,
45         MTHCA_TABLE_CHUNK_SIZE = 1 << 18
46 };
47
48 void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm)
49 {
50         struct mthca_icm_chunk *chunk, *tmp;
51         int i;
52
53         if (!icm)
54                 return;
55
56         list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
57                 if (chunk->nsg > 0)
58                         pci_unmap_sg(dev->pdev, chunk->mem, chunk->npages,
59                                      PCI_DMA_BIDIRECTIONAL);
60
61                 for (i = 0; i < chunk->npages; ++i)
62                         __free_pages(chunk->mem[i].page,
63                                      get_order(chunk->mem[i].length));
64
65                 kfree(chunk);
66         }
67
68         kfree(icm);
69 }
70
71 struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
72                                   unsigned int gfp_mask)
73 {
74         struct mthca_icm *icm;
75         struct mthca_icm_chunk *chunk = NULL;
76         int cur_order;
77
78         icm = kmalloc(sizeof *icm, gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
79         if (!icm)
80                 return icm;
81
82         INIT_LIST_HEAD(&icm->chunk_list);
83
84         cur_order = get_order(MTHCA_ICM_ALLOC_SIZE);
85
86         while (npages > 0) {
87                 if (!chunk) {
88                         chunk = kmalloc(sizeof *chunk,
89                                         gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
90                         if (!chunk)
91                                 goto fail;
92
93                         chunk->npages = 0;
94                         chunk->nsg    = 0;
95                         list_add_tail(&chunk->list, &icm->chunk_list);
96                 }
97
98                 while (1 << cur_order > npages)
99                         --cur_order;
100
101                 chunk->mem[chunk->npages].page = alloc_pages(gfp_mask, cur_order);
102                 if (chunk->mem[chunk->npages].page) {
103                         chunk->mem[chunk->npages].length = PAGE_SIZE << cur_order;
104                         chunk->mem[chunk->npages].offset = 0;
105
106                         if (++chunk->npages == MTHCA_ICM_CHUNK_LEN) {
107                                 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
108                                                         chunk->npages,
109                                                         PCI_DMA_BIDIRECTIONAL);
110
111                                 if (chunk->nsg <= 0)
112                                         goto fail;
113
114                                 chunk = NULL;
115                         }
116
117                         npages -= 1 << cur_order;
118                 } else {
119                         --cur_order;
120                         if (cur_order < 0)
121                                 goto fail;
122                 }
123         }
124
125         if (chunk) {
126                 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
127                                         chunk->npages,
128                                         PCI_DMA_BIDIRECTIONAL);
129
130                 if (chunk->nsg <= 0)
131                         goto fail;
132         }
133
134         return icm;
135
136 fail:
137         mthca_free_icm(dev, icm);
138         return NULL;
139 }
140
141 struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
142                                               u64 virt, unsigned size,
143                                               unsigned reserved,
144                                               int use_lowmem)
145 {
146         struct mthca_icm_table *table;
147         int num_icm;
148         int i;
149         u8 status;
150
151         num_icm = size / MTHCA_TABLE_CHUNK_SIZE;
152
153         table = kmalloc(sizeof *table + num_icm * sizeof *table->icm, GFP_KERNEL);
154         if (!table)
155                 return NULL;
156
157         table->virt    = virt;
158         table->num_icm = num_icm;
159         init_MUTEX(&table->sem);
160
161         for (i = 0; i < num_icm; ++i)
162                 table->icm[i] = NULL;
163
164         for (i = 0; i < (reserved + MTHCA_TABLE_CHUNK_SIZE - 1) / MTHCA_TABLE_CHUNK_SIZE; ++i) {
165                 table->icm[i] = mthca_alloc_icm(dev, MTHCA_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
166                                                 (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
167                                                 __GFP_NOWARN);
168                 if (!table->icm[i])
169                         goto err;
170                 if (mthca_MAP_ICM(dev, table->icm[i], virt + i * MTHCA_TABLE_CHUNK_SIZE,
171                                   &status) || status) {
172                         mthca_free_icm(dev, table->icm[i]);
173                         table->icm[i] = NULL;
174                         goto err;
175                 }
176         }
177
178         return table;
179
180 err:
181         for (i = 0; i < num_icm; ++i)
182                 if (table->icm[i]) {
183                         mthca_UNMAP_ICM(dev, virt + i * MTHCA_TABLE_CHUNK_SIZE,
184                                         MTHCA_TABLE_CHUNK_SIZE >> 12, &status);
185                         mthca_free_icm(dev, table->icm[i]);
186                 }
187
188         kfree(table);
189
190         return NULL;
191 }
192
193 void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table)
194 {
195         int i;
196         u8 status;
197
198         for (i = 0; i < table->num_icm; ++i)
199                 if (table->icm[i]) {
200                         mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
201                                         MTHCA_TABLE_CHUNK_SIZE >> 12, &status);
202                         mthca_free_icm(dev, table->icm[i]);
203                 }
204
205         kfree(table);
206 }