Path: blob/master/drivers/infiniband/hw/mthca/mthca_allocator.c
15112 views
/*1* Copyright (c) 2004 Topspin Communications. All rights reserved.2*3* This software is available to you under a choice of one of two4* licenses. You may choose to be licensed under the terms of the GNU5* General Public License (GPL) Version 2, available from the file6* COPYING in the main directory of this source tree, or the7* OpenIB.org BSD license below:8*9* Redistribution and use in source and binary forms, with or10* without modification, are permitted provided that the following11* conditions are met:12*13* - Redistributions of source code must retain the above14* copyright notice, this list of conditions and the following15* disclaimer.16*17* - Redistributions in binary form must reproduce the above18* copyright notice, this list of conditions and the following19* disclaimer in the documentation and/or other materials20* provided with the distribution.21*22* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,23* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF24* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND25* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS26* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN27* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN28* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE29* SOFTWARE.30*/3132#include <linux/errno.h>33#include <linux/slab.h>34#include <linux/bitmap.h>3536#include "mthca_dev.h"3738/* Trivial bitmap-based allocator */39u32 mthca_alloc(struct mthca_alloc *alloc)40{41unsigned long flags;42u32 obj;4344spin_lock_irqsave(&alloc->lock, flags);4546obj = find_next_zero_bit(alloc->table, alloc->max, alloc->last);47if (obj >= alloc->max) {48alloc->top = (alloc->top + alloc->max) & alloc->mask;49obj = find_first_zero_bit(alloc->table, alloc->max);50}5152if (obj < alloc->max) {53set_bit(obj, alloc->table);54obj |= alloc->top;55} else56obj = -1;5758spin_unlock_irqrestore(&alloc->lock, flags);5960return obj;61}6263void mthca_free(struct mthca_alloc *alloc, u32 obj)64{65unsigned long flags;6667obj &= alloc->max - 1;6869spin_lock_irqsave(&alloc->lock, flags);7071clear_bit(obj, alloc->table);72alloc->last = min(alloc->last, obj);73alloc->top = (alloc->top + alloc->max) & alloc->mask;7475spin_unlock_irqrestore(&alloc->lock, flags);76}7778int mthca_alloc_init(struct mthca_alloc *alloc, u32 num, u32 mask,79u32 reserved)80{81int i;8283/* num must be a power of 2 */84if (num != 1 << (ffs(num) - 1))85return -EINVAL;8687alloc->last = 0;88alloc->top = 0;89alloc->max = num;90alloc->mask = mask;91spin_lock_init(&alloc->lock);92alloc->table = kmalloc(BITS_TO_LONGS(num) * sizeof (long),93GFP_KERNEL);94if (!alloc->table)95return -ENOMEM;9697bitmap_zero(alloc->table, num);98for (i = 0; i < reserved; ++i)99set_bit(i, alloc->table);100101return 0;102}103104void mthca_alloc_cleanup(struct mthca_alloc *alloc)105{106kfree(alloc->table);107}108109/*110* Array of pointers with lazy allocation of leaf pages. Callers of111* _get, _set and _clear methods must use a lock or otherwise112* serialize access to the array.113*/114115#define MTHCA_ARRAY_MASK (PAGE_SIZE / sizeof (void *) - 1)116117void *mthca_array_get(struct mthca_array *array, int index)118{119int p = (index * sizeof (void *)) >> PAGE_SHIFT;120121if (array->page_list[p].page)122return array->page_list[p].page[index & MTHCA_ARRAY_MASK];123else124return NULL;125}126127int mthca_array_set(struct mthca_array *array, int index, void *value)128{129int p = (index * sizeof (void *)) >> PAGE_SHIFT;130131/* Allocate with GFP_ATOMIC because we'll be called with locks held. */132if (!array->page_list[p].page)133array->page_list[p].page = (void **) get_zeroed_page(GFP_ATOMIC);134135if (!array->page_list[p].page)136return -ENOMEM;137138array->page_list[p].page[index & MTHCA_ARRAY_MASK] = value;139++array->page_list[p].used;140141return 0;142}143144void mthca_array_clear(struct mthca_array *array, int index)145{146int p = (index * sizeof (void *)) >> PAGE_SHIFT;147148if (--array->page_list[p].used == 0) {149free_page((unsigned long) array->page_list[p].page);150array->page_list[p].page = NULL;151} else152array->page_list[p].page[index & MTHCA_ARRAY_MASK] = NULL;153154if (array->page_list[p].used < 0)155pr_debug("Array %p index %d page %d with ref count %d < 0\n",156array, index, p, array->page_list[p].used);157}158159int mthca_array_init(struct mthca_array *array, int nent)160{161int npage = (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE;162int i;163164array->page_list = kmalloc(npage * sizeof *array->page_list, GFP_KERNEL);165if (!array->page_list)166return -ENOMEM;167168for (i = 0; i < npage; ++i) {169array->page_list[i].page = NULL;170array->page_list[i].used = 0;171}172173return 0;174}175176void mthca_array_cleanup(struct mthca_array *array, int nent)177{178int i;179180for (i = 0; i < (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE; ++i)181free_page((unsigned long) array->page_list[i].page);182183kfree(array->page_list);184}185186/*187* Handling for queue buffers -- we allocate a bunch of memory and188* register it in a memory region at HCA virtual address 0. If the189* requested size is > max_direct, we split the allocation into190* multiple pages, so we don't require too much contiguous memory.191*/192193int mthca_buf_alloc(struct mthca_dev *dev, int size, int max_direct,194union mthca_buf *buf, int *is_direct, struct mthca_pd *pd,195int hca_write, struct mthca_mr *mr)196{197int err = -ENOMEM;198int npages, shift;199u64 *dma_list = NULL;200dma_addr_t t;201int i;202203if (size <= max_direct) {204*is_direct = 1;205npages = 1;206shift = get_order(size) + PAGE_SHIFT;207208buf->direct.buf = dma_alloc_coherent(&dev->pdev->dev,209size, &t, GFP_KERNEL);210if (!buf->direct.buf)211return -ENOMEM;212213dma_unmap_addr_set(&buf->direct, mapping, t);214215memset(buf->direct.buf, 0, size);216217while (t & ((1 << shift) - 1)) {218--shift;219npages *= 2;220}221222dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);223if (!dma_list)224goto err_free;225226for (i = 0; i < npages; ++i)227dma_list[i] = t + i * (1 << shift);228} else {229*is_direct = 0;230npages = (size + PAGE_SIZE - 1) / PAGE_SIZE;231shift = PAGE_SHIFT;232233dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);234if (!dma_list)235return -ENOMEM;236237buf->page_list = kmalloc(npages * sizeof *buf->page_list,238GFP_KERNEL);239if (!buf->page_list)240goto err_out;241242for (i = 0; i < npages; ++i)243buf->page_list[i].buf = NULL;244245for (i = 0; i < npages; ++i) {246buf->page_list[i].buf =247dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,248&t, GFP_KERNEL);249if (!buf->page_list[i].buf)250goto err_free;251252dma_list[i] = t;253dma_unmap_addr_set(&buf->page_list[i], mapping, t);254255clear_page(buf->page_list[i].buf);256}257}258259err = mthca_mr_alloc_phys(dev, pd->pd_num,260dma_list, shift, npages,2610, size,262MTHCA_MPT_FLAG_LOCAL_READ |263(hca_write ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0),264mr);265if (err)266goto err_free;267268kfree(dma_list);269270return 0;271272err_free:273mthca_buf_free(dev, size, buf, *is_direct, NULL);274275err_out:276kfree(dma_list);277278return err;279}280281void mthca_buf_free(struct mthca_dev *dev, int size, union mthca_buf *buf,282int is_direct, struct mthca_mr *mr)283{284int i;285286if (mr)287mthca_free_mr(dev, mr);288289if (is_direct)290dma_free_coherent(&dev->pdev->dev, size, buf->direct.buf,291dma_unmap_addr(&buf->direct, mapping));292else {293for (i = 0; i < (size + PAGE_SIZE - 1) / PAGE_SIZE; ++i)294dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,295buf->page_list[i].buf,296dma_unmap_addr(&buf->page_list[i],297mapping));298kfree(buf->page_list);299}300}301302303