Path: blob/master/drivers/infiniband/hw/amso1100/c2_alloc.c
15112 views
/*1* Copyright (c) 2004 Topspin Communications. All rights reserved.2* Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* 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 OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <linux/errno.h>34#include <linux/bitmap.h>3536#include "c2.h"3738static int c2_alloc_mqsp_chunk(struct c2_dev *c2dev, gfp_t gfp_mask,39struct sp_chunk **head)40{41int i;42struct sp_chunk *new_head;43dma_addr_t dma_addr;4445new_head = dma_alloc_coherent(&c2dev->pcidev->dev, PAGE_SIZE,46&dma_addr, gfp_mask);47if (new_head == NULL)48return -ENOMEM;4950new_head->dma_addr = dma_addr;51dma_unmap_addr_set(new_head, mapping, new_head->dma_addr);5253new_head->next = NULL;54new_head->head = 0;5556/* build list where each index is the next free slot */57for (i = 0;58i < (PAGE_SIZE - sizeof(struct sp_chunk) -59sizeof(u16)) / sizeof(u16) - 1;60i++) {61new_head->shared_ptr[i] = i + 1;62}63/* terminate list */64new_head->shared_ptr[i] = 0xFFFF;6566*head = new_head;67return 0;68}6970int c2_init_mqsp_pool(struct c2_dev *c2dev, gfp_t gfp_mask,71struct sp_chunk **root)72{73return c2_alloc_mqsp_chunk(c2dev, gfp_mask, root);74}7576void c2_free_mqsp_pool(struct c2_dev *c2dev, struct sp_chunk *root)77{78struct sp_chunk *next;7980while (root) {81next = root->next;82dma_free_coherent(&c2dev->pcidev->dev, PAGE_SIZE, root,83dma_unmap_addr(root, mapping));84root = next;85}86}8788__be16 *c2_alloc_mqsp(struct c2_dev *c2dev, struct sp_chunk *head,89dma_addr_t *dma_addr, gfp_t gfp_mask)90{91u16 mqsp;9293while (head) {94mqsp = head->head;95if (mqsp != 0xFFFF) {96head->head = head->shared_ptr[mqsp];97break;98} else if (head->next == NULL) {99if (c2_alloc_mqsp_chunk(c2dev, gfp_mask, &head->next) ==1000) {101head = head->next;102mqsp = head->head;103head->head = head->shared_ptr[mqsp];104break;105} else106return NULL;107} else108head = head->next;109}110if (head) {111*dma_addr = head->dma_addr +112((unsigned long) &(head->shared_ptr[mqsp]) -113(unsigned long) head);114pr_debug("%s addr %p dma_addr %llx\n", __func__,115&(head->shared_ptr[mqsp]), (unsigned long long) *dma_addr);116return (__force __be16 *) &(head->shared_ptr[mqsp]);117}118return NULL;119}120121void c2_free_mqsp(__be16 *mqsp)122{123struct sp_chunk *head;124u16 idx;125126/* The chunk containing this ptr begins at the page boundary */127head = (struct sp_chunk *) ((unsigned long) mqsp & PAGE_MASK);128129/* Link head to new mqsp */130*mqsp = (__force __be16) head->head;131132/* Compute the shared_ptr index */133idx = ((unsigned long) mqsp & ~PAGE_MASK) >> 1;134idx -= (unsigned long) &(((struct sp_chunk *) 0)->shared_ptr[0]) >> 1;135136/* Point this index at the head */137head->shared_ptr[idx] = head->head;138139/* Point head at this index */140head->head = idx;141}142143144