2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
35 #include "mthca_memfree.h"
36 #include "mthca_dev.h"
37 #include "mthca_cmd.h"
40 * We allocate in as big chunks as we can, up to a maximum of 256 KB
44 MTHCA_ICM_ALLOC_SIZE = 1 << 18,
45 MTHCA_TABLE_CHUNK_SIZE = 1 << 18
48 void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm)
50 struct mthca_icm_chunk *chunk, *tmp;
56 list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
58 pci_unmap_sg(dev->pdev, chunk->mem, chunk->npages,
59 PCI_DMA_BIDIRECTIONAL);
61 for (i = 0; i < chunk->npages; ++i)
62 __free_pages(chunk->mem[i].page,
63 get_order(chunk->mem[i].length));
71 struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
72 unsigned int gfp_mask)
74 struct mthca_icm *icm;
75 struct mthca_icm_chunk *chunk = NULL;
78 icm = kmalloc(sizeof *icm, gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
82 INIT_LIST_HEAD(&icm->chunk_list);
84 cur_order = get_order(MTHCA_ICM_ALLOC_SIZE);
88 chunk = kmalloc(sizeof *chunk,
89 gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
95 list_add_tail(&chunk->list, &icm->chunk_list);
98 while (1 << cur_order > npages)
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;
106 if (++chunk->npages == MTHCA_ICM_CHUNK_LEN) {
107 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
109 PCI_DMA_BIDIRECTIONAL);
117 npages -= 1 << cur_order;
126 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
128 PCI_DMA_BIDIRECTIONAL);
137 mthca_free_icm(dev, icm);
141 struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
142 u64 virt, unsigned size,
146 struct mthca_icm_table *table;
151 num_icm = size / MTHCA_TABLE_CHUNK_SIZE;
153 table = kmalloc(sizeof *table + num_icm * sizeof *table->icm, GFP_KERNEL);
158 table->num_icm = num_icm;
159 init_MUTEX(&table->sem);
161 for (i = 0; i < num_icm; ++i)
162 table->icm[i] = NULL;
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) |
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;
181 for (i = 0; i < num_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]);
193 void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table)
198 for (i = 0; i < table->num_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]);