blob: 1f4d27d7c16d9d5f8741364e6d7b613ca8ba17af [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.
33 *
34 * $Id$
35 */
36
Roland Dreierdbcf31b2005-05-01 08:59:14 -070037#include <linux/mm.h>
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +020038#include <linux/scatterlist.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040039#include <linux/sched.h>
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +020040
41#include <asm/page.h>
Roland Dreierdbcf31b2005-05-01 08:59:14 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "mthca_memfree.h"
44#include "mthca_dev.h"
45#include "mthca_cmd.h"
46
47/*
48 * We allocate in as big chunks as we can, up to a maximum of 256 KB
49 * per chunk.
50 */
51enum {
52 MTHCA_ICM_ALLOC_SIZE = 1 << 18,
53 MTHCA_TABLE_CHUNK_SIZE = 1 << 18
54};
55
Roland Dreier56483ec2005-07-07 17:57:16 -070056struct mthca_user_db_table {
Roland Dreierfd9cfdd2006-01-30 16:45:11 -080057 struct mutex mutex;
Roland Dreier56483ec2005-07-07 17:57:16 -070058 struct {
59 u64 uvirt;
60 struct scatterlist mem;
61 int refcount;
62 } page[0];
63};
64
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +020065static void mthca_free_icm_pages(struct mthca_dev *dev, struct mthca_icm_chunk *chunk)
66{
67 int i;
68
69 if (chunk->nsg > 0)
70 pci_unmap_sg(dev->pdev, chunk->mem, chunk->npages,
71 PCI_DMA_BIDIRECTIONAL);
72
73 for (i = 0; i < chunk->npages; ++i)
Jens Axboe45711f12007-10-22 21:19:53 +020074 __free_pages(sg_page(&chunk->mem[i]),
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +020075 get_order(chunk->mem[i].length));
76}
77
78static void mthca_free_icm_coherent(struct mthca_dev *dev, struct mthca_icm_chunk *chunk)
79{
80 int i;
81
82 for (i = 0; i < chunk->npages; ++i) {
83 dma_free_coherent(&dev->pdev->dev, chunk->mem[i].length,
Jens Axboe45711f12007-10-22 21:19:53 +020084 lowmem_page_address(sg_page(&chunk->mem[i])),
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +020085 sg_dma_address(&chunk->mem[i]));
86 }
87}
88
89void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm, int coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 struct mthca_icm_chunk *chunk, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 if (!icm)
94 return;
95
96 list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +020097 if (coherent)
98 mthca_free_icm_coherent(dev, chunk);
99 else
100 mthca_free_icm_pages(dev, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 kfree(chunk);
103 }
104
105 kfree(icm);
106}
107
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200108static int mthca_alloc_icm_pages(struct scatterlist *mem, int order, gfp_t gfp_mask)
109{
Jens Axboe45711f12007-10-22 21:19:53 +0200110 struct page *page;
111
112 page = alloc_pages(gfp_mask, order);
113 if (!page)
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200114 return -ENOMEM;
115
Jens Axboe642f1492007-10-24 11:20:47 +0200116 sg_set_page(mem, page, PAGE_SIZE << order, 0);
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200117 return 0;
118}
119
120static int mthca_alloc_icm_coherent(struct device *dev, struct scatterlist *mem,
121 int order, gfp_t gfp_mask)
122{
123 void *buf = dma_alloc_coherent(dev, PAGE_SIZE << order, &sg_dma_address(mem),
124 gfp_mask);
125 if (!buf)
126 return -ENOMEM;
127
128 sg_set_buf(mem, buf, PAGE_SIZE << order);
129 BUG_ON(mem->offset);
130 sg_dma_len(mem) = PAGE_SIZE << order;
131 return 0;
132}
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200135 gfp_t gfp_mask, int coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct mthca_icm *icm;
138 struct mthca_icm_chunk *chunk = NULL;
139 int cur_order;
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200140 int ret;
141
142 /* We use sg_set_buf for coherent allocs, which assumes low memory */
143 BUG_ON(coherent && (gfp_mask & __GFP_HIGHMEM));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 icm = kmalloc(sizeof *icm, gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
146 if (!icm)
147 return icm;
148
149 icm->refcount = 0;
150 INIT_LIST_HEAD(&icm->chunk_list);
151
152 cur_order = get_order(MTHCA_ICM_ALLOC_SIZE);
153
154 while (npages > 0) {
155 if (!chunk) {
156 chunk = kmalloc(sizeof *chunk,
157 gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
158 if (!chunk)
159 goto fail;
160
Jens Axboe45711f12007-10-22 21:19:53 +0200161 sg_init_table(chunk->mem, MTHCA_ICM_CHUNK_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 chunk->npages = 0;
163 chunk->nsg = 0;
164 list_add_tail(&chunk->list, &icm->chunk_list);
165 }
166
167 while (1 << cur_order > npages)
168 --cur_order;
169
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200170 if (coherent)
171 ret = mthca_alloc_icm_coherent(&dev->pdev->dev,
172 &chunk->mem[chunk->npages],
173 cur_order, gfp_mask);
174 else
175 ret = mthca_alloc_icm_pages(&chunk->mem[chunk->npages],
176 cur_order, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200178 if (!ret) {
179 ++chunk->npages;
180
Roland Dreier11282b32007-02-16 13:57:33 -0800181 if (coherent)
182 ++chunk->nsg;
183 else if (chunk->npages == MTHCA_ICM_CHUNK_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
185 chunk->npages,
186 PCI_DMA_BIDIRECTIONAL);
187
188 if (chunk->nsg <= 0)
189 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200192 if (chunk->npages == MTHCA_ICM_CHUNK_LEN)
193 chunk = NULL;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 npages -= 1 << cur_order;
196 } else {
197 --cur_order;
198 if (cur_order < 0)
199 goto fail;
200 }
201 }
202
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200203 if (!coherent && chunk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
205 chunk->npages,
206 PCI_DMA_BIDIRECTIONAL);
207
208 if (chunk->nsg <= 0)
209 goto fail;
210 }
211
212 return icm;
213
214fail:
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200215 mthca_free_icm(dev, icm, coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return NULL;
217}
218
219int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
220{
221 int i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
222 int ret = 0;
223 u8 status;
224
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800225 mutex_lock(&table->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 if (table->icm[i]) {
228 ++table->icm[i]->refcount;
229 goto out;
230 }
231
232 table->icm[i] = mthca_alloc_icm(dev, MTHCA_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
233 (table->lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200234 __GFP_NOWARN, table->coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (!table->icm[i]) {
236 ret = -ENOMEM;
237 goto out;
238 }
239
240 if (mthca_MAP_ICM(dev, table->icm[i], table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
241 &status) || status) {
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200242 mthca_free_icm(dev, table->icm[i], table->coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 table->icm[i] = NULL;
244 ret = -ENOMEM;
245 goto out;
246 }
247
248 ++table->icm[i]->refcount;
249
250out:
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800251 mutex_unlock(&table->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return ret;
253}
254
255void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
256{
Roland Dreiera03a5a62005-06-27 14:36:43 -0700257 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 u8 status;
259
Roland Dreiera03a5a62005-06-27 14:36:43 -0700260 if (!mthca_is_memfree(dev))
261 return;
262
263 i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
264
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800265 mutex_lock(&table->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if (--table->icm[i]->refcount == 0) {
268 mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800269 MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
270 &status);
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200271 mthca_free_icm(dev, table->icm[i], table->coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 table->icm[i] = NULL;
273 }
274
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800275 mutex_unlock(&table->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200278void *mthca_table_find(struct mthca_icm_table *table, int obj, dma_addr_t *dma_handle)
Michael S. Tsirkin0fabd9f2005-04-16 15:26:29 -0700279{
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200280 int idx, offset, dma_offset, i;
Michael S. Tsirkin0fabd9f2005-04-16 15:26:29 -0700281 struct mthca_icm_chunk *chunk;
282 struct mthca_icm *icm;
283 struct page *page = NULL;
284
285 if (!table->lowmem)
286 return NULL;
287
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800288 mutex_lock(&table->mutex);
Michael S. Tsirkin0fabd9f2005-04-16 15:26:29 -0700289
290 idx = (obj & (table->num_obj - 1)) * table->obj_size;
291 icm = table->icm[idx / MTHCA_TABLE_CHUNK_SIZE];
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200292 dma_offset = offset = idx % MTHCA_TABLE_CHUNK_SIZE;
Michael S. Tsirkin0fabd9f2005-04-16 15:26:29 -0700293
294 if (!icm)
295 goto out;
296
297 list_for_each_entry(chunk, &icm->chunk_list, list) {
298 for (i = 0; i < chunk->npages; ++i) {
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200299 if (dma_handle && dma_offset >= 0) {
300 if (sg_dma_len(&chunk->mem[i]) > dma_offset)
301 *dma_handle = sg_dma_address(&chunk->mem[i]) +
302 dma_offset;
303 dma_offset -= sg_dma_len(&chunk->mem[i]);
304 }
305 /* DMA mapping can merge pages but not split them,
306 * so if we found the page, dma_handle has already
307 * been assigned to. */
Michael S. Tsirkin46707e92007-01-03 14:46:30 +0200308 if (chunk->mem[i].length > offset) {
Jens Axboe45711f12007-10-22 21:19:53 +0200309 page = sg_page(&chunk->mem[i]);
Michael S. Tsirkin6c7d2a72005-12-15 13:55:50 -0800310 goto out;
Michael S. Tsirkin0fabd9f2005-04-16 15:26:29 -0700311 }
312 offset -= chunk->mem[i].length;
313 }
314 }
315
316out:
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800317 mutex_unlock(&table->mutex);
Michael S. Tsirkin0fabd9f2005-04-16 15:26:29 -0700318 return page ? lowmem_page_address(page) + offset : NULL;
319}
320
Roland Dreier86562a12005-04-16 15:26:13 -0700321int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,
322 int start, int end)
323{
324 int inc = MTHCA_TABLE_CHUNK_SIZE / table->obj_size;
325 int i, err;
326
327 for (i = start; i <= end; i += inc) {
328 err = mthca_table_get(dev, table, i);
329 if (err)
330 goto fail;
331 }
332
333 return 0;
334
335fail:
336 while (i > start) {
337 i -= inc;
338 mthca_table_put(dev, table, i);
339 }
340
341 return err;
342}
343
344void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,
345 int start, int end)
346{
347 int i;
348
Roland Dreiera03a5a62005-06-27 14:36:43 -0700349 if (!mthca_is_memfree(dev))
350 return;
351
Roland Dreier86562a12005-04-16 15:26:13 -0700352 for (i = start; i <= end; i += MTHCA_TABLE_CHUNK_SIZE / table->obj_size)
353 mthca_table_put(dev, table, i);
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
357 u64 virt, int obj_size,
358 int nobj, int reserved,
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200359 int use_lowmem, int use_coherent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct mthca_icm_table *table;
362 int num_icm;
Roland Dreierd20a4012005-08-19 10:36:11 -0700363 unsigned chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int i;
365 u8 status;
366
Michael S. Tsirkinf02b16b2005-09-26 21:12:26 -0700367 num_icm = (obj_size * nobj + MTHCA_TABLE_CHUNK_SIZE - 1) / MTHCA_TABLE_CHUNK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 table = kmalloc(sizeof *table + num_icm * sizeof *table->icm, GFP_KERNEL);
370 if (!table)
371 return NULL;
372
373 table->virt = virt;
374 table->num_icm = num_icm;
375 table->num_obj = nobj;
376 table->obj_size = obj_size;
377 table->lowmem = use_lowmem;
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200378 table->coherent = use_coherent;
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800379 mutex_init(&table->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 for (i = 0; i < num_icm; ++i)
382 table->icm[i] = NULL;
383
384 for (i = 0; i * MTHCA_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) {
Roland Dreierd20a4012005-08-19 10:36:11 -0700385 chunk_size = MTHCA_TABLE_CHUNK_SIZE;
386 if ((i + 1) * MTHCA_TABLE_CHUNK_SIZE > nobj * obj_size)
387 chunk_size = nobj * obj_size - i * MTHCA_TABLE_CHUNK_SIZE;
388
389 table->icm[i] = mthca_alloc_icm(dev, chunk_size >> PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200391 __GFP_NOWARN, use_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (!table->icm[i])
393 goto err;
394 if (mthca_MAP_ICM(dev, table->icm[i], virt + i * MTHCA_TABLE_CHUNK_SIZE,
395 &status) || status) {
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200396 mthca_free_icm(dev, table->icm[i], table->coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 table->icm[i] = NULL;
398 goto err;
399 }
400
401 /*
402 * Add a reference to this ICM chunk so that it never
403 * gets freed (since it contains reserved firmware objects).
404 */
405 ++table->icm[i]->refcount;
406 }
407
408 return table;
409
410err:
411 for (i = 0; i < num_icm; ++i)
412 if (table->icm[i]) {
413 mthca_UNMAP_ICM(dev, virt + i * MTHCA_TABLE_CHUNK_SIZE,
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800414 MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
415 &status);
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200416 mthca_free_icm(dev, table->icm[i], table->coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
419 kfree(table);
420
421 return NULL;
422}
423
424void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table)
425{
426 int i;
427 u8 status;
428
429 for (i = 0; i < table->num_icm; ++i)
430 if (table->icm[i]) {
431 mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800432 MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
433 &status);
Michael S. Tsirkin391e4de2007-02-10 23:15:08 +0200434 mthca_free_icm(dev, table->icm[i], table->coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
437 kfree(table);
438}
439
Roland Dreier56483ec2005-07-07 17:57:16 -0700440static u64 mthca_uarc_virt(struct mthca_dev *dev, struct mthca_uar *uar, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 return dev->uar_table.uarc_base +
Roland Dreier56483ec2005-07-07 17:57:16 -0700443 uar->index * dev->uar_table.uarc_size +
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800444 page * MTHCA_ICM_PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Roland Dreier56483ec2005-07-07 17:57:16 -0700447int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
448 struct mthca_user_db_table *db_tab, int index, u64 uaddr)
449{
Jens Axboe45711f12007-10-22 21:19:53 +0200450 struct page *pages[1];
Roland Dreier56483ec2005-07-07 17:57:16 -0700451 int ret = 0;
452 u8 status;
453 int i;
454
455 if (!mthca_is_memfree(dev))
456 return 0;
457
458 if (index < 0 || index > dev->uar_table.uarc_size / 8)
459 return -EINVAL;
460
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800461 mutex_lock(&db_tab->mutex);
Roland Dreier56483ec2005-07-07 17:57:16 -0700462
463 i = index / MTHCA_DB_REC_PER_PAGE;
464
465 if ((db_tab->page[i].refcount >= MTHCA_DB_REC_PER_PAGE) ||
466 (db_tab->page[i].uvirt && db_tab->page[i].uvirt != uaddr) ||
467 (uaddr & 4095)) {
468 ret = -EINVAL;
469 goto out;
470 }
471
472 if (db_tab->page[i].refcount) {
473 ++db_tab->page[i].refcount;
474 goto out;
475 }
476
477 ret = get_user_pages(current, current->mm, uaddr & PAGE_MASK, 1, 1, 0,
Jens Axboe45711f12007-10-22 21:19:53 +0200478 pages, NULL);
Roland Dreier56483ec2005-07-07 17:57:16 -0700479 if (ret < 0)
480 goto out;
481
Jens Axboe642f1492007-10-24 11:20:47 +0200482 sg_set_page(&db_tab->page[i].mem, pages[0], MTHCA_ICM_PAGE_SIZE,
483 uaddr & ~PAGE_MASK);
Roland Dreier56483ec2005-07-07 17:57:16 -0700484
485 ret = pci_map_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
486 if (ret < 0) {
Jens Axboe45711f12007-10-22 21:19:53 +0200487 put_page(pages[0]);
Roland Dreier56483ec2005-07-07 17:57:16 -0700488 goto out;
489 }
490
491 ret = mthca_MAP_ICM_page(dev, sg_dma_address(&db_tab->page[i].mem),
492 mthca_uarc_virt(dev, uar, i), &status);
493 if (!ret && status)
494 ret = -EINVAL;
495 if (ret) {
496 pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
Jens Axboe45711f12007-10-22 21:19:53 +0200497 put_page(sg_page(&db_tab->page[i].mem));
Roland Dreier56483ec2005-07-07 17:57:16 -0700498 goto out;
499 }
500
501 db_tab->page[i].uvirt = uaddr;
502 db_tab->page[i].refcount = 1;
503
504out:
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800505 mutex_unlock(&db_tab->mutex);
Roland Dreier56483ec2005-07-07 17:57:16 -0700506 return ret;
507}
508
509void mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
510 struct mthca_user_db_table *db_tab, int index)
511{
512 if (!mthca_is_memfree(dev))
513 return;
514
515 /*
516 * To make our bookkeeping simpler, we don't unmap DB
517 * pages until we clean up the whole db table.
518 */
519
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800520 mutex_lock(&db_tab->mutex);
Roland Dreier56483ec2005-07-07 17:57:16 -0700521
522 --db_tab->page[index / MTHCA_DB_REC_PER_PAGE].refcount;
523
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800524 mutex_unlock(&db_tab->mutex);
Roland Dreier56483ec2005-07-07 17:57:16 -0700525}
526
527struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev)
528{
529 struct mthca_user_db_table *db_tab;
530 int npages;
531 int i;
532
533 if (!mthca_is_memfree(dev))
534 return NULL;
535
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800536 npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE;
Roland Dreier56483ec2005-07-07 17:57:16 -0700537 db_tab = kmalloc(sizeof *db_tab + npages * sizeof *db_tab->page, GFP_KERNEL);
538 if (!db_tab)
539 return ERR_PTR(-ENOMEM);
540
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800541 mutex_init(&db_tab->mutex);
Roland Dreier56483ec2005-07-07 17:57:16 -0700542 for (i = 0; i < npages; ++i) {
543 db_tab->page[i].refcount = 0;
544 db_tab->page[i].uvirt = 0;
545 }
546
547 return db_tab;
548}
549
550void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,
551 struct mthca_user_db_table *db_tab)
552{
553 int i;
554 u8 status;
555
556 if (!mthca_is_memfree(dev))
557 return;
558
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800559 for (i = 0; i < dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE; ++i) {
Roland Dreier56483ec2005-07-07 17:57:16 -0700560 if (db_tab->page[i].uvirt) {
561 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, uar, i), 1, &status);
562 pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
Jens Axboe45711f12007-10-22 21:19:53 +0200563 put_page(sg_page(&db_tab->page[i].mem));
Roland Dreier56483ec2005-07-07 17:57:16 -0700564 }
565 }
Jack Morgenstein52d0df12005-12-09 13:48:50 -0800566
567 kfree(db_tab);
Roland Dreier56483ec2005-07-07 17:57:16 -0700568}
569
Roland Dreierc6f5cb72005-10-18 13:22:16 -0700570int mthca_alloc_db(struct mthca_dev *dev, enum mthca_db_type type,
571 u32 qn, __be32 **db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 int group;
574 int start, end, dir;
575 int i, j;
576 struct mthca_db_page *page;
577 int ret = 0;
578 u8 status;
579
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800580 mutex_lock(&dev->db_tab->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 switch (type) {
583 case MTHCA_DB_TYPE_CQ_ARM:
584 case MTHCA_DB_TYPE_SQ:
585 group = 0;
586 start = 0;
587 end = dev->db_tab->max_group1;
588 dir = 1;
589 break;
590
591 case MTHCA_DB_TYPE_CQ_SET_CI:
592 case MTHCA_DB_TYPE_RQ:
593 case MTHCA_DB_TYPE_SRQ:
594 group = 1;
595 start = dev->db_tab->npages - 1;
596 end = dev->db_tab->min_group2;
597 dir = -1;
598 break;
599
600 default:
Roland Dreier2714eb52005-04-16 15:26:20 -0700601 ret = -EINVAL;
602 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
605 for (i = start; i != end; i += dir)
606 if (dev->db_tab->page[i].db_rec &&
607 !bitmap_full(dev->db_tab->page[i].used,
608 MTHCA_DB_REC_PER_PAGE)) {
609 page = dev->db_tab->page + i;
610 goto found;
611 }
612
Roland Dreier018771f2005-09-21 21:40:12 -0700613 for (i = start; i != end; i += dir)
614 if (!dev->db_tab->page[i].db_rec) {
615 page = dev->db_tab->page + i;
616 goto alloc;
617 }
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (dev->db_tab->max_group1 >= dev->db_tab->min_group2 - 1) {
620 ret = -ENOMEM;
621 goto out;
622 }
623
Roland Dreier018771f2005-09-21 21:40:12 -0700624 if (group == 0)
625 ++dev->db_tab->max_group1;
626 else
627 --dev->db_tab->min_group2;
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 page = dev->db_tab->page + end;
Roland Dreier018771f2005-09-21 21:40:12 -0700630
631alloc:
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800632 page->db_rec = dma_alloc_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 &page->mapping, GFP_KERNEL);
634 if (!page->db_rec) {
635 ret = -ENOMEM;
636 goto out;
637 }
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800638 memset(page->db_rec, 0, MTHCA_ICM_PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Roland Dreier56483ec2005-07-07 17:57:16 -0700640 ret = mthca_MAP_ICM_page(dev, page->mapping,
641 mthca_uarc_virt(dev, &dev->driver_uar, i), &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (!ret && status)
643 ret = -EINVAL;
644 if (ret) {
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800645 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 page->db_rec, page->mapping);
647 goto out;
648 }
649
650 bitmap_zero(page->used, MTHCA_DB_REC_PER_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652found:
653 j = find_first_zero_bit(page->used, MTHCA_DB_REC_PER_PAGE);
654 set_bit(j, page->used);
655
656 if (group == 1)
657 j = MTHCA_DB_REC_PER_PAGE - 1 - j;
658
659 ret = i * MTHCA_DB_REC_PER_PAGE + j;
660
661 page->db_rec[j] = cpu_to_be64((qn << 8) | (type << 5));
662
Sean Hefty97f52eb2005-08-13 21:05:57 -0700663 *db = (__be32 *) &page->db_rec[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665out:
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800666 mutex_unlock(&dev->db_tab->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 return ret;
669}
670
671void mthca_free_db(struct mthca_dev *dev, int type, int db_index)
672{
673 int i, j;
674 struct mthca_db_page *page;
675 u8 status;
676
677 i = db_index / MTHCA_DB_REC_PER_PAGE;
678 j = db_index % MTHCA_DB_REC_PER_PAGE;
679
680 page = dev->db_tab->page + i;
681
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800682 mutex_lock(&dev->db_tab->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 page->db_rec[j] = 0;
685 if (i >= dev->db_tab->min_group2)
686 j = MTHCA_DB_REC_PER_PAGE - 1 - j;
687 clear_bit(j, page->used);
688
689 if (bitmap_empty(page->used, MTHCA_DB_REC_PER_PAGE) &&
690 i >= dev->db_tab->max_group1 - 1) {
Roland Dreier56483ec2005-07-07 17:57:16 -0700691 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800693 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 page->db_rec, page->mapping);
695 page->db_rec = NULL;
696
697 if (i == dev->db_tab->max_group1) {
698 --dev->db_tab->max_group1;
699 /* XXX may be able to unmap more pages now */
700 }
701 if (i == dev->db_tab->min_group2)
702 ++dev->db_tab->min_group2;
703 }
704
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800705 mutex_unlock(&dev->db_tab->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708int mthca_init_db_tab(struct mthca_dev *dev)
709{
710 int i;
711
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700712 if (!mthca_is_memfree(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 0;
714
715 dev->db_tab = kmalloc(sizeof *dev->db_tab, GFP_KERNEL);
716 if (!dev->db_tab)
717 return -ENOMEM;
718
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800719 mutex_init(&dev->db_tab->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800721 dev->db_tab->npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 dev->db_tab->max_group1 = 0;
723 dev->db_tab->min_group2 = dev->db_tab->npages - 1;
724
725 dev->db_tab->page = kmalloc(dev->db_tab->npages *
726 sizeof *dev->db_tab->page,
727 GFP_KERNEL);
728 if (!dev->db_tab->page) {
729 kfree(dev->db_tab);
730 return -ENOMEM;
731 }
732
733 for (i = 0; i < dev->db_tab->npages; ++i)
734 dev->db_tab->page[i].db_rec = NULL;
735
736 return 0;
737}
738
739void mthca_cleanup_db_tab(struct mthca_dev *dev)
740{
741 int i;
742 u8 status;
743
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700744 if (!mthca_is_memfree(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return;
746
747 /*
748 * Because we don't always free our UARC pages when they
749 * become empty to make mthca_free_db() simpler we need to
750 * make a sweep through the doorbell pages and free any
751 * leftover pages now.
752 */
753 for (i = 0; i < dev->db_tab->npages; ++i) {
754 if (!dev->db_tab->page[i].db_rec)
755 continue;
756
757 if (!bitmap_empty(dev->db_tab->page[i].used, MTHCA_DB_REC_PER_PAGE))
758 mthca_warn(dev, "Kernel UARC page %d not empty\n", i);
759
Roland Dreier56483ec2005-07-07 17:57:16 -0700760 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Ishai Rabinovitz8d3ef292006-03-01 22:33:11 -0800762 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 dev->db_tab->page[i].db_rec,
764 dev->db_tab->page[i].mapping);
765 }
766
767 kfree(dev->db_tab->page);
768 kfree(dev->db_tab);
769}