1*33ec1ccbSHans Petter Selasky /*
2*33ec1ccbSHans Petter Selasky * Copyright (c) 2004 Topspin Communications. All rights reserved.
3*33ec1ccbSHans Petter Selasky *
4*33ec1ccbSHans Petter Selasky * This software is available to you under a choice of one of two
5*33ec1ccbSHans Petter Selasky * licenses. You may choose to be licensed under the terms of the GNU
6*33ec1ccbSHans Petter Selasky * General Public License (GPL) Version 2, available from the file
7*33ec1ccbSHans Petter Selasky * COPYING in the main directory of this source tree, or the
8*33ec1ccbSHans Petter Selasky * OpenIB.org BSD license below:
9*33ec1ccbSHans Petter Selasky *
10*33ec1ccbSHans Petter Selasky * Redistribution and use in source and binary forms, with or
11*33ec1ccbSHans Petter Selasky * without modification, are permitted provided that the following
12*33ec1ccbSHans Petter Selasky * conditions are met:
13*33ec1ccbSHans Petter Selasky *
14*33ec1ccbSHans Petter Selasky * - Redistributions of source code must retain the above
15*33ec1ccbSHans Petter Selasky * copyright notice, this list of conditions and the following
16*33ec1ccbSHans Petter Selasky * disclaimer.
17*33ec1ccbSHans Petter Selasky *
18*33ec1ccbSHans Petter Selasky * - Redistributions in binary form must reproduce the above
19*33ec1ccbSHans Petter Selasky * copyright notice, this list of conditions and the following
20*33ec1ccbSHans Petter Selasky * disclaimer in the documentation and/or other materials
21*33ec1ccbSHans Petter Selasky * provided with the distribution.
22*33ec1ccbSHans Petter Selasky *
23*33ec1ccbSHans Petter Selasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*33ec1ccbSHans Petter Selasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*33ec1ccbSHans Petter Selasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*33ec1ccbSHans Petter Selasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*33ec1ccbSHans Petter Selasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*33ec1ccbSHans Petter Selasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*33ec1ccbSHans Petter Selasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*33ec1ccbSHans Petter Selasky * SOFTWARE.
31*33ec1ccbSHans Petter Selasky */
32*33ec1ccbSHans Petter Selasky
33*33ec1ccbSHans Petter Selasky #include <linux/errno.h>
34*33ec1ccbSHans Petter Selasky #include <linux/slab.h>
35*33ec1ccbSHans Petter Selasky #include <linux/bitmap.h>
36*33ec1ccbSHans Petter Selasky
37*33ec1ccbSHans Petter Selasky #include "mthca_dev.h"
38*33ec1ccbSHans Petter Selasky
39*33ec1ccbSHans Petter Selasky /* Trivial bitmap-based allocator */
mthca_alloc(struct mthca_alloc * alloc)40*33ec1ccbSHans Petter Selasky u32 mthca_alloc(struct mthca_alloc *alloc)
41*33ec1ccbSHans Petter Selasky {
42*33ec1ccbSHans Petter Selasky unsigned long flags;
43*33ec1ccbSHans Petter Selasky u32 obj;
44*33ec1ccbSHans Petter Selasky
45*33ec1ccbSHans Petter Selasky spin_lock_irqsave(&alloc->lock, flags);
46*33ec1ccbSHans Petter Selasky
47*33ec1ccbSHans Petter Selasky obj = find_next_zero_bit(alloc->table, alloc->max, alloc->last);
48*33ec1ccbSHans Petter Selasky if (obj >= alloc->max) {
49*33ec1ccbSHans Petter Selasky alloc->top = (alloc->top + alloc->max) & alloc->mask;
50*33ec1ccbSHans Petter Selasky obj = find_first_zero_bit(alloc->table, alloc->max);
51*33ec1ccbSHans Petter Selasky }
52*33ec1ccbSHans Petter Selasky
53*33ec1ccbSHans Petter Selasky if (obj < alloc->max) {
54*33ec1ccbSHans Petter Selasky set_bit(obj, alloc->table);
55*33ec1ccbSHans Petter Selasky obj |= alloc->top;
56*33ec1ccbSHans Petter Selasky } else
57*33ec1ccbSHans Petter Selasky obj = -1;
58*33ec1ccbSHans Petter Selasky
59*33ec1ccbSHans Petter Selasky spin_unlock_irqrestore(&alloc->lock, flags);
60*33ec1ccbSHans Petter Selasky
61*33ec1ccbSHans Petter Selasky return obj;
62*33ec1ccbSHans Petter Selasky }
63*33ec1ccbSHans Petter Selasky
mthca_free(struct mthca_alloc * alloc,u32 obj)64*33ec1ccbSHans Petter Selasky void mthca_free(struct mthca_alloc *alloc, u32 obj)
65*33ec1ccbSHans Petter Selasky {
66*33ec1ccbSHans Petter Selasky unsigned long flags;
67*33ec1ccbSHans Petter Selasky
68*33ec1ccbSHans Petter Selasky obj &= alloc->max - 1;
69*33ec1ccbSHans Petter Selasky
70*33ec1ccbSHans Petter Selasky spin_lock_irqsave(&alloc->lock, flags);
71*33ec1ccbSHans Petter Selasky
72*33ec1ccbSHans Petter Selasky clear_bit(obj, alloc->table);
73*33ec1ccbSHans Petter Selasky alloc->last = min(alloc->last, obj);
74*33ec1ccbSHans Petter Selasky alloc->top = (alloc->top + alloc->max) & alloc->mask;
75*33ec1ccbSHans Petter Selasky
76*33ec1ccbSHans Petter Selasky spin_unlock_irqrestore(&alloc->lock, flags);
77*33ec1ccbSHans Petter Selasky }
78*33ec1ccbSHans Petter Selasky
mthca_alloc_init(struct mthca_alloc * alloc,u32 num,u32 mask,u32 reserved)79*33ec1ccbSHans Petter Selasky int mthca_alloc_init(struct mthca_alloc *alloc, u32 num, u32 mask,
80*33ec1ccbSHans Petter Selasky u32 reserved)
81*33ec1ccbSHans Petter Selasky {
82*33ec1ccbSHans Petter Selasky int i;
83*33ec1ccbSHans Petter Selasky
84*33ec1ccbSHans Petter Selasky /* num must be a power of 2 */
85*33ec1ccbSHans Petter Selasky if (num != 1 << (ffs(num) - 1))
86*33ec1ccbSHans Petter Selasky return -EINVAL;
87*33ec1ccbSHans Petter Selasky
88*33ec1ccbSHans Petter Selasky alloc->last = 0;
89*33ec1ccbSHans Petter Selasky alloc->top = 0;
90*33ec1ccbSHans Petter Selasky alloc->max = num;
91*33ec1ccbSHans Petter Selasky alloc->mask = mask;
92*33ec1ccbSHans Petter Selasky spin_lock_init(&alloc->lock);
93*33ec1ccbSHans Petter Selasky alloc->table = kmalloc(BITS_TO_LONGS(num) * sizeof (long),
94*33ec1ccbSHans Petter Selasky GFP_KERNEL);
95*33ec1ccbSHans Petter Selasky if (!alloc->table)
96*33ec1ccbSHans Petter Selasky return -ENOMEM;
97*33ec1ccbSHans Petter Selasky
98*33ec1ccbSHans Petter Selasky bitmap_zero(alloc->table, num);
99*33ec1ccbSHans Petter Selasky for (i = 0; i < reserved; ++i)
100*33ec1ccbSHans Petter Selasky set_bit(i, alloc->table);
101*33ec1ccbSHans Petter Selasky
102*33ec1ccbSHans Petter Selasky return 0;
103*33ec1ccbSHans Petter Selasky }
104*33ec1ccbSHans Petter Selasky
mthca_alloc_cleanup(struct mthca_alloc * alloc)105*33ec1ccbSHans Petter Selasky void mthca_alloc_cleanup(struct mthca_alloc *alloc)
106*33ec1ccbSHans Petter Selasky {
107*33ec1ccbSHans Petter Selasky kfree(alloc->table);
108*33ec1ccbSHans Petter Selasky }
109*33ec1ccbSHans Petter Selasky
110*33ec1ccbSHans Petter Selasky /*
111*33ec1ccbSHans Petter Selasky * Array of pointers with lazy allocation of leaf pages. Callers of
112*33ec1ccbSHans Petter Selasky * _get, _set and _clear methods must use a lock or otherwise
113*33ec1ccbSHans Petter Selasky * serialize access to the array.
114*33ec1ccbSHans Petter Selasky */
115*33ec1ccbSHans Petter Selasky
116*33ec1ccbSHans Petter Selasky #define MTHCA_ARRAY_MASK (PAGE_SIZE / sizeof (void *) - 1)
117*33ec1ccbSHans Petter Selasky
mthca_array_get(struct mthca_array * array,int index)118*33ec1ccbSHans Petter Selasky void *mthca_array_get(struct mthca_array *array, int index)
119*33ec1ccbSHans Petter Selasky {
120*33ec1ccbSHans Petter Selasky int p = (index * sizeof (void *)) >> PAGE_SHIFT;
121*33ec1ccbSHans Petter Selasky
122*33ec1ccbSHans Petter Selasky if (array->page_list[p].page)
123*33ec1ccbSHans Petter Selasky return array->page_list[p].page[index & MTHCA_ARRAY_MASK];
124*33ec1ccbSHans Petter Selasky else
125*33ec1ccbSHans Petter Selasky return NULL;
126*33ec1ccbSHans Petter Selasky }
127*33ec1ccbSHans Petter Selasky
mthca_array_set(struct mthca_array * array,int index,void * value)128*33ec1ccbSHans Petter Selasky int mthca_array_set(struct mthca_array *array, int index, void *value)
129*33ec1ccbSHans Petter Selasky {
130*33ec1ccbSHans Petter Selasky int p = (index * sizeof (void *)) >> PAGE_SHIFT;
131*33ec1ccbSHans Petter Selasky
132*33ec1ccbSHans Petter Selasky /* Allocate with GFP_ATOMIC because we'll be called with locks held. */
133*33ec1ccbSHans Petter Selasky if (!array->page_list[p].page)
134*33ec1ccbSHans Petter Selasky array->page_list[p].page = (void **) get_zeroed_page(GFP_ATOMIC);
135*33ec1ccbSHans Petter Selasky
136*33ec1ccbSHans Petter Selasky if (!array->page_list[p].page)
137*33ec1ccbSHans Petter Selasky return -ENOMEM;
138*33ec1ccbSHans Petter Selasky
139*33ec1ccbSHans Petter Selasky array->page_list[p].page[index & MTHCA_ARRAY_MASK] = value;
140*33ec1ccbSHans Petter Selasky ++array->page_list[p].used;
141*33ec1ccbSHans Petter Selasky
142*33ec1ccbSHans Petter Selasky return 0;
143*33ec1ccbSHans Petter Selasky }
144*33ec1ccbSHans Petter Selasky
mthca_array_clear(struct mthca_array * array,int index)145*33ec1ccbSHans Petter Selasky void mthca_array_clear(struct mthca_array *array, int index)
146*33ec1ccbSHans Petter Selasky {
147*33ec1ccbSHans Petter Selasky int p = (index * sizeof (void *)) >> PAGE_SHIFT;
148*33ec1ccbSHans Petter Selasky
149*33ec1ccbSHans Petter Selasky if (--array->page_list[p].used == 0) {
150*33ec1ccbSHans Petter Selasky free_page((unsigned long) array->page_list[p].page);
151*33ec1ccbSHans Petter Selasky array->page_list[p].page = NULL;
152*33ec1ccbSHans Petter Selasky } else
153*33ec1ccbSHans Petter Selasky array->page_list[p].page[index & MTHCA_ARRAY_MASK] = NULL;
154*33ec1ccbSHans Petter Selasky
155*33ec1ccbSHans Petter Selasky if (array->page_list[p].used < 0)
156*33ec1ccbSHans Petter Selasky pr_debug("Array %p index %d page %d with ref count %d < 0\n",
157*33ec1ccbSHans Petter Selasky array, index, p, array->page_list[p].used);
158*33ec1ccbSHans Petter Selasky }
159*33ec1ccbSHans Petter Selasky
mthca_array_init(struct mthca_array * array,int nent)160*33ec1ccbSHans Petter Selasky int mthca_array_init(struct mthca_array *array, int nent)
161*33ec1ccbSHans Petter Selasky {
162*33ec1ccbSHans Petter Selasky int npage = (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE;
163*33ec1ccbSHans Petter Selasky int i;
164*33ec1ccbSHans Petter Selasky
165*33ec1ccbSHans Petter Selasky array->page_list = kmalloc(npage * sizeof *array->page_list, GFP_KERNEL);
166*33ec1ccbSHans Petter Selasky if (!array->page_list)
167*33ec1ccbSHans Petter Selasky return -ENOMEM;
168*33ec1ccbSHans Petter Selasky
169*33ec1ccbSHans Petter Selasky for (i = 0; i < npage; ++i) {
170*33ec1ccbSHans Petter Selasky array->page_list[i].page = NULL;
171*33ec1ccbSHans Petter Selasky array->page_list[i].used = 0;
172*33ec1ccbSHans Petter Selasky }
173*33ec1ccbSHans Petter Selasky
174*33ec1ccbSHans Petter Selasky return 0;
175*33ec1ccbSHans Petter Selasky }
176*33ec1ccbSHans Petter Selasky
mthca_array_cleanup(struct mthca_array * array,int nent)177*33ec1ccbSHans Petter Selasky void mthca_array_cleanup(struct mthca_array *array, int nent)
178*33ec1ccbSHans Petter Selasky {
179*33ec1ccbSHans Petter Selasky int i;
180*33ec1ccbSHans Petter Selasky
181*33ec1ccbSHans Petter Selasky for (i = 0; i < (nent * sizeof (void *) + PAGE_SIZE - 1) / PAGE_SIZE; ++i)
182*33ec1ccbSHans Petter Selasky free_page((unsigned long) array->page_list[i].page);
183*33ec1ccbSHans Petter Selasky
184*33ec1ccbSHans Petter Selasky kfree(array->page_list);
185*33ec1ccbSHans Petter Selasky }
186*33ec1ccbSHans Petter Selasky
187*33ec1ccbSHans Petter Selasky /*
188*33ec1ccbSHans Petter Selasky * Handling for queue buffers -- we allocate a bunch of memory and
189*33ec1ccbSHans Petter Selasky * register it in a memory region at HCA virtual address 0. If the
190*33ec1ccbSHans Petter Selasky * requested size is > max_direct, we split the allocation into
191*33ec1ccbSHans Petter Selasky * multiple pages, so we don't require too much contiguous memory.
192*33ec1ccbSHans Petter Selasky */
193*33ec1ccbSHans Petter Selasky
mthca_buf_alloc(struct mthca_dev * dev,int size,int max_direct,union mthca_buf * buf,int * is_direct,struct mthca_pd * pd,int hca_write,struct mthca_mr * mr)194*33ec1ccbSHans Petter Selasky int mthca_buf_alloc(struct mthca_dev *dev, int size, int max_direct,
195*33ec1ccbSHans Petter Selasky union mthca_buf *buf, int *is_direct, struct mthca_pd *pd,
196*33ec1ccbSHans Petter Selasky int hca_write, struct mthca_mr *mr)
197*33ec1ccbSHans Petter Selasky {
198*33ec1ccbSHans Petter Selasky int err = -ENOMEM;
199*33ec1ccbSHans Petter Selasky int npages, shift;
200*33ec1ccbSHans Petter Selasky u64 *dma_list = NULL;
201*33ec1ccbSHans Petter Selasky dma_addr_t t;
202*33ec1ccbSHans Petter Selasky int i;
203*33ec1ccbSHans Petter Selasky
204*33ec1ccbSHans Petter Selasky if (size <= max_direct) {
205*33ec1ccbSHans Petter Selasky *is_direct = 1;
206*33ec1ccbSHans Petter Selasky npages = 1;
207*33ec1ccbSHans Petter Selasky shift = get_order(size) + PAGE_SHIFT;
208*33ec1ccbSHans Petter Selasky
209*33ec1ccbSHans Petter Selasky buf->direct.buf = dma_alloc_coherent(&dev->pdev->dev,
210*33ec1ccbSHans Petter Selasky size, &t, GFP_KERNEL);
211*33ec1ccbSHans Petter Selasky if (!buf->direct.buf)
212*33ec1ccbSHans Petter Selasky return -ENOMEM;
213*33ec1ccbSHans Petter Selasky
214*33ec1ccbSHans Petter Selasky dma_unmap_addr_set(&buf->direct, mapping, t);
215*33ec1ccbSHans Petter Selasky
216*33ec1ccbSHans Petter Selasky memset(buf->direct.buf, 0, size);
217*33ec1ccbSHans Petter Selasky
218*33ec1ccbSHans Petter Selasky while (t & ((1 << shift) - 1)) {
219*33ec1ccbSHans Petter Selasky --shift;
220*33ec1ccbSHans Petter Selasky npages *= 2;
221*33ec1ccbSHans Petter Selasky }
222*33ec1ccbSHans Petter Selasky
223*33ec1ccbSHans Petter Selasky dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
224*33ec1ccbSHans Petter Selasky if (!dma_list)
225*33ec1ccbSHans Petter Selasky goto err_free;
226*33ec1ccbSHans Petter Selasky
227*33ec1ccbSHans Petter Selasky for (i = 0; i < npages; ++i)
228*33ec1ccbSHans Petter Selasky dma_list[i] = t + i * (1 << shift);
229*33ec1ccbSHans Petter Selasky } else {
230*33ec1ccbSHans Petter Selasky *is_direct = 0;
231*33ec1ccbSHans Petter Selasky npages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
232*33ec1ccbSHans Petter Selasky shift = PAGE_SHIFT;
233*33ec1ccbSHans Petter Selasky
234*33ec1ccbSHans Petter Selasky dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
235*33ec1ccbSHans Petter Selasky if (!dma_list)
236*33ec1ccbSHans Petter Selasky return -ENOMEM;
237*33ec1ccbSHans Petter Selasky
238*33ec1ccbSHans Petter Selasky buf->page_list = kmalloc(npages * sizeof *buf->page_list,
239*33ec1ccbSHans Petter Selasky GFP_KERNEL);
240*33ec1ccbSHans Petter Selasky if (!buf->page_list)
241*33ec1ccbSHans Petter Selasky goto err_out;
242*33ec1ccbSHans Petter Selasky
243*33ec1ccbSHans Petter Selasky for (i = 0; i < npages; ++i)
244*33ec1ccbSHans Petter Selasky buf->page_list[i].buf = NULL;
245*33ec1ccbSHans Petter Selasky
246*33ec1ccbSHans Petter Selasky for (i = 0; i < npages; ++i) {
247*33ec1ccbSHans Petter Selasky buf->page_list[i].buf =
248*33ec1ccbSHans Petter Selasky dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE,
249*33ec1ccbSHans Petter Selasky &t, GFP_KERNEL);
250*33ec1ccbSHans Petter Selasky if (!buf->page_list[i].buf)
251*33ec1ccbSHans Petter Selasky goto err_free;
252*33ec1ccbSHans Petter Selasky
253*33ec1ccbSHans Petter Selasky dma_list[i] = t;
254*33ec1ccbSHans Petter Selasky dma_unmap_addr_set(&buf->page_list[i], mapping, t);
255*33ec1ccbSHans Petter Selasky
256*33ec1ccbSHans Petter Selasky clear_page(buf->page_list[i].buf);
257*33ec1ccbSHans Petter Selasky }
258*33ec1ccbSHans Petter Selasky }
259*33ec1ccbSHans Petter Selasky
260*33ec1ccbSHans Petter Selasky err = mthca_mr_alloc_phys(dev, pd->pd_num,
261*33ec1ccbSHans Petter Selasky dma_list, shift, npages,
262*33ec1ccbSHans Petter Selasky 0, size,
263*33ec1ccbSHans Petter Selasky MTHCA_MPT_FLAG_LOCAL_READ |
264*33ec1ccbSHans Petter Selasky (hca_write ? MTHCA_MPT_FLAG_LOCAL_WRITE : 0),
265*33ec1ccbSHans Petter Selasky mr);
266*33ec1ccbSHans Petter Selasky if (err)
267*33ec1ccbSHans Petter Selasky goto err_free;
268*33ec1ccbSHans Petter Selasky
269*33ec1ccbSHans Petter Selasky kfree(dma_list);
270*33ec1ccbSHans Petter Selasky
271*33ec1ccbSHans Petter Selasky return 0;
272*33ec1ccbSHans Petter Selasky
273*33ec1ccbSHans Petter Selasky err_free:
274*33ec1ccbSHans Petter Selasky mthca_buf_free(dev, size, buf, *is_direct, NULL);
275*33ec1ccbSHans Petter Selasky
276*33ec1ccbSHans Petter Selasky err_out:
277*33ec1ccbSHans Petter Selasky kfree(dma_list);
278*33ec1ccbSHans Petter Selasky
279*33ec1ccbSHans Petter Selasky return err;
280*33ec1ccbSHans Petter Selasky }
281*33ec1ccbSHans Petter Selasky
mthca_buf_free(struct mthca_dev * dev,int size,union mthca_buf * buf,int is_direct,struct mthca_mr * mr)282*33ec1ccbSHans Petter Selasky void mthca_buf_free(struct mthca_dev *dev, int size, union mthca_buf *buf,
283*33ec1ccbSHans Petter Selasky int is_direct, struct mthca_mr *mr)
284*33ec1ccbSHans Petter Selasky {
285*33ec1ccbSHans Petter Selasky int i;
286*33ec1ccbSHans Petter Selasky
287*33ec1ccbSHans Petter Selasky if (mr)
288*33ec1ccbSHans Petter Selasky mthca_free_mr(dev, mr);
289*33ec1ccbSHans Petter Selasky
290*33ec1ccbSHans Petter Selasky if (is_direct)
291*33ec1ccbSHans Petter Selasky dma_free_coherent(&dev->pdev->dev, size, buf->direct.buf,
292*33ec1ccbSHans Petter Selasky dma_unmap_addr(&buf->direct, mapping));
293*33ec1ccbSHans Petter Selasky else {
294*33ec1ccbSHans Petter Selasky for (i = 0; i < (size + PAGE_SIZE - 1) / PAGE_SIZE; ++i)
295*33ec1ccbSHans Petter Selasky dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
296*33ec1ccbSHans Petter Selasky buf->page_list[i].buf,
297*33ec1ccbSHans Petter Selasky dma_unmap_addr(&buf->page_list[i],
298*33ec1ccbSHans Petter Selasky mapping));
299*33ec1ccbSHans Petter Selasky kfree(buf->page_list);
300*33ec1ccbSHans Petter Selasky }
301*33ec1ccbSHans Petter Selasky }
302