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
39 #include "mthca_memfree.h"
40 #include "mthca_dev.h"
41 #include "mthca_cmd.h"
44 * We allocate in as big chunks as we can, up to a maximum of 256 KB
48 MTHCA_ICM_ALLOC_SIZE = 1 << 18,
49 MTHCA_TABLE_CHUNK_SIZE = 1 << 18
52 struct mthca_user_db_table {
56 struct scatterlist mem;
61 void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm)
63 struct mthca_icm_chunk *chunk, *tmp;
69 list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
71 pci_unmap_sg(dev->pdev, chunk->mem, chunk->npages,
72 PCI_DMA_BIDIRECTIONAL);
74 for (i = 0; i < chunk->npages; ++i)
75 __free_pages(chunk->mem[i].page,
76 get_order(chunk->mem[i].length));
84 struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
87 struct mthca_icm *icm;
88 struct mthca_icm_chunk *chunk = NULL;
91 icm = kmalloc(sizeof *icm, gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
96 INIT_LIST_HEAD(&icm->chunk_list);
98 cur_order = get_order(MTHCA_ICM_ALLOC_SIZE);
102 chunk = kmalloc(sizeof *chunk,
103 gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
109 list_add_tail(&chunk->list, &icm->chunk_list);
112 while (1 << cur_order > npages)
115 chunk->mem[chunk->npages].page = alloc_pages(gfp_mask, cur_order);
116 if (chunk->mem[chunk->npages].page) {
117 chunk->mem[chunk->npages].length = PAGE_SIZE << cur_order;
118 chunk->mem[chunk->npages].offset = 0;
120 if (++chunk->npages == MTHCA_ICM_CHUNK_LEN) {
121 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
123 PCI_DMA_BIDIRECTIONAL);
131 npages -= 1 << cur_order;
140 chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
142 PCI_DMA_BIDIRECTIONAL);
151 mthca_free_icm(dev, icm);
155 int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
157 int i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
161 mutex_lock(&table->mutex);
164 ++table->icm[i]->refcount;
168 table->icm[i] = mthca_alloc_icm(dev, MTHCA_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
169 (table->lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
171 if (!table->icm[i]) {
176 if (mthca_MAP_ICM(dev, table->icm[i], table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
177 &status) || status) {
178 mthca_free_icm(dev, table->icm[i]);
179 table->icm[i] = NULL;
184 ++table->icm[i]->refcount;
187 mutex_unlock(&table->mutex);
191 void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
196 if (!mthca_is_memfree(dev))
199 i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
201 mutex_lock(&table->mutex);
203 if (--table->icm[i]->refcount == 0) {
204 mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
205 MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
207 mthca_free_icm(dev, table->icm[i]);
208 table->icm[i] = NULL;
211 mutex_unlock(&table->mutex);
214 void *mthca_table_find(struct mthca_icm_table *table, int obj)
217 struct mthca_icm_chunk *chunk;
218 struct mthca_icm *icm;
219 struct page *page = NULL;
224 mutex_lock(&table->mutex);
226 idx = (obj & (table->num_obj - 1)) * table->obj_size;
227 icm = table->icm[idx / MTHCA_TABLE_CHUNK_SIZE];
228 offset = idx % MTHCA_TABLE_CHUNK_SIZE;
233 list_for_each_entry(chunk, &icm->chunk_list, list) {
234 for (i = 0; i < chunk->npages; ++i) {
235 if (chunk->mem[i].length >= offset) {
236 page = chunk->mem[i].page;
239 offset -= chunk->mem[i].length;
244 mutex_unlock(&table->mutex);
245 return page ? lowmem_page_address(page) + offset : NULL;
248 int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,
251 int inc = MTHCA_TABLE_CHUNK_SIZE / table->obj_size;
254 for (i = start; i <= end; i += inc) {
255 err = mthca_table_get(dev, table, i);
265 mthca_table_put(dev, table, i);
271 void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,
276 if (!mthca_is_memfree(dev))
279 for (i = start; i <= end; i += MTHCA_TABLE_CHUNK_SIZE / table->obj_size)
280 mthca_table_put(dev, table, i);
283 struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
284 u64 virt, int obj_size,
285 int nobj, int reserved,
288 struct mthca_icm_table *table;
294 num_icm = (obj_size * nobj + MTHCA_TABLE_CHUNK_SIZE - 1) / MTHCA_TABLE_CHUNK_SIZE;
296 table = kmalloc(sizeof *table + num_icm * sizeof *table->icm, GFP_KERNEL);
301 table->num_icm = num_icm;
302 table->num_obj = nobj;
303 table->obj_size = obj_size;
304 table->lowmem = use_lowmem;
305 mutex_init(&table->mutex);
307 for (i = 0; i < num_icm; ++i)
308 table->icm[i] = NULL;
310 for (i = 0; i * MTHCA_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) {
311 chunk_size = MTHCA_TABLE_CHUNK_SIZE;
312 if ((i + 1) * MTHCA_TABLE_CHUNK_SIZE > nobj * obj_size)
313 chunk_size = nobj * obj_size - i * MTHCA_TABLE_CHUNK_SIZE;
315 table->icm[i] = mthca_alloc_icm(dev, chunk_size >> PAGE_SHIFT,
316 (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
320 if (mthca_MAP_ICM(dev, table->icm[i], virt + i * MTHCA_TABLE_CHUNK_SIZE,
321 &status) || status) {
322 mthca_free_icm(dev, table->icm[i]);
323 table->icm[i] = NULL;
328 * Add a reference to this ICM chunk so that it never
329 * gets freed (since it contains reserved firmware objects).
331 ++table->icm[i]->refcount;
337 for (i = 0; i < num_icm; ++i)
339 mthca_UNMAP_ICM(dev, virt + i * MTHCA_TABLE_CHUNK_SIZE,
340 MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
342 mthca_free_icm(dev, table->icm[i]);
350 void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table)
355 for (i = 0; i < table->num_icm; ++i)
357 mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
358 MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
360 mthca_free_icm(dev, table->icm[i]);
366 static u64 mthca_uarc_virt(struct mthca_dev *dev, struct mthca_uar *uar, int page)
368 return dev->uar_table.uarc_base +
369 uar->index * dev->uar_table.uarc_size +
370 page * MTHCA_ICM_PAGE_SIZE;
373 int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
374 struct mthca_user_db_table *db_tab, int index, u64 uaddr)
380 if (!mthca_is_memfree(dev))
383 if (index < 0 || index > dev->uar_table.uarc_size / 8)
386 mutex_lock(&db_tab->mutex);
388 i = index / MTHCA_DB_REC_PER_PAGE;
390 if ((db_tab->page[i].refcount >= MTHCA_DB_REC_PER_PAGE) ||
391 (db_tab->page[i].uvirt && db_tab->page[i].uvirt != uaddr) ||
397 if (db_tab->page[i].refcount) {
398 ++db_tab->page[i].refcount;
402 ret = get_user_pages(current, current->mm, uaddr & PAGE_MASK, 1, 1, 0,
403 &db_tab->page[i].mem.page, NULL);
407 db_tab->page[i].mem.length = MTHCA_ICM_PAGE_SIZE;
408 db_tab->page[i].mem.offset = uaddr & ~PAGE_MASK;
410 ret = pci_map_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
412 put_page(db_tab->page[i].mem.page);
416 ret = mthca_MAP_ICM_page(dev, sg_dma_address(&db_tab->page[i].mem),
417 mthca_uarc_virt(dev, uar, i), &status);
421 pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
422 put_page(db_tab->page[i].mem.page);
426 db_tab->page[i].uvirt = uaddr;
427 db_tab->page[i].refcount = 1;
430 mutex_unlock(&db_tab->mutex);
434 void mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
435 struct mthca_user_db_table *db_tab, int index)
437 if (!mthca_is_memfree(dev))
441 * To make our bookkeeping simpler, we don't unmap DB
442 * pages until we clean up the whole db table.
445 mutex_lock(&db_tab->mutex);
447 --db_tab->page[index / MTHCA_DB_REC_PER_PAGE].refcount;
449 mutex_unlock(&db_tab->mutex);
452 struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev)
454 struct mthca_user_db_table *db_tab;
458 if (!mthca_is_memfree(dev))
461 npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE;
462 db_tab = kmalloc(sizeof *db_tab + npages * sizeof *db_tab->page, GFP_KERNEL);
464 return ERR_PTR(-ENOMEM);
466 mutex_init(&db_tab->mutex);
467 for (i = 0; i < npages; ++i) {
468 db_tab->page[i].refcount = 0;
469 db_tab->page[i].uvirt = 0;
475 void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,
476 struct mthca_user_db_table *db_tab)
481 if (!mthca_is_memfree(dev))
484 for (i = 0; i < dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE; ++i) {
485 if (db_tab->page[i].uvirt) {
486 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, uar, i), 1, &status);
487 pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
488 put_page(db_tab->page[i].mem.page);
495 int mthca_alloc_db(struct mthca_dev *dev, enum mthca_db_type type,
501 struct mthca_db_page *page;
505 mutex_lock(&dev->db_tab->mutex);
508 case MTHCA_DB_TYPE_CQ_ARM:
509 case MTHCA_DB_TYPE_SQ:
512 end = dev->db_tab->max_group1;
516 case MTHCA_DB_TYPE_CQ_SET_CI:
517 case MTHCA_DB_TYPE_RQ:
518 case MTHCA_DB_TYPE_SRQ:
520 start = dev->db_tab->npages - 1;
521 end = dev->db_tab->min_group2;
530 for (i = start; i != end; i += dir)
531 if (dev->db_tab->page[i].db_rec &&
532 !bitmap_full(dev->db_tab->page[i].used,
533 MTHCA_DB_REC_PER_PAGE)) {
534 page = dev->db_tab->page + i;
538 for (i = start; i != end; i += dir)
539 if (!dev->db_tab->page[i].db_rec) {
540 page = dev->db_tab->page + i;
544 if (dev->db_tab->max_group1 >= dev->db_tab->min_group2 - 1) {
550 ++dev->db_tab->max_group1;
552 --dev->db_tab->min_group2;
554 page = dev->db_tab->page + end;
557 page->db_rec = dma_alloc_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
558 &page->mapping, GFP_KERNEL);
563 memset(page->db_rec, 0, MTHCA_ICM_PAGE_SIZE);
565 ret = mthca_MAP_ICM_page(dev, page->mapping,
566 mthca_uarc_virt(dev, &dev->driver_uar, i), &status);
570 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
571 page->db_rec, page->mapping);
575 bitmap_zero(page->used, MTHCA_DB_REC_PER_PAGE);
578 j = find_first_zero_bit(page->used, MTHCA_DB_REC_PER_PAGE);
579 set_bit(j, page->used);
582 j = MTHCA_DB_REC_PER_PAGE - 1 - j;
584 ret = i * MTHCA_DB_REC_PER_PAGE + j;
586 page->db_rec[j] = cpu_to_be64((qn << 8) | (type << 5));
588 *db = (__be32 *) &page->db_rec[j];
591 mutex_unlock(&dev->db_tab->mutex);
596 void mthca_free_db(struct mthca_dev *dev, int type, int db_index)
599 struct mthca_db_page *page;
602 i = db_index / MTHCA_DB_REC_PER_PAGE;
603 j = db_index % MTHCA_DB_REC_PER_PAGE;
605 page = dev->db_tab->page + i;
607 mutex_lock(&dev->db_tab->mutex);
610 if (i >= dev->db_tab->min_group2)
611 j = MTHCA_DB_REC_PER_PAGE - 1 - j;
612 clear_bit(j, page->used);
614 if (bitmap_empty(page->used, MTHCA_DB_REC_PER_PAGE) &&
615 i >= dev->db_tab->max_group1 - 1) {
616 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1, &status);
618 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
619 page->db_rec, page->mapping);
622 if (i == dev->db_tab->max_group1) {
623 --dev->db_tab->max_group1;
624 /* XXX may be able to unmap more pages now */
626 if (i == dev->db_tab->min_group2)
627 ++dev->db_tab->min_group2;
630 mutex_unlock(&dev->db_tab->mutex);
633 int mthca_init_db_tab(struct mthca_dev *dev)
637 if (!mthca_is_memfree(dev))
640 dev->db_tab = kmalloc(sizeof *dev->db_tab, GFP_KERNEL);
644 mutex_init(&dev->db_tab->mutex);
646 dev->db_tab->npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE;
647 dev->db_tab->max_group1 = 0;
648 dev->db_tab->min_group2 = dev->db_tab->npages - 1;
650 dev->db_tab->page = kmalloc(dev->db_tab->npages *
651 sizeof *dev->db_tab->page,
653 if (!dev->db_tab->page) {
658 for (i = 0; i < dev->db_tab->npages; ++i)
659 dev->db_tab->page[i].db_rec = NULL;
664 void mthca_cleanup_db_tab(struct mthca_dev *dev)
669 if (!mthca_is_memfree(dev))
673 * Because we don't always free our UARC pages when they
674 * become empty to make mthca_free_db() simpler we need to
675 * make a sweep through the doorbell pages and free any
676 * leftover pages now.
678 for (i = 0; i < dev->db_tab->npages; ++i) {
679 if (!dev->db_tab->page[i].db_rec)
682 if (!bitmap_empty(dev->db_tab->page[i].used, MTHCA_DB_REC_PER_PAGE))
683 mthca_warn(dev, "Kernel UARC page %d not empty\n", i);
685 mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1, &status);
687 dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
688 dev->db_tab->page[i].db_rec,
689 dev->db_tab->page[i].mapping);
692 kfree(dev->db_tab->page);