Path: blob/master/drivers/infiniband/hw/amso1100/c2_pd.c
15112 views
/*1* Copyright (c) 2004 Topspin Communications. All rights reserved.2* Copyright (c) 2005 Cisco Systems. All rights reserved.3* Copyright (c) 2005 Mellanox Technologies. All rights reserved.4* Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.5*6* This software is available to you under a choice of one of two7* licenses. You may choose to be licensed under the terms of the GNU8* General Public License (GPL) Version 2, available from the file9* COPYING in the main directory of this source tree, or the10* OpenIB.org BSD license below:11*12* Redistribution and use in source and binary forms, with or13* without modification, are permitted provided that the following14* conditions are met:15*16* - Redistributions of source code must retain the above17* copyright notice, this list of conditions and the following18* disclaimer.19*20* - Redistributions in binary form must reproduce the above21* copyright notice, this list of conditions and the following22* disclaimer in the documentation and/or other materials23* provided with the distribution.24*25* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,26* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF27* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND28* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS29* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN30* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN31* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE32* SOFTWARE.33*/3435#include <linux/init.h>36#include <linux/slab.h>37#include <linux/errno.h>3839#include "c2.h"40#include "c2_provider.h"4142int c2_pd_alloc(struct c2_dev *c2dev, int privileged, struct c2_pd *pd)43{44u32 obj;45int ret = 0;4647spin_lock(&c2dev->pd_table.lock);48obj = find_next_zero_bit(c2dev->pd_table.table, c2dev->pd_table.max,49c2dev->pd_table.last);50if (obj >= c2dev->pd_table.max)51obj = find_first_zero_bit(c2dev->pd_table.table,52c2dev->pd_table.max);53if (obj < c2dev->pd_table.max) {54pd->pd_id = obj;55__set_bit(obj, c2dev->pd_table.table);56c2dev->pd_table.last = obj+1;57if (c2dev->pd_table.last >= c2dev->pd_table.max)58c2dev->pd_table.last = 0;59} else60ret = -ENOMEM;61spin_unlock(&c2dev->pd_table.lock);62return ret;63}6465void c2_pd_free(struct c2_dev *c2dev, struct c2_pd *pd)66{67spin_lock(&c2dev->pd_table.lock);68__clear_bit(pd->pd_id, c2dev->pd_table.table);69spin_unlock(&c2dev->pd_table.lock);70}7172int __devinit c2_init_pd_table(struct c2_dev *c2dev)73{7475c2dev->pd_table.last = 0;76c2dev->pd_table.max = c2dev->props.max_pd;77spin_lock_init(&c2dev->pd_table.lock);78c2dev->pd_table.table = kmalloc(BITS_TO_LONGS(c2dev->props.max_pd) *79sizeof(long), GFP_KERNEL);80if (!c2dev->pd_table.table)81return -ENOMEM;82bitmap_zero(c2dev->pd_table.table, c2dev->props.max_pd);83return 0;84}8586void __devexit c2_cleanup_pd_table(struct c2_dev *c2dev)87{88kfree(c2dev->pd_table.table);89}909192