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.
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:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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.
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
36 #include <linux/scatterlist.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
42 #include "mthca_memfree.h"
43 #include "mthca_dev.h"
44 #include "mthca_cmd.h"
47 * We allocate in as big chunks as we can, up to a maximum of 256 KB
51 MTHCA_ICM_ALLOC_SIZE = 1 << 18,
52 MTHCA_TABLE_CHUNK_SIZE = 1 << 18
55 struct mthca_user_db_table {
59 struct scatterlist mem;
64 static void mthca_free_icm_pages(struct mthca_dev *dev, struct mthca_icm_chunk *chunk)
69 pci_unmap_sg(dev->pdev, chunk->mem, chunk->npages,
70 PCI_DMA_BIDIRECTIONAL);
72 for (i = 0; i < chunk->npages; ++i)
73 __free_pages(sg_page(&chunk->mem[i]),
74 get_order(chunk->mem[i].length));
77 static void mthca_free_icm_coherent(struct mthca_dev *dev, struct mthca_icm_chunk *chunk)
81 for (i = 0; i < chunk->npages; ++i) {
82 dma_free_coherent(&dev->pdev->dev, chunk->mem[i].length,
83 lowmem_page_address(sg_page(&chunk->mem[i])),
84 sg_dma_address(&chunk->mem[i]));
88 void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm, int coherent)
90 struct mthca_icm_chunk *chunk, *tmp;
95 list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
97 mthca_free_icm_coherent(dev, chunk);
99 mthca_free_icm_pages(dev, chunk);
107 static int mthca_alloc_icm_pages(struct scatterlist *mem, int order, gfp_t gfp_mask)
112 * Use __GFP_ZERO because buggy firmware assumes ICM pages are
113 * cleared, and subtle failures are seen if they aren't.
115 page = alloc_pages(gfp_mask | __GFP_ZERO, order);
119 sg_set_page(mem, page, PAGE_SIZE << order, 0);
123 static int mthca_alloc_icm_coherent(struct device *dev, struct scatterlist *mem,
124 int order, gfp_t gfp_mask)
126 void *buf = dma_alloc_coherent(dev, PAGE_SIZE << order, &sg_dma_address(mem),
131 sg_set_buf(mem, buf, PAGE_SIZE << order);
133 sg_dma_len(mem) = PAGE_SIZE << order;
137 struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
138 gfp_t gfp_mask, int coherent)
140 struct mthca_icm *icm;
141 struct mthca_icm_chunk *chunk = NULL;
145 /* We use sg_set_buf for coherent allocs, which assumes low memory */
146 BUG_ON(coherent && (gfp_mask & __GFP_HIGHMEM));
148 icm = kmalloc(sizeof *icm, gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
153 INIT_LIST_HEAD(&icm->chunk_list);
155 cur_order = get_order(MTHCA_ICM_ALLOC_SIZE);
159 chunk = kmalloc(sizeof *chunk,
160 gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
164 sg_init_table(chunk->mem, MTHCA_ICM_CHUNK_LEN);
167 list_add_tail(&chunk->list, &icm->chunk_list);
170 while (1 << cur_order > npages)
174 ret = mthca_alloc_icm_coherent(&dev->pdev->dev,
175 &chunk->mem[chunk->npages],
176 cur_order, gfp_mask);
178 ret = mthca_alloc_icm_pages(&chunk->mem[chunk->npages],
179 cur_order, gfp_mask);
186 else if (chunk->npages == MTHCA_ICM_CHUNK_LEN) {
187 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
189 PCI_DMA_BIDIRECTIONAL);
195 if (chunk->npages == MTHCA_ICM_CHUNK_LEN)
198 npages -= 1 << cur_order;
206 if (!coherent && chunk) {
207 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
209 PCI_DMA_BIDIRECTIONAL);
218 mthca_free_icm(dev, icm, coherent);
222 int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
224 int i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
227 mutex_lock(&table->mutex);
230 ++table->icm[i]->refcount;
234 table->icm[i] = mthca_alloc_icm(dev, MTHCA_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
235 (table->lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
236 __GFP_NOWARN, table->coherent);
237 if (!table->icm[i]) {
242 if (mthca_MAP_ICM(dev, table->icm[i],
243 table->virt + i * MTHCA_TABLE_CHUNK_SIZE)) {
244 mthca_free_icm(dev, table->icm[i], table->coherent);
245 table->icm[i] = NULL;
250 ++table->icm[i]->refcount;
253 mutex_unlock(&table->mutex);
257 void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
261 if (!mthca_is_memfree(dev))
264 i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
266 mutex_lock(&table->mutex);
268 if (--table->icm[i]->refcount == 0) {
269 mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
270 MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE);
271 mthca_free_icm(dev, table->icm[i], table->coherent);
272 table->icm[i] = NULL;
275 mutex_unlock(&table->mutex);
278 void *mthca_table_find(struct mthca_icm_table *table, int obj, dma_addr_t *dma_handle)
280 int idx, offset, dma_offset, i;
281 struct mthca_icm_chunk *chunk;
282 struct mthca_icm *icm;
283 struct page *page = NULL;
288 mutex_lock(&table->mutex);
290 idx = (obj & (table->num_obj - 1)) * table->obj_size;
291 icm = table->icm[idx / MTHCA_TABLE_CHUNK_SIZE];
292 dma_offset = offset = idx % MTHCA_TABLE_CHUNK_SIZE;
297 list_for_each_entry(chunk, &icm->chunk_list, list) {
298 for (i = 0; i < chunk->npages; ++i) {
299 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]) +
303 dma_offset -= sg_dma_len(&chunk->mem[i]);
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. */
308 if (chunk->mem[i].length > offset) {
309 page = sg_page(&chunk->mem[i]);
312 offset -= chunk->mem[i].length;
317 mutex_unlock(&table->mutex);
318 return page ? lowmem_page_address(page) + offset : NULL;
321 int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,
324 int inc = MTHCA_TABLE_CHUNK_SIZE / table->obj_size;
327 for (i = start; i <= end; i += inc) {
328 err = mthca_table_get(dev, table, i);
338 mthca_table_put(dev, table, i);
344 void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,
349 if (!mthca_is_memfree(dev))
352 for (i = start; i <= end; i += MTHCA_TABLE_CHUNK_SIZE / table->obj_size)
353 mthca_table_put(dev, table, i);
356 struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
357 u64 virt, int obj_size,
358 int nobj, int reserved,
359 int use_lowmem, int use_coherent)
361 struct mthca_icm_table *table;
367 obj_per_chunk = MTHCA_TABLE_CHUNK_SIZE / obj_size;
368 num_icm = DIV_ROUND_UP(nobj, obj_per_chunk);
370 table = kmalloc(sizeof *table + num_icm * sizeof *table->icm, GFP_KERNEL);
375 table->num_icm = num_icm;
376 table->num_obj = nobj;
377 table->obj_size = obj_size;
378 table->lowmem = use_lowmem;
379 table->coherent = use_coherent;
380 mutex_init(&table->mutex);
382 for (i = 0; i < num_icm; ++i)
383 table->icm[i] = NULL;
385 for (i = 0; i * MTHCA_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) {
386 chunk_size = MTHCA_TABLE_CHUNK_SIZE;
387 if ((i + 1) * MTHCA_TABLE_CHUNK_SIZE > nobj * obj_size)
388 chunk_size = nobj * obj_size - i * MTHCA_TABLE_CHUNK_SIZE;
390 table->icm[i] = mthca_alloc_icm(dev, chunk_size >> PAGE_SHIFT,
391 (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
392 __GFP_NOWARN, use_coherent);
395 if (mthca_MAP_ICM(dev, table->icm[i],
396 virt + i * MTHCA_TABLE_CHUNK_SIZE)) {
397 mthca_free_icm(dev, table->icm[i], table->coherent);
398 table->icm[i] = NULL;
403 * Add a reference to this ICM chunk so that it never
404 * gets freed (since it contains reserved firmware objects).
406 ++table->icm[i]->refcount;
412 for (i = 0; i < num_icm; ++i)
414 mthca_UNMAP_ICM(dev, virt + i * MTHCA_TABLE_CHUNK_SIZE,
415 MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE);
416 mthca_free_icm(dev, table->icm[i], table->coherent);
424 void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table)
428 for (i = 0; i < table->num_icm; ++i)
431 table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
432 MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE);
433 mthca_free_icm(dev, table->icm[i], table->coherent);
439 static u64 mthca_uarc_virt(struct mthca_dev *dev, struct mthca_uar *uar, int page)
441 return dev->uar_table.uarc_base +
442 uar->index * dev->uar_table.uarc_size +
443 page * MTHCA_ICM_PAGE_SIZE;
446 int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
447 struct mthca_user_db_table *db_tab, int index, u64 uaddr)
449 struct page *pages[1];
453 if (!mthca_is_memfree(dev))
456 if (index < 0 || index > dev->uar_table.uarc_size / 8)
459 mutex_lock(&db_tab->mutex);
461 i = index / MTHCA_DB_REC_PER_PAGE;
463 if ((db_tab->page[i].refcount >= MTHCA_DB_REC_PER_PAGE) ||
464 (db_tab->page[i].uvirt && db_tab->page[i].uvirt != uaddr) ||
470 if (db_tab->page[i].refcount) {
471 ++db_tab->page[i].refcount;
475 ret = get_user_pages(uaddr & PAGE_MASK, 1, FOLL_WRITE, pages, NULL);
479 sg_set_page(&db_tab->page[i].mem, pages[0], MTHCA_ICM_PAGE_SIZE,
482 ret = pci_map_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
488 ret = mthca_MAP_ICM_page(dev, sg_dma_address(&db_tab->page[i].mem),
489 mthca_uarc_virt(dev, uar, i));
491 pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
492 put_page(sg_page(&db_tab->page[i].mem));
496 db_tab->page[i].uvirt = uaddr;
497 db_tab->page[i].refcount = 1;
500 mutex_unlock(&db_tab->mutex);
504 void mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
505 struct mthca_user_db_table *db_tab, int index)
507 if (!mthca_is_memfree(dev))
511 * To make our bookkeeping simpler, we don't unmap DB
512 * pages until we clean up the whole db table.
515 mutex_lock(&db_tab->mutex);
517 --db_tab->page[index / MTHCA_DB_REC_PER_PAGE].refcount;
519 mutex_unlock(&db_tab->mutex);
522 struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev)
524 struct mthca_user_db_table *db_tab;
528 if (!mthca_is_memfree(dev))
531 npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE;
532 db_tab = kmalloc(sizeof *db_tab + npages * sizeof *db_tab->page, GFP_KERNEL);
534 return ERR_PTR(-ENOMEM);
536 mutex_init(&db_tab->mutex);
537 for (i = 0; i < npages; ++i) {
538 db_tab->page[i].refcount = 0;
539 db_tab->page[i].uvirt = 0;
540 sg_init_table(&db_tab->page[i].mem, 1);
546 void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,
547 struct mthca_user_db_table *db_tab)
551 if (!mthca_is_memfree(dev))
554 for (i = 0; i < dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE; ++i) {
555 if (db_tab->page[i].uvirt) {
556 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, uar, i), 1);
557 pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
558 put_page(sg_page(&db_tab->page[i].mem));
565 int mthca_alloc_db(struct mthca_dev *dev, enum mthca_db_type type,
571 struct mthca_db_page *page;
574 mutex_lock(&dev->db_tab->mutex);
577 case MTHCA_DB_TYPE_CQ_ARM:
578 case MTHCA_DB_TYPE_SQ:
581 end = dev->db_tab->max_group1;
585 case MTHCA_DB_TYPE_CQ_SET_CI:
586 case MTHCA_DB_TYPE_RQ:
587 case MTHCA_DB_TYPE_SRQ:
589 start = dev->db_tab->npages - 1;
590 end = dev->db_tab->min_group2;
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;
607 for (i = start; i != end; i += dir)
608 if (!dev->db_tab->page[i].db_rec) {
609 page = dev->db_tab->page + i;
613 if (dev->db_tab->max_group1 >= dev->db_tab->min_group2 - 1) {
619 ++dev->db_tab->max_group1;
621 --dev->db_tab->min_group2;
623 page = dev->db_tab->page + end;
626 page->db_rec = dma_alloc_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
627 &page->mapping, GFP_KERNEL);
632 memset(page->db_rec, 0, MTHCA_ICM_PAGE_SIZE);
634 ret = mthca_MAP_ICM_page(dev, page->mapping,
635 mthca_uarc_virt(dev, &dev->driver_uar, i));
637 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
638 page->db_rec, page->mapping);
642 bitmap_zero(page->used, MTHCA_DB_REC_PER_PAGE);
645 j = find_first_zero_bit(page->used, MTHCA_DB_REC_PER_PAGE);
646 set_bit(j, page->used);
649 j = MTHCA_DB_REC_PER_PAGE - 1 - j;
651 ret = i * MTHCA_DB_REC_PER_PAGE + j;
653 page->db_rec[j] = cpu_to_be64((qn << 8) | (type << 5));
655 *db = (__be32 *) &page->db_rec[j];
658 mutex_unlock(&dev->db_tab->mutex);
663 void mthca_free_db(struct mthca_dev *dev, int type, int db_index)
666 struct mthca_db_page *page;
668 i = db_index / MTHCA_DB_REC_PER_PAGE;
669 j = db_index % MTHCA_DB_REC_PER_PAGE;
671 page = dev->db_tab->page + i;
673 mutex_lock(&dev->db_tab->mutex);
676 if (i >= dev->db_tab->min_group2)
677 j = MTHCA_DB_REC_PER_PAGE - 1 - j;
678 clear_bit(j, page->used);
680 if (bitmap_empty(page->used, MTHCA_DB_REC_PER_PAGE) &&
681 i >= dev->db_tab->max_group1 - 1) {
682 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1);
684 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
685 page->db_rec, page->mapping);
688 if (i == dev->db_tab->max_group1) {
689 --dev->db_tab->max_group1;
690 /* XXX may be able to unmap more pages now */
692 if (i == dev->db_tab->min_group2)
693 ++dev->db_tab->min_group2;
696 mutex_unlock(&dev->db_tab->mutex);
699 int mthca_init_db_tab(struct mthca_dev *dev)
703 if (!mthca_is_memfree(dev))
706 dev->db_tab = kmalloc(sizeof *dev->db_tab, GFP_KERNEL);
710 mutex_init(&dev->db_tab->mutex);
712 dev->db_tab->npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE;
713 dev->db_tab->max_group1 = 0;
714 dev->db_tab->min_group2 = dev->db_tab->npages - 1;
716 dev->db_tab->page = kmalloc(dev->db_tab->npages *
717 sizeof *dev->db_tab->page,
719 if (!dev->db_tab->page) {
724 for (i = 0; i < dev->db_tab->npages; ++i)
725 dev->db_tab->page[i].db_rec = NULL;
730 void mthca_cleanup_db_tab(struct mthca_dev *dev)
734 if (!mthca_is_memfree(dev))
738 * Because we don't always free our UARC pages when they
739 * become empty to make mthca_free_db() simpler we need to
740 * make a sweep through the doorbell pages and free any
741 * leftover pages now.
743 for (i = 0; i < dev->db_tab->npages; ++i) {
744 if (!dev->db_tab->page[i].db_rec)
747 if (!bitmap_empty(dev->db_tab->page[i].used, MTHCA_DB_REC_PER_PAGE))
748 mthca_warn(dev, "Kernel UARC page %d not empty\n", i);
750 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1);
752 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
753 dev->db_tab->page[i].db_rec,
754 dev->db_tab->page[i].mapping);
757 kfree(dev->db_tab->page);