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