Pull bugzilla-7897 into release branch
[linux-2.6] / drivers / infiniband / hw / mthca / mthca_memfree.c
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Cisco Systems.  All rights reserved.
4  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5  *
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
37 #include <linux/mm.h>
38 #include <linux/scatterlist.h>
39
40 #include <asm/page.h>
41
42 #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  */
50 enum {
51         MTHCA_ICM_ALLOC_SIZE   = 1 << 18,
52         MTHCA_TABLE_CHUNK_SIZE = 1 << 18
53 };
54
55 struct mthca_user_db_table {
56         struct mutex mutex;
57         struct {
58                 u64                uvirt;
59                 struct scatterlist mem;
60                 int                refcount;
61         }                page[0];
62 };
63
64 static 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)
73                 __free_pages(chunk->mem[i].page,
74                              get_order(chunk->mem[i].length));
75 }
76
77 static 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,
83                                   lowmem_page_address(chunk->mem[i].page),
84                                   sg_dma_address(&chunk->mem[i]));
85         }
86 }
87
88 void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm, int coherent)
89 {
90         struct mthca_icm_chunk *chunk, *tmp;
91
92         if (!icm)
93                 return;
94
95         list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
96                 if (coherent)
97                         mthca_free_icm_coherent(dev, chunk);
98                 else
99                         mthca_free_icm_pages(dev, chunk);
100
101                 kfree(chunk);
102         }
103
104         kfree(icm);
105 }
106
107 static int mthca_alloc_icm_pages(struct scatterlist *mem, int order, gfp_t gfp_mask)
108 {
109         mem->page = alloc_pages(gfp_mask, order);
110         if (!mem->page)
111                 return -ENOMEM;
112
113         mem->length = PAGE_SIZE << order;
114         mem->offset = 0;
115         return 0;
116 }
117
118 static int mthca_alloc_icm_coherent(struct device *dev, struct scatterlist *mem,
119                                     int order, gfp_t gfp_mask)
120 {
121         void *buf = dma_alloc_coherent(dev, PAGE_SIZE << order, &sg_dma_address(mem),
122                                        gfp_mask);
123         if (!buf)
124                 return -ENOMEM;
125
126         sg_set_buf(mem, buf, PAGE_SIZE << order);
127         BUG_ON(mem->offset);
128         sg_dma_len(mem) = PAGE_SIZE << order;
129         return 0;
130 }
131
132 struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
133                                   gfp_t gfp_mask, int coherent)
134 {
135         struct mthca_icm *icm;
136         struct mthca_icm_chunk *chunk = NULL;
137         int cur_order;
138         int ret;
139
140         /* We use sg_set_buf for coherent allocs, which assumes low memory */
141         BUG_ON(coherent && (gfp_mask & __GFP_HIGHMEM));
142
143         icm = kmalloc(sizeof *icm, gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
144         if (!icm)
145                 return icm;
146
147         icm->refcount = 0;
148         INIT_LIST_HEAD(&icm->chunk_list);
149
150         cur_order = get_order(MTHCA_ICM_ALLOC_SIZE);
151
152         while (npages > 0) {
153                 if (!chunk) {
154                         chunk = kmalloc(sizeof *chunk,
155                                         gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
156                         if (!chunk)
157                                 goto fail;
158
159                         chunk->npages = 0;
160                         chunk->nsg    = 0;
161                         list_add_tail(&chunk->list, &icm->chunk_list);
162                 }
163
164                 while (1 << cur_order > npages)
165                         --cur_order;
166
167                 if (coherent)
168                         ret = mthca_alloc_icm_coherent(&dev->pdev->dev,
169                                                        &chunk->mem[chunk->npages],
170                                                        cur_order, gfp_mask);
171                 else
172                         ret = mthca_alloc_icm_pages(&chunk->mem[chunk->npages],
173                                                     cur_order, gfp_mask);
174
175                 if (!ret) {
176                         ++chunk->npages;
177
178                         if (!coherent && chunk->npages == MTHCA_ICM_CHUNK_LEN) {
179                                 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
180                                                         chunk->npages,
181                                                         PCI_DMA_BIDIRECTIONAL);
182
183                                 if (chunk->nsg <= 0)
184                                         goto fail;
185                         }
186
187                         if (chunk->npages == MTHCA_ICM_CHUNK_LEN)
188                                 chunk = NULL;
189
190                         npages -= 1 << cur_order;
191                 } else {
192                         --cur_order;
193                         if (cur_order < 0)
194                                 goto fail;
195                 }
196         }
197
198         if (!coherent && chunk) {
199                 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
200                                         chunk->npages,
201                                         PCI_DMA_BIDIRECTIONAL);
202
203                 if (chunk->nsg <= 0)
204                         goto fail;
205         }
206
207         return icm;
208
209 fail:
210         mthca_free_icm(dev, icm, coherent);
211         return NULL;
212 }
213
214 int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
215 {
216         int i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
217         int ret = 0;
218         u8 status;
219
220         mutex_lock(&table->mutex);
221
222         if (table->icm[i]) {
223                 ++table->icm[i]->refcount;
224                 goto out;
225         }
226
227         table->icm[i] = mthca_alloc_icm(dev, MTHCA_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
228                                         (table->lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
229                                         __GFP_NOWARN, table->coherent);
230         if (!table->icm[i]) {
231                 ret = -ENOMEM;
232                 goto out;
233         }
234
235         if (mthca_MAP_ICM(dev, table->icm[i], table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
236                           &status) || status) {
237                 mthca_free_icm(dev, table->icm[i], table->coherent);
238                 table->icm[i] = NULL;
239                 ret = -ENOMEM;
240                 goto out;
241         }
242
243         ++table->icm[i]->refcount;
244
245 out:
246         mutex_unlock(&table->mutex);
247         return ret;
248 }
249
250 void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
251 {
252         int i;
253         u8 status;
254
255         if (!mthca_is_memfree(dev))
256                 return;
257
258         i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
259
260         mutex_lock(&table->mutex);
261
262         if (--table->icm[i]->refcount == 0) {
263                 mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
264                                 MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
265                                 &status);
266                 mthca_free_icm(dev, table->icm[i], table->coherent);
267                 table->icm[i] = NULL;
268         }
269
270         mutex_unlock(&table->mutex);
271 }
272
273 void *mthca_table_find(struct mthca_icm_table *table, int obj, dma_addr_t *dma_handle)
274 {
275         int idx, offset, dma_offset, i;
276         struct mthca_icm_chunk *chunk;
277         struct mthca_icm *icm;
278         struct page *page = NULL;
279
280         if (!table->lowmem)
281                 return NULL;
282
283         mutex_lock(&table->mutex);
284
285         idx = (obj & (table->num_obj - 1)) * table->obj_size;
286         icm = table->icm[idx / MTHCA_TABLE_CHUNK_SIZE];
287         dma_offset = offset = idx % MTHCA_TABLE_CHUNK_SIZE;
288
289         if (!icm)
290                 goto out;
291
292         list_for_each_entry(chunk, &icm->chunk_list, list) {
293                 for (i = 0; i < chunk->npages; ++i) {
294                         if (dma_handle && dma_offset >= 0) {
295                                 if (sg_dma_len(&chunk->mem[i]) > dma_offset)
296                                         *dma_handle = sg_dma_address(&chunk->mem[i]) +
297                                                 dma_offset;
298                                 dma_offset -= sg_dma_len(&chunk->mem[i]);
299                         }
300                         /* DMA mapping can merge pages but not split them,
301                          * so if we found the page, dma_handle has already
302                          * been assigned to. */
303                         if (chunk->mem[i].length > offset) {
304                                 page = chunk->mem[i].page;
305                                 goto out;
306                         }
307                         offset -= chunk->mem[i].length;
308                 }
309         }
310
311 out:
312         mutex_unlock(&table->mutex);
313         return page ? lowmem_page_address(page) + offset : NULL;
314 }
315
316 int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,
317                           int start, int end)
318 {
319         int inc = MTHCA_TABLE_CHUNK_SIZE / table->obj_size;
320         int i, err;
321
322         for (i = start; i <= end; i += inc) {
323                 err = mthca_table_get(dev, table, i);
324                 if (err)
325                         goto fail;
326         }
327
328         return 0;
329
330 fail:
331         while (i > start) {
332                 i -= inc;
333                 mthca_table_put(dev, table, i);
334         }
335
336         return err;
337 }
338
339 void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,
340                            int start, int end)
341 {
342         int i;
343
344         if (!mthca_is_memfree(dev))
345                 return;
346
347         for (i = start; i <= end; i += MTHCA_TABLE_CHUNK_SIZE / table->obj_size)
348                 mthca_table_put(dev, table, i);
349 }
350
351 struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
352                                               u64 virt, int obj_size,
353                                               int nobj, int reserved,
354                                               int use_lowmem, int use_coherent)
355 {
356         struct mthca_icm_table *table;
357         int num_icm;
358         unsigned chunk_size;
359         int i;
360         u8 status;
361
362         num_icm = (obj_size * nobj + MTHCA_TABLE_CHUNK_SIZE - 1) / MTHCA_TABLE_CHUNK_SIZE;
363
364         table = kmalloc(sizeof *table + num_icm * sizeof *table->icm, GFP_KERNEL);
365         if (!table)
366                 return NULL;
367
368         table->virt     = virt;
369         table->num_icm  = num_icm;
370         table->num_obj  = nobj;
371         table->obj_size = obj_size;
372         table->lowmem   = use_lowmem;
373         table->coherent = use_coherent;
374         mutex_init(&table->mutex);
375
376         for (i = 0; i < num_icm; ++i)
377                 table->icm[i] = NULL;
378
379         for (i = 0; i * MTHCA_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) {
380                 chunk_size = MTHCA_TABLE_CHUNK_SIZE;
381                 if ((i + 1) * MTHCA_TABLE_CHUNK_SIZE > nobj * obj_size)
382                         chunk_size = nobj * obj_size - i * MTHCA_TABLE_CHUNK_SIZE;
383
384                 table->icm[i] = mthca_alloc_icm(dev, chunk_size >> PAGE_SHIFT,
385                                                 (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
386                                                 __GFP_NOWARN, use_coherent);
387                 if (!table->icm[i])
388                         goto err;
389                 if (mthca_MAP_ICM(dev, table->icm[i], virt + i * MTHCA_TABLE_CHUNK_SIZE,
390                                   &status) || status) {
391                         mthca_free_icm(dev, table->icm[i], table->coherent);
392                         table->icm[i] = NULL;
393                         goto err;
394                 }
395
396                 /*
397                  * Add a reference to this ICM chunk so that it never
398                  * gets freed (since it contains reserved firmware objects).
399                  */
400                 ++table->icm[i]->refcount;
401         }
402
403         return table;
404
405 err:
406         for (i = 0; i < num_icm; ++i)
407                 if (table->icm[i]) {
408                         mthca_UNMAP_ICM(dev, virt + i * MTHCA_TABLE_CHUNK_SIZE,
409                                         MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
410                                         &status);
411                         mthca_free_icm(dev, table->icm[i], table->coherent);
412                 }
413
414         kfree(table);
415
416         return NULL;
417 }
418
419 void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table)
420 {
421         int i;
422         u8 status;
423
424         for (i = 0; i < table->num_icm; ++i)
425                 if (table->icm[i]) {
426                         mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
427                                         MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
428                                         &status);
429                         mthca_free_icm(dev, table->icm[i], table->coherent);
430                 }
431
432         kfree(table);
433 }
434
435 static u64 mthca_uarc_virt(struct mthca_dev *dev, struct mthca_uar *uar, int page)
436 {
437         return dev->uar_table.uarc_base +
438                 uar->index * dev->uar_table.uarc_size +
439                 page * MTHCA_ICM_PAGE_SIZE;
440 }
441
442 int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
443                       struct mthca_user_db_table *db_tab, int index, u64 uaddr)
444 {
445         int ret = 0;
446         u8 status;
447         int i;
448
449         if (!mthca_is_memfree(dev))
450                 return 0;
451
452         if (index < 0 || index > dev->uar_table.uarc_size / 8)
453                 return -EINVAL;
454
455         mutex_lock(&db_tab->mutex);
456
457         i = index / MTHCA_DB_REC_PER_PAGE;
458
459         if ((db_tab->page[i].refcount >= MTHCA_DB_REC_PER_PAGE)       ||
460             (db_tab->page[i].uvirt && db_tab->page[i].uvirt != uaddr) ||
461             (uaddr & 4095)) {
462                 ret = -EINVAL;
463                 goto out;
464         }
465
466         if (db_tab->page[i].refcount) {
467                 ++db_tab->page[i].refcount;
468                 goto out;
469         }
470
471         ret = get_user_pages(current, current->mm, uaddr & PAGE_MASK, 1, 1, 0,
472                              &db_tab->page[i].mem.page, NULL);
473         if (ret < 0)
474                 goto out;
475
476         db_tab->page[i].mem.length = MTHCA_ICM_PAGE_SIZE;
477         db_tab->page[i].mem.offset = uaddr & ~PAGE_MASK;
478
479         ret = pci_map_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
480         if (ret < 0) {
481                 put_page(db_tab->page[i].mem.page);
482                 goto out;
483         }
484
485         ret = mthca_MAP_ICM_page(dev, sg_dma_address(&db_tab->page[i].mem),
486                                  mthca_uarc_virt(dev, uar, i), &status);
487         if (!ret && status)
488                 ret = -EINVAL;
489         if (ret) {
490                 pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
491                 put_page(db_tab->page[i].mem.page);
492                 goto out;
493         }
494
495         db_tab->page[i].uvirt    = uaddr;
496         db_tab->page[i].refcount = 1;
497
498 out:
499         mutex_unlock(&db_tab->mutex);
500         return ret;
501 }
502
503 void mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
504                          struct mthca_user_db_table *db_tab, int index)
505 {
506         if (!mthca_is_memfree(dev))
507                 return;
508
509         /*
510          * To make our bookkeeping simpler, we don't unmap DB
511          * pages until we clean up the whole db table.
512          */
513
514         mutex_lock(&db_tab->mutex);
515
516         --db_tab->page[index / MTHCA_DB_REC_PER_PAGE].refcount;
517
518         mutex_unlock(&db_tab->mutex);
519 }
520
521 struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev)
522 {
523         struct mthca_user_db_table *db_tab;
524         int npages;
525         int i;
526
527         if (!mthca_is_memfree(dev))
528                 return NULL;
529
530         npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE;
531         db_tab = kmalloc(sizeof *db_tab + npages * sizeof *db_tab->page, GFP_KERNEL);
532         if (!db_tab)
533                 return ERR_PTR(-ENOMEM);
534
535         mutex_init(&db_tab->mutex);
536         for (i = 0; i < npages; ++i) {
537                 db_tab->page[i].refcount = 0;
538                 db_tab->page[i].uvirt    = 0;
539         }
540
541         return db_tab;
542 }
543
544 void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,
545                                struct mthca_user_db_table *db_tab)
546 {
547         int i;
548         u8 status;
549
550         if (!mthca_is_memfree(dev))
551                 return;
552
553         for (i = 0; i < dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE; ++i) {
554                 if (db_tab->page[i].uvirt) {
555                         mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, uar, i), 1, &status);
556                         pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
557                         put_page(db_tab->page[i].mem.page);
558                 }
559         }
560
561         kfree(db_tab);
562 }
563
564 int mthca_alloc_db(struct mthca_dev *dev, enum mthca_db_type type,
565                    u32 qn, __be32 **db)
566 {
567         int group;
568         int start, end, dir;
569         int i, j;
570         struct mthca_db_page *page;
571         int ret = 0;
572         u8 status;
573
574         mutex_lock(&dev->db_tab->mutex);
575
576         switch (type) {
577         case MTHCA_DB_TYPE_CQ_ARM:
578         case MTHCA_DB_TYPE_SQ:
579                 group = 0;
580                 start = 0;
581                 end   = dev->db_tab->max_group1;
582                 dir   = 1;
583                 break;
584
585         case MTHCA_DB_TYPE_CQ_SET_CI:
586         case MTHCA_DB_TYPE_RQ:
587         case MTHCA_DB_TYPE_SRQ:
588                 group = 1;
589                 start = dev->db_tab->npages - 1;
590                 end   = dev->db_tab->min_group2;
591                 dir   = -1;
592                 break;
593
594         default:
595                 ret = -EINVAL;
596                 goto out;
597         }
598
599         for (i = start; i != end; i += dir)
600                 if (dev->db_tab->page[i].db_rec &&
601                     !bitmap_full(dev->db_tab->page[i].used,
602                                  MTHCA_DB_REC_PER_PAGE)) {
603                         page = dev->db_tab->page + i;
604                         goto found;
605                 }
606
607         for (i = start; i != end; i += dir)
608                 if (!dev->db_tab->page[i].db_rec) {
609                         page = dev->db_tab->page + i;
610                         goto alloc;
611                 }
612
613         if (dev->db_tab->max_group1 >= dev->db_tab->min_group2 - 1) {
614                 ret = -ENOMEM;
615                 goto out;
616         }
617
618         if (group == 0)
619                 ++dev->db_tab->max_group1;
620         else
621                 --dev->db_tab->min_group2;
622
623         page = dev->db_tab->page + end;
624
625 alloc:
626         page->db_rec = dma_alloc_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
627                                           &page->mapping, GFP_KERNEL);
628         if (!page->db_rec) {
629                 ret = -ENOMEM;
630                 goto out;
631         }
632         memset(page->db_rec, 0, MTHCA_ICM_PAGE_SIZE);
633
634         ret = mthca_MAP_ICM_page(dev, page->mapping,
635                                  mthca_uarc_virt(dev, &dev->driver_uar, i), &status);
636         if (!ret && status)
637                 ret = -EINVAL;
638         if (ret) {
639                 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
640                                   page->db_rec, page->mapping);
641                 goto out;
642         }
643
644         bitmap_zero(page->used, MTHCA_DB_REC_PER_PAGE);
645
646 found:
647         j = find_first_zero_bit(page->used, MTHCA_DB_REC_PER_PAGE);
648         set_bit(j, page->used);
649
650         if (group == 1)
651                 j = MTHCA_DB_REC_PER_PAGE - 1 - j;
652
653         ret = i * MTHCA_DB_REC_PER_PAGE + j;
654
655         page->db_rec[j] = cpu_to_be64((qn << 8) | (type << 5));
656
657         *db = (__be32 *) &page->db_rec[j];
658
659 out:
660         mutex_unlock(&dev->db_tab->mutex);
661
662         return ret;
663 }
664
665 void mthca_free_db(struct mthca_dev *dev, int type, int db_index)
666 {
667         int i, j;
668         struct mthca_db_page *page;
669         u8 status;
670
671         i = db_index / MTHCA_DB_REC_PER_PAGE;
672         j = db_index % MTHCA_DB_REC_PER_PAGE;
673
674         page = dev->db_tab->page + i;
675
676         mutex_lock(&dev->db_tab->mutex);
677
678         page->db_rec[j] = 0;
679         if (i >= dev->db_tab->min_group2)
680                 j = MTHCA_DB_REC_PER_PAGE - 1 - j;
681         clear_bit(j, page->used);
682
683         if (bitmap_empty(page->used, MTHCA_DB_REC_PER_PAGE) &&
684             i >= dev->db_tab->max_group1 - 1) {
685                 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1, &status);
686
687                 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
688                                   page->db_rec, page->mapping);
689                 page->db_rec = NULL;
690
691                 if (i == dev->db_tab->max_group1) {
692                         --dev->db_tab->max_group1;
693                         /* XXX may be able to unmap more pages now */
694                 }
695                 if (i == dev->db_tab->min_group2)
696                         ++dev->db_tab->min_group2;
697         }
698
699         mutex_unlock(&dev->db_tab->mutex);
700 }
701
702 int mthca_init_db_tab(struct mthca_dev *dev)
703 {
704         int i;
705
706         if (!mthca_is_memfree(dev))
707                 return 0;
708
709         dev->db_tab = kmalloc(sizeof *dev->db_tab, GFP_KERNEL);
710         if (!dev->db_tab)
711                 return -ENOMEM;
712
713         mutex_init(&dev->db_tab->mutex);
714
715         dev->db_tab->npages     = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE;
716         dev->db_tab->max_group1 = 0;
717         dev->db_tab->min_group2 = dev->db_tab->npages - 1;
718
719         dev->db_tab->page = kmalloc(dev->db_tab->npages *
720                                     sizeof *dev->db_tab->page,
721                                     GFP_KERNEL);
722         if (!dev->db_tab->page) {
723                 kfree(dev->db_tab);
724                 return -ENOMEM;
725         }
726
727         for (i = 0; i < dev->db_tab->npages; ++i)
728                 dev->db_tab->page[i].db_rec = NULL;
729
730         return 0;
731 }
732
733 void mthca_cleanup_db_tab(struct mthca_dev *dev)
734 {
735         int i;
736         u8 status;
737
738         if (!mthca_is_memfree(dev))
739                 return;
740
741         /*
742          * Because we don't always free our UARC pages when they
743          * become empty to make mthca_free_db() simpler we need to
744          * make a sweep through the doorbell pages and free any
745          * leftover pages now.
746          */
747         for (i = 0; i < dev->db_tab->npages; ++i) {
748                 if (!dev->db_tab->page[i].db_rec)
749                         continue;
750
751                 if (!bitmap_empty(dev->db_tab->page[i].used, MTHCA_DB_REC_PER_PAGE))
752                         mthca_warn(dev, "Kernel UARC page %d not empty\n", i);
753
754                 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1, &status);
755
756                 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
757                                   dev->db_tab->page[i].db_rec,
758                                   dev->db_tab->page[i].mapping);
759         }
760
761         kfree(dev->db_tab->page);
762         kfree(dev->db_tab);
763 }