1 /* 2 * Copyright (c) 2004 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: mthca_allocator.c 1349 2004-12-16 21:09:43Z roland $ 33 */ 34 35 #include <linux/errno.h> 36 #include <linux/slab.h> 37 #include <linux/bitmap.h> 38 39 #include "mthca_dev.h" 40 41 /* Trivial bitmap-based allocator */ 42 u32 mthca_alloc(struct mthca_alloc *alloc) 43 { 44 unsigned long flags; 45 u32 obj; 46 47 spin_lock_irqsave(&alloc->lock, flags); 48 49 obj = find_next_zero_bit(alloc->table, alloc->max, alloc->last); 50 if (obj >= alloc->max) { 51 alloc->top = (alloc->top + alloc->max) & alloc->mask; 52 obj = find_first_zero_bit(alloc->table, alloc->max); 53 } 54 55 if (obj < alloc->max) { 56 set_bit(obj, alloc->table); 57 obj |= alloc->top; 58 } else 59 obj = -1; 60 61 spin_unlock_irqrestore(&alloc->lock, flags); 62 63 return obj; 64 } 65 66 void mthca_free(struct mthca_alloc *alloc, u32 obj) 67 { 68 unsigned long flags; 69 70 obj &= alloc->max - 1; 71 72 spin_lock_irqsave(&alloc->lock, flags); 73 74 clear_bit(obj, alloc->table); 75 alloc->last = min(alloc->last, obj); 76 alloc->top = (alloc->top + alloc->max) & alloc->mask; 77 78 spin_unlock_irqrestore(&alloc->lock, flags); 79 } 80 81 int mthca_alloc_init(struct mthca_alloc *alloc, u32 num, u32 mask, 82 u32 reserved) 83 { 84 int i; 85 86 /* num must be a power of 2 */ 87 if (num != 1 << (ffs(num) - 1)) 88 return -EINVAL; 89 90 alloc->last = 0; 91 alloc->top = 0; 92 alloc->max = num; 93 alloc->mask = mask; 94 spin_lock_init(&alloc->lock); 95 alloc->table = kmalloc(BITS_TO_LONGS(num) * sizeof (long), 96 GFP_KERNEL); 97 if (!alloc->table) 98 return -ENOMEM; 99 100 bitmap_zero(alloc->table, num); 101 for (i = 0; i < reserved; ++i) 102 set_bit(i, alloc->table); 103 104 return 0; 105 } 106 107 void mthca_alloc_cleanup(struct mthca_alloc *alloc) 108 { 109 kfree(alloc->table); 110 } 111 112 /* 113 * Array of pointers with lazy allocation of leaf pages. Callers of 114 * _get, _set and _clear methods must use a lock or otherwise 115 * serialize access to the array. 116 */ 117 118 #define MTHCA_ARRAY_MASK (PAGE_SIZE / sizeof (void *) - 1) 119 120 void *mthca_array_get(struct mthca_array *array, int index) 121 { 122 int p = (index * sizeof (void *)) >> PAGE_SHIFT; 123 124 if (array->page_list[p].page) 125 return array->page_list[p].page[index & MTHCA_ARRAY_MASK]; 126 else 127 return NULL; 128 } 129 130 int mthca_array_set(struct mthca_array *array, int index, void *value) 131 { 132 int p = (index * sizeof (void *)) >> PAGE_SHIFT; 133 134 /* Allocate with GFP_ATOMIC because we'll be called with locks held. */ 135 if (!array->page_list[p].page) 136 array->page_list[p].page = (void **) get_zeroed_page(GFP_ATOMIC); 137 138 if (!array->page_list[p].page) 139 return -ENOMEM; 140 141 array->page_list[p].page[index & MTHCA_ARRAY_MASK] = value; 142 ++array->page_list[p].used; 143 144 return 0; 145 } 146 147 void mthca_array_clear(struct mthca_array *array, int index) 148 { 149 int p = (index * sizeof (void *)) >> PAGE_SHIFT; 150 151 if (--array->page_list[p].used == 0) { 152 free_page((unsigned long) array->page_list[p].page); 153 array->page_list[p].page = NULL; 154 } else 155 array->page_list[p].page[index & MTHCA_ARRAY_MASK] = NULL; 156 157 if (array->page_list[p].used < 0) 158 pr_debug("Array %p index %d page %d with ref count %d < 0\n", 159 array, index, p, array->page_list[p].used); 160 } 161 162 int mthca_array_init(struct mthca_array *array, int nent) 163 { 164 int npage = (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE; 165 int i; 166 167 array->page_list = kmalloc(npage * sizeof *array->page_list, GFP_KERNEL); 168 if (!array->page_list) 169 return -ENOMEM; 170 171 for (i = 0; i < npage; ++i) { 172 array->page_list[i].page = NULL; 173 array->page_list[i].used = 0; 174 } 175 176 return 0; 177 } 178 179 void mthca_array_cleanup(struct mthca_array *array, int nent) 180 { 181 int i; 182 183 for (i = 0; i < (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE; ++i) 184 free_page((unsigned long) array->page_list[i].page); 185 186 kfree(array->page_list); 187 } 188 189 /* 190 * Handling for queue buffers -- we allocate a bunch of memory and 191 * register it in a memory region at HCA virtual address 0. If the 192 * requested size is > max_direct, we split the allocation into 193 * multiple pages, so we don't require too much contiguous memory. 194 */ 195 196 int mthca_buf_alloc(struct mthca_dev *dev, int size, int max_direct, 197 union mthca_buf *buf, int *is_direct, struct mthca_pd *pd, 198 int hca_write, struct mthca_mr *mr) 199 { 200 int err = -ENOMEM; 201 int npages, shift; 202 u64 *dma_list = NULL; 203 dma_addr_t t; 204 int i; 205 206 if (size <= max_direct) { 207 *is_direct = 1; 208 npages = 1; 209 shift = get_order(size) + PAGE_SHIFT; 210 211 buf->direct.buf = dma_alloc_coherent(&dev->pdev->dev, 212 size, &t, GFP_KERNEL); 213 if (!buf->direct.buf) 214 return -ENOMEM; 215 216 pci_unmap_addr_set(&buf->direct, mapping, t); 217 218 memset(buf->direct.buf, 0, size); 219 220 while (t & ((1 << shift) - 1)) { 221 --shift; 222 npages *= 2; 223 } 224 225 dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL); 226 if (!dma_list) 227 goto err_free; 228 229 for (i = 0; i < npages; ++i) 230 dma_list[i] = t + i * (1 << shift); 231 } else { 232 *is_direct = 0; 233 npages = (size + PAGE_SIZE - 1) / PAGE_SIZE; 234 shift = PAGE_SHIFT; 235 236 dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL); 237 if (!dma_list) 238 return -ENOMEM; 239 240 buf->page_list = kmalloc(npages * sizeof *buf->page_list, 241 GFP_KERNEL); 242 if (!buf->page_list) 243 goto err_out; 244 245 for (i = 0; i < npages; ++i) 246 buf->page_list[i].buf = NULL; 247 248 for (i = 0; i < npages; ++i) { 249 buf->page_list[i].buf = 250 dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE, 251 &t, GFP_KERNEL); 252 if (!buf->page_list[i].buf) 253 goto err_free; 254 255 dma_list[i] = t; 256 pci_unmap_addr_set(&buf->page_list[i], mapping, t); 257 258 clear_page(buf->page_list[i].buf); 259 } 260 } 261 262 err = mthca_mr_alloc_phys(dev, pd->pd_num, 263 dma_list, shift, npages, 264 0, size, 265 MTHCA_MPT_FLAG_LOCAL_READ | 266 (hca_write ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0), 267 mr); 268 if (err) 269 goto err_free; 270 271 kfree(dma_list); 272 273 return 0; 274 275 err_free: 276 mthca_buf_free(dev, size, buf, *is_direct, NULL); 277 278 err_out: 279 kfree(dma_list); 280 281 return err; 282 } 283 284 void mthca_buf_free(struct mthca_dev *dev, int size, union mthca_buf *buf, 285 int is_direct, struct mthca_mr *mr) 286 { 287 int i; 288 289 if (mr) 290 mthca_free_mr(dev, mr); 291 292 if (is_direct) 293 dma_free_coherent(&dev->pdev->dev, size, buf->direct.buf, 294 pci_unmap_addr(&buf->direct, mapping)); 295 else { 296 for (i = 0; i < (size + PAGE_SIZE - 1) / PAGE_SIZE; ++i) 297 dma_free_coherent(&dev->pdev->dev, PAGE_SIZE, 298 buf->page_list[i].buf, 299 pci_unmap_addr(&buf->page_list[i], 300 mapping)); 301 kfree(buf->page_list); 302 } 303 } 304