blob: 8c2a83732b5d53ca1a4f019a1303de335233ec9f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Roland Dreier56483ec2005-07-07 17:57:16 -07003 * Copyright (c) 2005 Cisco Systems. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07004 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * 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 OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
Roland Dreierdbcf31b2005-05-01 08:59:14 -070035#include <linux/mm.h>
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +020036#include <linux/scatterlist.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040037#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +020039
40#include <asm/page.h>
Roland Dreierdbcf31b2005-05-01 08:59:14 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "mthca_memfree.h"
43#include "mthca_dev.h"
44#include "mthca_cmd.h"
45
46/*
47 * We allocate in as big chunks as we can, up to a maximum of 256 KB
48 * per chunk.
49 */
50enum {
51 MTHCA_ICM_ALLOC_SIZE = 1 << 18,
52 MTHCA_TABLE_CHUNK_SIZE = 1 << 18
53};
54
Roland Dreier56483ec2005-07-07 17:57:16 -070055struct mthca_user_db_table {
Roland Dreierfd9cfdd2006-01-30 16:45:11 -080056 struct mutex mutex;
Roland Dreier56483ec2005-07-07 17:57:16 -070057 struct {
58 u64 uvirt;
59 struct scatterlist mem;
60 int refcount;
61 } page[0];
62};
63
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +020064static void mthca_free_icm_pages(struct mthca_dev *dev, struct mthca_icm_chunk *chunk)
65{
66 int i;
67
68 if (chunk->nsg > 0)
69 pci_unmap_sg(dev->pdev, chunk->mem, chunk->npages,
70 PCI_DMA_BIDIRECTIONAL);
71
72 for (i = 0; i < chunk->npages; ++i)
Jens Axboe45711f12007-10-22 21:19:53 +020073 __free_pages(sg_page(&chunk->mem[i]),
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +020074 get_order(chunk->mem[i].length));
75}
76
77static void mthca_free_icm_coherent(struct mthca_dev *dev, struct mthca_icm_chunk *chunk)
78{
79 int i;
80
81 for (i = 0; i < chunk->npages; ++i) {
82 dma_free_coherent(&dev->pdev->dev, chunk->mem[i].length,
Jens Axboe45711f12007-10-22 21:19:53 +020083 lowmem_page_address(sg_page(&chunk->mem[i])),
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +020084 sg_dma_address(&chunk->mem[i]));
85 }
86}
87
88void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm, int coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 struct mthca_icm_chunk *chunk, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 if (!icm)
93 return;
94
95 list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +020096 if (coherent)
97 mthca_free_icm_coherent(dev, chunk);
98 else
99 mthca_free_icm_pages(dev, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 kfree(chunk);
102 }
103
104 kfree(icm);
105}
106
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200107static int mthca_alloc_icm_pages(struct scatterlist *mem, int order, gfp_t gfp_mask)
108{
Jens Axboe45711f12007-10-22 21:19:53 +0200109 struct page *page;
110
Eli Cohen87afd442008-06-23 09:29:58 -0700111 /*
112 * Use __GFP_ZERO because buggy firmware assumes ICM pages are
113 * cleared, and subtle failures are seen if they aren't.
114 */
115 page = alloc_pages(gfp_mask | __GFP_ZERO, order);
Jens Axboe45711f12007-10-22 21:19:53 +0200116 if (!page)
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200117 return -ENOMEM;
118
Jens Axboe642f1492007-10-24 11:20:47 +0200119 sg_set_page(mem, page, PAGE_SIZE << order, 0);
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200120 return 0;
121}
122
123static int mthca_alloc_icm_coherent(struct device *dev, struct scatterlist *mem,
124 int order, gfp_t gfp_mask)
125{
126 void *buf = dma_alloc_coherent(dev, PAGE_SIZE << order, &sg_dma_address(mem),
127 gfp_mask);
128 if (!buf)
129 return -ENOMEM;
130
131 sg_set_buf(mem, buf, PAGE_SIZE << order);
132 BUG_ON(mem->offset);
133 sg_dma_len(mem) = PAGE_SIZE << order;
134 return 0;
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200138 gfp_t gfp_mask, int coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 struct mthca_icm *icm;
141 struct mthca_icm_chunk *chunk = NULL;
142 int cur_order;
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200143 int ret;
144
145 /* We use sg_set_buf for coherent allocs, which assumes low memory */
146 BUG_ON(coherent && (gfp_mask & __GFP_HIGHMEM));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 icm = kmalloc(sizeof *icm, gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
149 if (!icm)
150 return icm;
151
152 icm->refcount = 0;
153 INIT_LIST_HEAD(&icm->chunk_list);
154
155 cur_order = get_order(MTHCA_ICM_ALLOC_SIZE);
156
157 while (npages > 0) {
158 if (!chunk) {
159 chunk = kmalloc(sizeof *chunk,
160 gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
161 if (!chunk)
162 goto fail;
163
Jens Axboe45711f12007-10-22 21:19:53 +0200164 sg_init_table(chunk->mem, MTHCA_ICM_CHUNK_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 chunk->npages = 0;
166 chunk->nsg = 0;
167 list_add_tail(&chunk->list, &icm->chunk_list);
168 }
169
170 while (1 << cur_order > npages)
171 --cur_order;
172
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200173 if (coherent)
174 ret = mthca_alloc_icm_coherent(&dev->pdev->dev,
175 &chunk->mem[chunk->npages],
176 cur_order, gfp_mask);
177 else
178 ret = mthca_alloc_icm_pages(&chunk->mem[chunk->npages],
179 cur_order, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200181 if (!ret) {
182 ++chunk->npages;
183
Roland Dreier11282b32007-02-16 13:57:33 -0800184 if (coherent)
185 ++chunk->nsg;
186 else if (chunk->npages == MTHCA_ICM_CHUNK_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
188 chunk->npages,
189 PCI_DMA_BIDIRECTIONAL);
190
191 if (chunk->nsg <= 0)
192 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200195 if (chunk->npages == MTHCA_ICM_CHUNK_LEN)
196 chunk = NULL;
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 npages -= 1 << cur_order;
199 } else {
200 --cur_order;
201 if (cur_order < 0)
202 goto fail;
203 }
204 }
205
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200206 if (!coherent && chunk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
208 chunk->npages,
209 PCI_DMA_BIDIRECTIONAL);
210
211 if (chunk->nsg <= 0)
212 goto fail;
213 }
214
215 return icm;
216
217fail:
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200218 mthca_free_icm(dev, icm, coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return NULL;
220}
221
222int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
223{
224 int i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
225 int ret = 0;
226 u8 status;
227
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800228 mutex_lock(&table->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (table->icm[i]) {
231 ++table->icm[i]->refcount;
232 goto out;
233 }
234
235 table->icm[i] = mthca_alloc_icm(dev, MTHCA_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
236 (table->lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200237 __GFP_NOWARN, table->coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (!table->icm[i]) {
239 ret = -ENOMEM;
240 goto out;
241 }
242
243 if (mthca_MAP_ICM(dev, table->icm[i], table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
244 &status) || status) {
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200245 mthca_free_icm(dev, table->icm[i], table->coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 table->icm[i] = NULL;
247 ret = -ENOMEM;
248 goto out;
249 }
250
251 ++table->icm[i]->refcount;
252
253out:
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800254 mutex_unlock(&table->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return ret;
256}
257
258void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
259{
Roland Dreiera03a5a62005-06-27 14:36:43 -0700260 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 u8 status;
262
Roland Dreiera03a5a62005-06-27 14:36:43 -0700263 if (!mthca_is_memfree(dev))
264 return;
265
266 i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
267
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800268 mutex_lock(&table->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (--table->icm[i]->refcount == 0) {
271 mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800272 MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
273 &status);
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200274 mthca_free_icm(dev, table->icm[i], table->coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 table->icm[i] = NULL;
276 }
277
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800278 mutex_unlock(&table->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200281void *mthca_table_find(struct mthca_icm_table *table, int obj, dma_addr_t *dma_handle)
Michael S. Tsirkin0fabd9f2005-04-16 15:26:29 -0700282{
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200283 int idx, offset, dma_offset, i;
Michael S. Tsirkin0fabd9f2005-04-16 15:26:29 -0700284 struct mthca_icm_chunk *chunk;
285 struct mthca_icm *icm;
286 struct page *page = NULL;
287
288 if (!table->lowmem)
289 return NULL;
290
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800291 mutex_lock(&table->mutex);
Michael S. Tsirkin0fabd9f2005-04-16 15:26:29 -0700292
293 idx = (obj & (table->num_obj - 1)) * table->obj_size;
294 icm = table->icm[idx / MTHCA_TABLE_CHUNK_SIZE];
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200295 dma_offset = offset = idx % MTHCA_TABLE_CHUNK_SIZE;
Michael S. Tsirkin0fabd9f2005-04-16 15:26:29 -0700296
297 if (!icm)
298 goto out;
299
300 list_for_each_entry(chunk, &icm->chunk_list, list) {
301 for (i = 0; i < chunk->npages; ++i) {
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200302 if (dma_handle && dma_offset >= 0) {
303 if (sg_dma_len(&chunk->mem[i]) > dma_offset)
304 *dma_handle = sg_dma_address(&chunk->mem[i]) +
305 dma_offset;
306 dma_offset -= sg_dma_len(&chunk->mem[i]);
307 }
308 /* DMA mapping can merge pages but not split them,
309 * so if we found the page, dma_handle has already
310 * been assigned to. */
Michael S. Tsirkin46707e92007-01-03 14:46:30 +0200311 if (chunk->mem[i].length > offset) {
Jens Axboe45711f12007-10-22 21:19:53 +0200312 page = sg_page(&chunk->mem[i]);
Michael S. Tsirkin6c7d2a72005-12-15 13:55:50 -0800313 goto out;
Michael S. Tsirkin0fabd9f2005-04-16 15:26:29 -0700314 }
315 offset -= chunk->mem[i].length;
316 }
317 }
318
319out:
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800320 mutex_unlock(&table->mutex);
Michael S. Tsirkin0fabd9f2005-04-16 15:26:29 -0700321 return page ? lowmem_page_address(page) + offset : NULL;
322}
323
Roland Dreier86562a12005-04-16 15:26:13 -0700324int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,
325 int start, int end)
326{
327 int inc = MTHCA_TABLE_CHUNK_SIZE / table->obj_size;
328 int i, err;
329
330 for (i = start; i <= end; i += inc) {
331 err = mthca_table_get(dev, table, i);
332 if (err)
333 goto fail;
334 }
335
336 return 0;
337
338fail:
339 while (i > start) {
340 i -= inc;
341 mthca_table_put(dev, table, i);
342 }
343
344 return err;
345}
346
347void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,
348 int start, int end)
349{
350 int i;
351
Roland Dreiera03a5a62005-06-27 14:36:43 -0700352 if (!mthca_is_memfree(dev))
353 return;
354
Roland Dreier86562a12005-04-16 15:26:13 -0700355 for (i = start; i <= end; i += MTHCA_TABLE_CHUNK_SIZE / table->obj_size)
356 mthca_table_put(dev, table, i);
357}
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
360 u64 virt, int obj_size,
361 int nobj, int reserved,
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200362 int use_lowmem, int use_coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct mthca_icm_table *table;
Roland Dreierc263ff62008-04-16 21:01:13 -0700365 int obj_per_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int num_icm;
Roland Dreierd20a4012005-08-19 10:36:11 -0700367 unsigned chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int i;
369 u8 status;
370
Roland Dreierc263ff62008-04-16 21:01:13 -0700371 obj_per_chunk = MTHCA_TABLE_CHUNK_SIZE / obj_size;
372 num_icm = DIV_ROUND_UP(nobj, obj_per_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 table = kmalloc(sizeof *table + num_icm * sizeof *table->icm, GFP_KERNEL);
375 if (!table)
376 return NULL;
377
378 table->virt = virt;
379 table->num_icm = num_icm;
380 table->num_obj = nobj;
381 table->obj_size = obj_size;
382 table->lowmem = use_lowmem;
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200383 table->coherent = use_coherent;
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800384 mutex_init(&table->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 for (i = 0; i < num_icm; ++i)
387 table->icm[i] = NULL;
388
389 for (i = 0; i * MTHCA_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) {
Roland Dreierd20a4012005-08-19 10:36:11 -0700390 chunk_size = MTHCA_TABLE_CHUNK_SIZE;
391 if ((i + 1) * MTHCA_TABLE_CHUNK_SIZE > nobj * obj_size)
392 chunk_size = nobj * obj_size - i * MTHCA_TABLE_CHUNK_SIZE;
393
394 table->icm[i] = mthca_alloc_icm(dev, chunk_size >> PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200396 __GFP_NOWARN, use_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!table->icm[i])
398 goto err;
399 if (mthca_MAP_ICM(dev, table->icm[i], virt + i * MTHCA_TABLE_CHUNK_SIZE,
400 &status) || status) {
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200401 mthca_free_icm(dev, table->icm[i], table->coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 table->icm[i] = NULL;
403 goto err;
404 }
405
406 /*
407 * Add a reference to this ICM chunk so that it never
408 * gets freed (since it contains reserved firmware objects).
409 */
410 ++table->icm[i]->refcount;
411 }
412
413 return table;
414
415err:
416 for (i = 0; i < num_icm; ++i)
417 if (table->icm[i]) {
418 mthca_UNMAP_ICM(dev, virt + i * MTHCA_TABLE_CHUNK_SIZE,
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800419 MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
Roland Dreierb3999392008-04-16 21:01:03 -0700420 &status);
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200421 mthca_free_icm(dev, table->icm[i], table->coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
424 kfree(table);
425
426 return NULL;
427}
428
429void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table)
430{
431 int i;
432 u8 status;
433
434 for (i = 0; i < table->num_icm; ++i)
435 if (table->icm[i]) {
436 mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800437 MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
438 &status);
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200439 mthca_free_icm(dev, table->icm[i], table->coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
442 kfree(table);
443}
444
Roland Dreier56483ec2005-07-07 17:57:16 -0700445static u64 mthca_uarc_virt(struct mthca_dev *dev, struct mthca_uar *uar, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 return dev->uar_table.uarc_base +
Roland Dreier56483ec2005-07-07 17:57:16 -0700448 uar->index * dev->uar_table.uarc_size +
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800449 page * MTHCA_ICM_PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Roland Dreier56483ec2005-07-07 17:57:16 -0700452int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
453 struct mthca_user_db_table *db_tab, int index, u64 uaddr)
454{
Jens Axboe45711f12007-10-22 21:19:53 +0200455 struct page *pages[1];
Roland Dreier56483ec2005-07-07 17:57:16 -0700456 int ret = 0;
457 u8 status;
458 int i;
459
460 if (!mthca_is_memfree(dev))
461 return 0;
462
463 if (index < 0 || index > dev->uar_table.uarc_size / 8)
464 return -EINVAL;
465
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800466 mutex_lock(&db_tab->mutex);
Roland Dreier56483ec2005-07-07 17:57:16 -0700467
468 i = index / MTHCA_DB_REC_PER_PAGE;
469
470 if ((db_tab->page[i].refcount >= MTHCA_DB_REC_PER_PAGE) ||
471 (db_tab->page[i].uvirt && db_tab->page[i].uvirt != uaddr) ||
472 (uaddr & 4095)) {
473 ret = -EINVAL;
474 goto out;
475 }
476
477 if (db_tab->page[i].refcount) {
478 ++db_tab->page[i].refcount;
479 goto out;
480 }
481
482 ret = get_user_pages(current, current->mm, uaddr & PAGE_MASK, 1, 1, 0,
Jens Axboe45711f12007-10-22 21:19:53 +0200483 pages, NULL);
Roland Dreier56483ec2005-07-07 17:57:16 -0700484 if (ret < 0)
485 goto out;
486
Jens Axboe642f1492007-10-24 11:20:47 +0200487 sg_set_page(&db_tab->page[i].mem, pages[0], MTHCA_ICM_PAGE_SIZE,
488 uaddr & ~PAGE_MASK);
Roland Dreier56483ec2005-07-07 17:57:16 -0700489
490 ret = pci_map_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
491 if (ret < 0) {
Jens Axboe45711f12007-10-22 21:19:53 +0200492 put_page(pages[0]);
Roland Dreier56483ec2005-07-07 17:57:16 -0700493 goto out;
494 }
495
496 ret = mthca_MAP_ICM_page(dev, sg_dma_address(&db_tab->page[i].mem),
497 mthca_uarc_virt(dev, uar, i), &status);
498 if (!ret && status)
499 ret = -EINVAL;
500 if (ret) {
501 pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
Jens Axboe45711f12007-10-22 21:19:53 +0200502 put_page(sg_page(&db_tab->page[i].mem));
Roland Dreier56483ec2005-07-07 17:57:16 -0700503 goto out;
504 }
505
506 db_tab->page[i].uvirt = uaddr;
507 db_tab->page[i].refcount = 1;
508
509out:
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800510 mutex_unlock(&db_tab->mutex);
Roland Dreier56483ec2005-07-07 17:57:16 -0700511 return ret;
512}
513
514void mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
515 struct mthca_user_db_table *db_tab, int index)
516{
517 if (!mthca_is_memfree(dev))
518 return;
519
520 /*
521 * To make our bookkeeping simpler, we don't unmap DB
522 * pages until we clean up the whole db table.
523 */
524
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800525 mutex_lock(&db_tab->mutex);
Roland Dreier56483ec2005-07-07 17:57:16 -0700526
527 --db_tab->page[index / MTHCA_DB_REC_PER_PAGE].refcount;
528
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800529 mutex_unlock(&db_tab->mutex);
Roland Dreier56483ec2005-07-07 17:57:16 -0700530}
531
532struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev)
533{
534 struct mthca_user_db_table *db_tab;
535 int npages;
536 int i;
537
538 if (!mthca_is_memfree(dev))
539 return NULL;
540
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800541 npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE;
Roland Dreier56483ec2005-07-07 17:57:16 -0700542 db_tab = kmalloc(sizeof *db_tab + npages * sizeof *db_tab->page, GFP_KERNEL);
543 if (!db_tab)
544 return ERR_PTR(-ENOMEM);
545
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800546 mutex_init(&db_tab->mutex);
Roland Dreier56483ec2005-07-07 17:57:16 -0700547 for (i = 0; i < npages; ++i) {
548 db_tab->page[i].refcount = 0;
549 db_tab->page[i].uvirt = 0;
Roland Dreierfe174352008-02-12 14:38:22 -0800550 sg_init_table(&db_tab->page[i].mem, 1);
Roland Dreier56483ec2005-07-07 17:57:16 -0700551 }
552
553 return db_tab;
554}
555
556void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,
557 struct mthca_user_db_table *db_tab)
558{
559 int i;
560 u8 status;
561
562 if (!mthca_is_memfree(dev))
563 return;
564
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800565 for (i = 0; i < dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE; ++i) {
Roland Dreier56483ec2005-07-07 17:57:16 -0700566 if (db_tab->page[i].uvirt) {
567 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, uar, i), 1, &status);
568 pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
Jens Axboe45711f12007-10-22 21:19:53 +0200569 put_page(sg_page(&db_tab->page[i].mem));
Roland Dreier56483ec2005-07-07 17:57:16 -0700570 }
571 }
Jack Morgenstein52d0df12005-12-09 13:48:50 -0800572
573 kfree(db_tab);
Roland Dreier56483ec2005-07-07 17:57:16 -0700574}
575
Roland Dreierc6f5cb72005-10-18 13:22:16 -0700576int mthca_alloc_db(struct mthca_dev *dev, enum mthca_db_type type,
577 u32 qn, __be32 **db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 int group;
580 int start, end, dir;
581 int i, j;
582 struct mthca_db_page *page;
583 int ret = 0;
584 u8 status;
585
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800586 mutex_lock(&dev->db_tab->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 switch (type) {
589 case MTHCA_DB_TYPE_CQ_ARM:
590 case MTHCA_DB_TYPE_SQ:
591 group = 0;
592 start = 0;
593 end = dev->db_tab->max_group1;
594 dir = 1;
595 break;
596
597 case MTHCA_DB_TYPE_CQ_SET_CI:
598 case MTHCA_DB_TYPE_RQ:
599 case MTHCA_DB_TYPE_SRQ:
600 group = 1;
601 start = dev->db_tab->npages - 1;
602 end = dev->db_tab->min_group2;
603 dir = -1;
604 break;
605
606 default:
Roland Dreier2714eb52005-04-16 15:26:20 -0700607 ret = -EINVAL;
608 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
611 for (i = start; i != end; i += dir)
612 if (dev->db_tab->page[i].db_rec &&
613 !bitmap_full(dev->db_tab->page[i].used,
614 MTHCA_DB_REC_PER_PAGE)) {
615 page = dev->db_tab->page + i;
616 goto found;
617 }
618
Roland Dreier018771f2005-09-21 21:40:12 -0700619 for (i = start; i != end; i += dir)
620 if (!dev->db_tab->page[i].db_rec) {
621 page = dev->db_tab->page + i;
622 goto alloc;
623 }
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (dev->db_tab->max_group1 >= dev->db_tab->min_group2 - 1) {
626 ret = -ENOMEM;
627 goto out;
628 }
629
Roland Dreier018771f2005-09-21 21:40:12 -0700630 if (group == 0)
631 ++dev->db_tab->max_group1;
632 else
633 --dev->db_tab->min_group2;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 page = dev->db_tab->page + end;
Roland Dreier018771f2005-09-21 21:40:12 -0700636
637alloc:
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800638 page->db_rec = dma_alloc_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 &page->mapping, GFP_KERNEL);
640 if (!page->db_rec) {
641 ret = -ENOMEM;
642 goto out;
643 }
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800644 memset(page->db_rec, 0, MTHCA_ICM_PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Roland Dreier56483ec2005-07-07 17:57:16 -0700646 ret = mthca_MAP_ICM_page(dev, page->mapping,
647 mthca_uarc_virt(dev, &dev->driver_uar, i), &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (!ret && status)
649 ret = -EINVAL;
650 if (ret) {
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800651 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 page->db_rec, page->mapping);
653 goto out;
654 }
655
656 bitmap_zero(page->used, MTHCA_DB_REC_PER_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658found:
659 j = find_first_zero_bit(page->used, MTHCA_DB_REC_PER_PAGE);
660 set_bit(j, page->used);
661
662 if (group == 1)
663 j = MTHCA_DB_REC_PER_PAGE - 1 - j;
664
665 ret = i * MTHCA_DB_REC_PER_PAGE + j;
666
667 page->db_rec[j] = cpu_to_be64((qn << 8) | (type << 5));
668
Sean Hefty97f52eb2005-08-13 21:05:57 -0700669 *db = (__be32 *) &page->db_rec[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671out:
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800672 mutex_unlock(&dev->db_tab->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 return ret;
675}
676
677void mthca_free_db(struct mthca_dev *dev, int type, int db_index)
678{
679 int i, j;
680 struct mthca_db_page *page;
681 u8 status;
682
683 i = db_index / MTHCA_DB_REC_PER_PAGE;
684 j = db_index % MTHCA_DB_REC_PER_PAGE;
685
686 page = dev->db_tab->page + i;
687
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800688 mutex_lock(&dev->db_tab->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 page->db_rec[j] = 0;
691 if (i >= dev->db_tab->min_group2)
692 j = MTHCA_DB_REC_PER_PAGE - 1 - j;
693 clear_bit(j, page->used);
694
695 if (bitmap_empty(page->used, MTHCA_DB_REC_PER_PAGE) &&
696 i >= dev->db_tab->max_group1 - 1) {
Roland Dreier56483ec2005-07-07 17:57:16 -0700697 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800699 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 page->db_rec, page->mapping);
701 page->db_rec = NULL;
702
703 if (i == dev->db_tab->max_group1) {
704 --dev->db_tab->max_group1;
705 /* XXX may be able to unmap more pages now */
706 }
707 if (i == dev->db_tab->min_group2)
708 ++dev->db_tab->min_group2;
709 }
710
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800711 mutex_unlock(&dev->db_tab->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714int mthca_init_db_tab(struct mthca_dev *dev)
715{
716 int i;
717
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700718 if (!mthca_is_memfree(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return 0;
720
721 dev->db_tab = kmalloc(sizeof *dev->db_tab, GFP_KERNEL);
722 if (!dev->db_tab)
723 return -ENOMEM;
724
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800725 mutex_init(&dev->db_tab->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800727 dev->db_tab->npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 dev->db_tab->max_group1 = 0;
729 dev->db_tab->min_group2 = dev->db_tab->npages - 1;
730
731 dev->db_tab->page = kmalloc(dev->db_tab->npages *
732 sizeof *dev->db_tab->page,
733 GFP_KERNEL);
734 if (!dev->db_tab->page) {
735 kfree(dev->db_tab);
736 return -ENOMEM;
737 }
738
739 for (i = 0; i < dev->db_tab->npages; ++i)
740 dev->db_tab->page[i].db_rec = NULL;
741
742 return 0;
743}
744
745void mthca_cleanup_db_tab(struct mthca_dev *dev)
746{
747 int i;
748 u8 status;
749
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700750 if (!mthca_is_memfree(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return;
752
753 /*
754 * Because we don't always free our UARC pages when they
755 * become empty to make mthca_free_db() simpler we need to
756 * make a sweep through the doorbell pages and free any
757 * leftover pages now.
758 */
759 for (i = 0; i < dev->db_tab->npages; ++i) {
760 if (!dev->db_tab->page[i].db_rec)
761 continue;
762
763 if (!bitmap_empty(dev->db_tab->page[i].used, MTHCA_DB_REC_PER_PAGE))
764 mthca_warn(dev, "Kernel UARC page %d not empty\n", i);
765
Roland Dreier56483ec2005-07-07 17:57:16 -0700766 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800768 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 dev->db_tab->page[i].db_rec,
770 dev->db_tab->page[i].mapping);
771 }
772
773 kfree(dev->db_tab->page);
774 kfree(dev->db_tab);
775}