Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / infiniband / hw / amso1100 / c2_alloc.c
blob0315f99e419108c9cb453fc9736bd06ff85fae01
1 /*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
34 #include <linux/errno.h>
35 #include <linux/slab.h>
36 #include <linux/bitmap.h>
38 #include "c2.h"
40 static int c2_alloc_mqsp_chunk(struct c2_dev *c2dev, gfp_t gfp_mask,
41 struct sp_chunk **head)
43 int i;
44 struct sp_chunk *new_head;
45 dma_addr_t dma_addr;
47 new_head = dma_alloc_coherent(&c2dev->pcidev->dev, PAGE_SIZE,
48 &dma_addr, gfp_mask);
49 if (new_head == NULL)
50 return -ENOMEM;
52 new_head->dma_addr = dma_addr;
53 pci_unmap_addr_set(new_head, mapping, new_head->dma_addr);
55 new_head->next = NULL;
56 new_head->head = 0;
58 /* build list where each index is the next free slot */
59 for (i = 0;
60 i < (PAGE_SIZE - sizeof(struct sp_chunk) -
61 sizeof(u16)) / sizeof(u16) - 1;
62 i++) {
63 new_head->shared_ptr[i] = i + 1;
65 /* terminate list */
66 new_head->shared_ptr[i] = 0xFFFF;
68 *head = new_head;
69 return 0;
72 int c2_init_mqsp_pool(struct c2_dev *c2dev, gfp_t gfp_mask,
73 struct sp_chunk **root)
75 return c2_alloc_mqsp_chunk(c2dev, gfp_mask, root);
78 void c2_free_mqsp_pool(struct c2_dev *c2dev, struct sp_chunk *root)
80 struct sp_chunk *next;
82 while (root) {
83 next = root->next;
84 dma_free_coherent(&c2dev->pcidev->dev, PAGE_SIZE, root,
85 pci_unmap_addr(root, mapping));
86 root = next;
90 u16 *c2_alloc_mqsp(struct c2_dev *c2dev, struct sp_chunk *head,
91 dma_addr_t *dma_addr, gfp_t gfp_mask)
93 u16 mqsp;
95 while (head) {
96 mqsp = head->head;
97 if (mqsp != 0xFFFF) {
98 head->head = head->shared_ptr[mqsp];
99 break;
100 } else if (head->next == NULL) {
101 if (c2_alloc_mqsp_chunk(c2dev, gfp_mask, &head->next) ==
102 0) {
103 head = head->next;
104 mqsp = head->head;
105 head->head = head->shared_ptr[mqsp];
106 break;
107 } else
108 return NULL;
109 } else
110 head = head->next;
112 if (head) {
113 *dma_addr = head->dma_addr +
114 ((unsigned long) &(head->shared_ptr[mqsp]) -
115 (unsigned long) head);
116 pr_debug("%s addr %p dma_addr %llx\n", __FUNCTION__,
117 &(head->shared_ptr[mqsp]), (unsigned long long) *dma_addr);
118 return &(head->shared_ptr[mqsp]);
120 return NULL;
123 void c2_free_mqsp(u16 * mqsp)
125 struct sp_chunk *head;
126 u16 idx;
128 /* The chunk containing this ptr begins at the page boundary */
129 head = (struct sp_chunk *) ((unsigned long) mqsp & PAGE_MASK);
131 /* Link head to new mqsp */
132 *mqsp = head->head;
134 /* Compute the shared_ptr index */
135 idx = ((unsigned long) mqsp & ~PAGE_MASK) >> 1;
136 idx -= (unsigned long) &(((struct sp_chunk *) 0)->shared_ptr[0]) >> 1;
138 /* Point this index at the head */
139 head->shared_ptr[idx] = head->head;
141 /* Point head at this index */
142 head->head = idx;