15a2cc190SJeff Kirsher /* 25a2cc190SJeff Kirsher * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved. 35a2cc190SJeff Kirsher * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved. 45a2cc190SJeff Kirsher * 55a2cc190SJeff Kirsher * This software is available to you under a choice of one of two 65a2cc190SJeff Kirsher * licenses. You may choose to be licensed under the terms of the GNU 75a2cc190SJeff Kirsher * General Public License (GPL) Version 2, available from the file 85a2cc190SJeff Kirsher * COPYING in the main directory of this source tree, or the 95a2cc190SJeff Kirsher * OpenIB.org BSD license below: 105a2cc190SJeff Kirsher * 115a2cc190SJeff Kirsher * Redistribution and use in source and binary forms, with or 125a2cc190SJeff Kirsher * without modification, are permitted provided that the following 135a2cc190SJeff Kirsher * conditions are met: 145a2cc190SJeff Kirsher * 155a2cc190SJeff Kirsher * - Redistributions of source code must retain the above 165a2cc190SJeff Kirsher * copyright notice, this list of conditions and the following 175a2cc190SJeff Kirsher * disclaimer. 185a2cc190SJeff Kirsher * 195a2cc190SJeff Kirsher * - Redistributions in binary form must reproduce the above 205a2cc190SJeff Kirsher * copyright notice, this list of conditions and the following 215a2cc190SJeff Kirsher * disclaimer in the documentation and/or other materials 225a2cc190SJeff Kirsher * provided with the distribution. 235a2cc190SJeff Kirsher * 245a2cc190SJeff Kirsher * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 255a2cc190SJeff Kirsher * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 265a2cc190SJeff Kirsher * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 275a2cc190SJeff Kirsher * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 285a2cc190SJeff Kirsher * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 295a2cc190SJeff Kirsher * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 305a2cc190SJeff Kirsher * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 315a2cc190SJeff Kirsher * SOFTWARE. 325a2cc190SJeff Kirsher */ 335a2cc190SJeff Kirsher 345a2cc190SJeff Kirsher #include <linux/errno.h> 355a2cc190SJeff Kirsher #include <linux/mm.h> 365a2cc190SJeff Kirsher #include <linux/scatterlist.h> 375a2cc190SJeff Kirsher #include <linux/slab.h> 385a2cc190SJeff Kirsher 395a2cc190SJeff Kirsher #include <linux/mlx4/cmd.h> 405a2cc190SJeff Kirsher 415a2cc190SJeff Kirsher #include "mlx4.h" 425a2cc190SJeff Kirsher #include "icm.h" 435a2cc190SJeff Kirsher #include "fw.h" 445a2cc190SJeff Kirsher 455a2cc190SJeff Kirsher /* 465a2cc190SJeff Kirsher * We allocate in as big chunks as we can, up to a maximum of 256 KB 475a2cc190SJeff Kirsher * per chunk. 485a2cc190SJeff Kirsher */ 495a2cc190SJeff Kirsher enum { 505a2cc190SJeff Kirsher MLX4_ICM_ALLOC_SIZE = 1 << 18, 515a2cc190SJeff Kirsher MLX4_TABLE_CHUNK_SIZE = 1 << 18 525a2cc190SJeff Kirsher }; 535a2cc190SJeff Kirsher 545a2cc190SJeff Kirsher static void mlx4_free_icm_pages(struct mlx4_dev *dev, struct mlx4_icm_chunk *chunk) 555a2cc190SJeff Kirsher { 565a2cc190SJeff Kirsher int i; 575a2cc190SJeff Kirsher 585a2cc190SJeff Kirsher if (chunk->nsg > 0) 595a2cc190SJeff Kirsher pci_unmap_sg(dev->pdev, chunk->mem, chunk->npages, 605a2cc190SJeff Kirsher PCI_DMA_BIDIRECTIONAL); 615a2cc190SJeff Kirsher 625a2cc190SJeff Kirsher for (i = 0; i < chunk->npages; ++i) 635a2cc190SJeff Kirsher __free_pages(sg_page(&chunk->mem[i]), 645a2cc190SJeff Kirsher get_order(chunk->mem[i].length)); 655a2cc190SJeff Kirsher } 665a2cc190SJeff Kirsher 675a2cc190SJeff Kirsher static void mlx4_free_icm_coherent(struct mlx4_dev *dev, struct mlx4_icm_chunk *chunk) 685a2cc190SJeff Kirsher { 695a2cc190SJeff Kirsher int i; 705a2cc190SJeff Kirsher 715a2cc190SJeff Kirsher for (i = 0; i < chunk->npages; ++i) 725a2cc190SJeff Kirsher dma_free_coherent(&dev->pdev->dev, chunk->mem[i].length, 735a2cc190SJeff Kirsher lowmem_page_address(sg_page(&chunk->mem[i])), 745a2cc190SJeff Kirsher sg_dma_address(&chunk->mem[i])); 755a2cc190SJeff Kirsher } 765a2cc190SJeff Kirsher 775a2cc190SJeff Kirsher void mlx4_free_icm(struct mlx4_dev *dev, struct mlx4_icm *icm, int coherent) 785a2cc190SJeff Kirsher { 795a2cc190SJeff Kirsher struct mlx4_icm_chunk *chunk, *tmp; 805a2cc190SJeff Kirsher 815a2cc190SJeff Kirsher if (!icm) 825a2cc190SJeff Kirsher return; 835a2cc190SJeff Kirsher 845a2cc190SJeff Kirsher list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) { 855a2cc190SJeff Kirsher if (coherent) 865a2cc190SJeff Kirsher mlx4_free_icm_coherent(dev, chunk); 875a2cc190SJeff Kirsher else 885a2cc190SJeff Kirsher mlx4_free_icm_pages(dev, chunk); 895a2cc190SJeff Kirsher 905a2cc190SJeff Kirsher kfree(chunk); 915a2cc190SJeff Kirsher } 925a2cc190SJeff Kirsher 935a2cc190SJeff Kirsher kfree(icm); 945a2cc190SJeff Kirsher } 955a2cc190SJeff Kirsher 965a2cc190SJeff Kirsher static int mlx4_alloc_icm_pages(struct scatterlist *mem, int order, gfp_t gfp_mask) 975a2cc190SJeff Kirsher { 985a2cc190SJeff Kirsher struct page *page; 995a2cc190SJeff Kirsher 1005a2cc190SJeff Kirsher page = alloc_pages(gfp_mask, order); 1015a2cc190SJeff Kirsher if (!page) 1025a2cc190SJeff Kirsher return -ENOMEM; 1035a2cc190SJeff Kirsher 1045a2cc190SJeff Kirsher sg_set_page(mem, page, PAGE_SIZE << order, 0); 1055a2cc190SJeff Kirsher return 0; 1065a2cc190SJeff Kirsher } 1075a2cc190SJeff Kirsher 1085a2cc190SJeff Kirsher static int mlx4_alloc_icm_coherent(struct device *dev, struct scatterlist *mem, 1095a2cc190SJeff Kirsher int order, gfp_t gfp_mask) 1105a2cc190SJeff Kirsher { 1115a2cc190SJeff Kirsher void *buf = dma_alloc_coherent(dev, PAGE_SIZE << order, 1125a2cc190SJeff Kirsher &sg_dma_address(mem), gfp_mask); 1135a2cc190SJeff Kirsher if (!buf) 1145a2cc190SJeff Kirsher return -ENOMEM; 1155a2cc190SJeff Kirsher 1165a2cc190SJeff Kirsher sg_set_buf(mem, buf, PAGE_SIZE << order); 1175a2cc190SJeff Kirsher BUG_ON(mem->offset); 1185a2cc190SJeff Kirsher sg_dma_len(mem) = PAGE_SIZE << order; 1195a2cc190SJeff Kirsher return 0; 1205a2cc190SJeff Kirsher } 1215a2cc190SJeff Kirsher 1225a2cc190SJeff Kirsher struct mlx4_icm *mlx4_alloc_icm(struct mlx4_dev *dev, int npages, 1235a2cc190SJeff Kirsher gfp_t gfp_mask, int coherent) 1245a2cc190SJeff Kirsher { 1255a2cc190SJeff Kirsher struct mlx4_icm *icm; 1265a2cc190SJeff Kirsher struct mlx4_icm_chunk *chunk = NULL; 1275a2cc190SJeff Kirsher int cur_order; 1285a2cc190SJeff Kirsher int ret; 1295a2cc190SJeff Kirsher 1305a2cc190SJeff Kirsher /* We use sg_set_buf for coherent allocs, which assumes low memory */ 1315a2cc190SJeff Kirsher BUG_ON(coherent && (gfp_mask & __GFP_HIGHMEM)); 1325a2cc190SJeff Kirsher 1335a2cc190SJeff Kirsher icm = kmalloc(sizeof *icm, gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN)); 1345a2cc190SJeff Kirsher if (!icm) 1355a2cc190SJeff Kirsher return NULL; 1365a2cc190SJeff Kirsher 1375a2cc190SJeff Kirsher icm->refcount = 0; 1385a2cc190SJeff Kirsher INIT_LIST_HEAD(&icm->chunk_list); 1395a2cc190SJeff Kirsher 1405a2cc190SJeff Kirsher cur_order = get_order(MLX4_ICM_ALLOC_SIZE); 1415a2cc190SJeff Kirsher 1425a2cc190SJeff Kirsher while (npages > 0) { 1435a2cc190SJeff Kirsher if (!chunk) { 1445a2cc190SJeff Kirsher chunk = kmalloc(sizeof *chunk, 1455a2cc190SJeff Kirsher gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN)); 1465a2cc190SJeff Kirsher if (!chunk) 1475a2cc190SJeff Kirsher goto fail; 1485a2cc190SJeff Kirsher 1495a2cc190SJeff Kirsher sg_init_table(chunk->mem, MLX4_ICM_CHUNK_LEN); 1505a2cc190SJeff Kirsher chunk->npages = 0; 1515a2cc190SJeff Kirsher chunk->nsg = 0; 1525a2cc190SJeff Kirsher list_add_tail(&chunk->list, &icm->chunk_list); 1535a2cc190SJeff Kirsher } 1545a2cc190SJeff Kirsher 1555a2cc190SJeff Kirsher while (1 << cur_order > npages) 1565a2cc190SJeff Kirsher --cur_order; 1575a2cc190SJeff Kirsher 1585a2cc190SJeff Kirsher if (coherent) 1595a2cc190SJeff Kirsher ret = mlx4_alloc_icm_coherent(&dev->pdev->dev, 1605a2cc190SJeff Kirsher &chunk->mem[chunk->npages], 1615a2cc190SJeff Kirsher cur_order, gfp_mask); 1625a2cc190SJeff Kirsher else 1635a2cc190SJeff Kirsher ret = mlx4_alloc_icm_pages(&chunk->mem[chunk->npages], 1645a2cc190SJeff Kirsher cur_order, gfp_mask); 1655a2cc190SJeff Kirsher 1665a2cc190SJeff Kirsher if (ret) { 1675a2cc190SJeff Kirsher if (--cur_order < 0) 1685a2cc190SJeff Kirsher goto fail; 1695a2cc190SJeff Kirsher else 1705a2cc190SJeff Kirsher continue; 1715a2cc190SJeff Kirsher } 1725a2cc190SJeff Kirsher 1735a2cc190SJeff Kirsher ++chunk->npages; 1745a2cc190SJeff Kirsher 1755a2cc190SJeff Kirsher if (coherent) 1765a2cc190SJeff Kirsher ++chunk->nsg; 1775a2cc190SJeff Kirsher else if (chunk->npages == MLX4_ICM_CHUNK_LEN) { 1785a2cc190SJeff Kirsher chunk->nsg = pci_map_sg(dev->pdev, chunk->mem, 1795a2cc190SJeff Kirsher chunk->npages, 1805a2cc190SJeff Kirsher PCI_DMA_BIDIRECTIONAL); 1815a2cc190SJeff Kirsher 1825a2cc190SJeff Kirsher if (chunk->nsg <= 0) 1835a2cc190SJeff Kirsher goto fail; 1845a2cc190SJeff Kirsher } 1855a2cc190SJeff Kirsher 1865a2cc190SJeff Kirsher if (chunk->npages == MLX4_ICM_CHUNK_LEN) 1875a2cc190SJeff Kirsher chunk = NULL; 1885a2cc190SJeff Kirsher 1895a2cc190SJeff Kirsher npages -= 1 << cur_order; 1905a2cc190SJeff Kirsher } 1915a2cc190SJeff Kirsher 1925a2cc190SJeff Kirsher if (!coherent && chunk) { 1935a2cc190SJeff Kirsher chunk->nsg = pci_map_sg(dev->pdev, chunk->mem, 1945a2cc190SJeff Kirsher chunk->npages, 1955a2cc190SJeff Kirsher PCI_DMA_BIDIRECTIONAL); 1965a2cc190SJeff Kirsher 1975a2cc190SJeff Kirsher if (chunk->nsg <= 0) 1985a2cc190SJeff Kirsher goto fail; 1995a2cc190SJeff Kirsher } 2005a2cc190SJeff Kirsher 2015a2cc190SJeff Kirsher return icm; 2025a2cc190SJeff Kirsher 2035a2cc190SJeff Kirsher fail: 2045a2cc190SJeff Kirsher mlx4_free_icm(dev, icm, coherent); 2055a2cc190SJeff Kirsher return NULL; 2065a2cc190SJeff Kirsher } 2075a2cc190SJeff Kirsher 2085a2cc190SJeff Kirsher static int mlx4_MAP_ICM(struct mlx4_dev *dev, struct mlx4_icm *icm, u64 virt) 2095a2cc190SJeff Kirsher { 2105a2cc190SJeff Kirsher return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM, icm, virt); 2115a2cc190SJeff Kirsher } 2125a2cc190SJeff Kirsher 2135a2cc190SJeff Kirsher static int mlx4_UNMAP_ICM(struct mlx4_dev *dev, u64 virt, u32 page_count) 2145a2cc190SJeff Kirsher { 2155a2cc190SJeff Kirsher return mlx4_cmd(dev, virt, page_count, 0, MLX4_CMD_UNMAP_ICM, 2165a2cc190SJeff Kirsher MLX4_CMD_TIME_CLASS_B); 2175a2cc190SJeff Kirsher } 2185a2cc190SJeff Kirsher 2195a2cc190SJeff Kirsher int mlx4_MAP_ICM_AUX(struct mlx4_dev *dev, struct mlx4_icm *icm) 2205a2cc190SJeff Kirsher { 2215a2cc190SJeff Kirsher return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM_AUX, icm, -1); 2225a2cc190SJeff Kirsher } 2235a2cc190SJeff Kirsher 2245a2cc190SJeff Kirsher int mlx4_UNMAP_ICM_AUX(struct mlx4_dev *dev) 2255a2cc190SJeff Kirsher { 2265a2cc190SJeff Kirsher return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_UNMAP_ICM_AUX, MLX4_CMD_TIME_CLASS_B); 2275a2cc190SJeff Kirsher } 2285a2cc190SJeff Kirsher 2295a2cc190SJeff Kirsher int mlx4_table_get(struct mlx4_dev *dev, struct mlx4_icm_table *table, int obj) 2305a2cc190SJeff Kirsher { 2315a2cc190SJeff Kirsher int i = (obj & (table->num_obj - 1)) / (MLX4_TABLE_CHUNK_SIZE / table->obj_size); 2325a2cc190SJeff Kirsher int ret = 0; 2335a2cc190SJeff Kirsher 2345a2cc190SJeff Kirsher mutex_lock(&table->mutex); 2355a2cc190SJeff Kirsher 2365a2cc190SJeff Kirsher if (table->icm[i]) { 2375a2cc190SJeff Kirsher ++table->icm[i]->refcount; 2385a2cc190SJeff Kirsher goto out; 2395a2cc190SJeff Kirsher } 2405a2cc190SJeff Kirsher 2415a2cc190SJeff Kirsher table->icm[i] = mlx4_alloc_icm(dev, MLX4_TABLE_CHUNK_SIZE >> PAGE_SHIFT, 2425a2cc190SJeff Kirsher (table->lowmem ? GFP_KERNEL : GFP_HIGHUSER) | 2435a2cc190SJeff Kirsher __GFP_NOWARN, table->coherent); 2445a2cc190SJeff Kirsher if (!table->icm[i]) { 2455a2cc190SJeff Kirsher ret = -ENOMEM; 2465a2cc190SJeff Kirsher goto out; 2475a2cc190SJeff Kirsher } 2485a2cc190SJeff Kirsher 2495a2cc190SJeff Kirsher if (mlx4_MAP_ICM(dev, table->icm[i], table->virt + 2505a2cc190SJeff Kirsher (u64) i * MLX4_TABLE_CHUNK_SIZE)) { 2515a2cc190SJeff Kirsher mlx4_free_icm(dev, table->icm[i], table->coherent); 2525a2cc190SJeff Kirsher table->icm[i] = NULL; 2535a2cc190SJeff Kirsher ret = -ENOMEM; 2545a2cc190SJeff Kirsher goto out; 2555a2cc190SJeff Kirsher } 2565a2cc190SJeff Kirsher 2575a2cc190SJeff Kirsher ++table->icm[i]->refcount; 2585a2cc190SJeff Kirsher 2595a2cc190SJeff Kirsher out: 2605a2cc190SJeff Kirsher mutex_unlock(&table->mutex); 2615a2cc190SJeff Kirsher return ret; 2625a2cc190SJeff Kirsher } 2635a2cc190SJeff Kirsher 2645a2cc190SJeff Kirsher void mlx4_table_put(struct mlx4_dev *dev, struct mlx4_icm_table *table, int obj) 2655a2cc190SJeff Kirsher { 2665a2cc190SJeff Kirsher int i; 2675a2cc190SJeff Kirsher 2685a2cc190SJeff Kirsher i = (obj & (table->num_obj - 1)) / (MLX4_TABLE_CHUNK_SIZE / table->obj_size); 2695a2cc190SJeff Kirsher 2705a2cc190SJeff Kirsher mutex_lock(&table->mutex); 2715a2cc190SJeff Kirsher 2725a2cc190SJeff Kirsher if (--table->icm[i]->refcount == 0) { 2735a2cc190SJeff Kirsher mlx4_UNMAP_ICM(dev, table->virt + i * MLX4_TABLE_CHUNK_SIZE, 2745a2cc190SJeff Kirsher MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE); 2755a2cc190SJeff Kirsher mlx4_free_icm(dev, table->icm[i], table->coherent); 2765a2cc190SJeff Kirsher table->icm[i] = NULL; 2775a2cc190SJeff Kirsher } 2785a2cc190SJeff Kirsher 2795a2cc190SJeff Kirsher mutex_unlock(&table->mutex); 2805a2cc190SJeff Kirsher } 2815a2cc190SJeff Kirsher 2825a2cc190SJeff Kirsher void *mlx4_table_find(struct mlx4_icm_table *table, int obj, dma_addr_t *dma_handle) 2835a2cc190SJeff Kirsher { 2845a2cc190SJeff Kirsher int idx, offset, dma_offset, i; 2855a2cc190SJeff Kirsher struct mlx4_icm_chunk *chunk; 2865a2cc190SJeff Kirsher struct mlx4_icm *icm; 2875a2cc190SJeff Kirsher struct page *page = NULL; 2885a2cc190SJeff Kirsher 2895a2cc190SJeff Kirsher if (!table->lowmem) 2905a2cc190SJeff Kirsher return NULL; 2915a2cc190SJeff Kirsher 2925a2cc190SJeff Kirsher mutex_lock(&table->mutex); 2935a2cc190SJeff Kirsher 2945a2cc190SJeff Kirsher idx = (obj & (table->num_obj - 1)) * table->obj_size; 2955a2cc190SJeff Kirsher icm = table->icm[idx / MLX4_TABLE_CHUNK_SIZE]; 2965a2cc190SJeff Kirsher dma_offset = offset = idx % MLX4_TABLE_CHUNK_SIZE; 2975a2cc190SJeff Kirsher 2985a2cc190SJeff Kirsher if (!icm) 2995a2cc190SJeff Kirsher goto out; 3005a2cc190SJeff Kirsher 3015a2cc190SJeff Kirsher list_for_each_entry(chunk, &icm->chunk_list, list) { 3025a2cc190SJeff Kirsher for (i = 0; i < chunk->npages; ++i) { 3035a2cc190SJeff Kirsher if (dma_handle && dma_offset >= 0) { 3045a2cc190SJeff Kirsher if (sg_dma_len(&chunk->mem[i]) > dma_offset) 3055a2cc190SJeff Kirsher *dma_handle = sg_dma_address(&chunk->mem[i]) + 3065a2cc190SJeff Kirsher dma_offset; 3075a2cc190SJeff Kirsher dma_offset -= sg_dma_len(&chunk->mem[i]); 3085a2cc190SJeff Kirsher } 3095a2cc190SJeff Kirsher /* 3105a2cc190SJeff Kirsher * DMA mapping can merge pages but not split them, 3115a2cc190SJeff Kirsher * so if we found the page, dma_handle has already 3125a2cc190SJeff Kirsher * been assigned to. 3135a2cc190SJeff Kirsher */ 3145a2cc190SJeff Kirsher if (chunk->mem[i].length > offset) { 3155a2cc190SJeff Kirsher page = sg_page(&chunk->mem[i]); 3165a2cc190SJeff Kirsher goto out; 3175a2cc190SJeff Kirsher } 3185a2cc190SJeff Kirsher offset -= chunk->mem[i].length; 3195a2cc190SJeff Kirsher } 3205a2cc190SJeff Kirsher } 3215a2cc190SJeff Kirsher 3225a2cc190SJeff Kirsher out: 3235a2cc190SJeff Kirsher mutex_unlock(&table->mutex); 3245a2cc190SJeff Kirsher return page ? lowmem_page_address(page) + offset : NULL; 3255a2cc190SJeff Kirsher } 3265a2cc190SJeff Kirsher 3275a2cc190SJeff Kirsher int mlx4_table_get_range(struct mlx4_dev *dev, struct mlx4_icm_table *table, 3285a2cc190SJeff Kirsher int start, int end) 3295a2cc190SJeff Kirsher { 3305a2cc190SJeff Kirsher int inc = MLX4_TABLE_CHUNK_SIZE / table->obj_size; 3315a2cc190SJeff Kirsher int i, err; 3325a2cc190SJeff Kirsher 3335a2cc190SJeff Kirsher for (i = start; i <= end; i += inc) { 3345a2cc190SJeff Kirsher err = mlx4_table_get(dev, table, i); 3355a2cc190SJeff Kirsher if (err) 3365a2cc190SJeff Kirsher goto fail; 3375a2cc190SJeff Kirsher } 3385a2cc190SJeff Kirsher 3395a2cc190SJeff Kirsher return 0; 3405a2cc190SJeff Kirsher 3415a2cc190SJeff Kirsher fail: 3425a2cc190SJeff Kirsher while (i > start) { 3435a2cc190SJeff Kirsher i -= inc; 3445a2cc190SJeff Kirsher mlx4_table_put(dev, table, i); 3455a2cc190SJeff Kirsher } 3465a2cc190SJeff Kirsher 3475a2cc190SJeff Kirsher return err; 3485a2cc190SJeff Kirsher } 3495a2cc190SJeff Kirsher 3505a2cc190SJeff Kirsher void mlx4_table_put_range(struct mlx4_dev *dev, struct mlx4_icm_table *table, 3515a2cc190SJeff Kirsher int start, int end) 3525a2cc190SJeff Kirsher { 3535a2cc190SJeff Kirsher int i; 3545a2cc190SJeff Kirsher 3555a2cc190SJeff Kirsher for (i = start; i <= end; i += MLX4_TABLE_CHUNK_SIZE / table->obj_size) 3565a2cc190SJeff Kirsher mlx4_table_put(dev, table, i); 3575a2cc190SJeff Kirsher } 3585a2cc190SJeff Kirsher 3595a2cc190SJeff Kirsher int mlx4_init_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table, 3605a2cc190SJeff Kirsher u64 virt, int obj_size, int nobj, int reserved, 3615a2cc190SJeff Kirsher int use_lowmem, int use_coherent) 3625a2cc190SJeff Kirsher { 3635a2cc190SJeff Kirsher int obj_per_chunk; 3645a2cc190SJeff Kirsher int num_icm; 3655a2cc190SJeff Kirsher unsigned chunk_size; 3665a2cc190SJeff Kirsher int i; 3675a2cc190SJeff Kirsher 3685a2cc190SJeff Kirsher obj_per_chunk = MLX4_TABLE_CHUNK_SIZE / obj_size; 3695a2cc190SJeff Kirsher num_icm = (nobj + obj_per_chunk - 1) / obj_per_chunk; 3705a2cc190SJeff Kirsher 3715a2cc190SJeff Kirsher table->icm = kcalloc(num_icm, sizeof *table->icm, GFP_KERNEL); 3725a2cc190SJeff Kirsher if (!table->icm) 3735a2cc190SJeff Kirsher return -ENOMEM; 3745a2cc190SJeff Kirsher table->virt = virt; 3755a2cc190SJeff Kirsher table->num_icm = num_icm; 3765a2cc190SJeff Kirsher table->num_obj = nobj; 3775a2cc190SJeff Kirsher table->obj_size = obj_size; 3785a2cc190SJeff Kirsher table->lowmem = use_lowmem; 3795a2cc190SJeff Kirsher table->coherent = use_coherent; 3805a2cc190SJeff Kirsher mutex_init(&table->mutex); 3815a2cc190SJeff Kirsher 3825a2cc190SJeff Kirsher for (i = 0; i * MLX4_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) { 3835a2cc190SJeff Kirsher chunk_size = MLX4_TABLE_CHUNK_SIZE; 3845a2cc190SJeff Kirsher if ((i + 1) * MLX4_TABLE_CHUNK_SIZE > nobj * obj_size) 3855a2cc190SJeff Kirsher chunk_size = PAGE_ALIGN(nobj * obj_size - i * MLX4_TABLE_CHUNK_SIZE); 3865a2cc190SJeff Kirsher 3875a2cc190SJeff Kirsher table->icm[i] = mlx4_alloc_icm(dev, chunk_size >> PAGE_SHIFT, 3885a2cc190SJeff Kirsher (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) | 3895a2cc190SJeff Kirsher __GFP_NOWARN, use_coherent); 3905a2cc190SJeff Kirsher if (!table->icm[i]) 3915a2cc190SJeff Kirsher goto err; 3925a2cc190SJeff Kirsher if (mlx4_MAP_ICM(dev, table->icm[i], virt + i * MLX4_TABLE_CHUNK_SIZE)) { 3935a2cc190SJeff Kirsher mlx4_free_icm(dev, table->icm[i], use_coherent); 3945a2cc190SJeff Kirsher table->icm[i] = NULL; 3955a2cc190SJeff Kirsher goto err; 3965a2cc190SJeff Kirsher } 3975a2cc190SJeff Kirsher 3985a2cc190SJeff Kirsher /* 3995a2cc190SJeff Kirsher * Add a reference to this ICM chunk so that it never 4005a2cc190SJeff Kirsher * gets freed (since it contains reserved firmware objects). 4015a2cc190SJeff Kirsher */ 4025a2cc190SJeff Kirsher ++table->icm[i]->refcount; 4035a2cc190SJeff Kirsher } 4045a2cc190SJeff Kirsher 4055a2cc190SJeff Kirsher return 0; 4065a2cc190SJeff Kirsher 4075a2cc190SJeff Kirsher err: 4085a2cc190SJeff Kirsher for (i = 0; i < num_icm; ++i) 4095a2cc190SJeff Kirsher if (table->icm[i]) { 4105a2cc190SJeff Kirsher mlx4_UNMAP_ICM(dev, virt + i * MLX4_TABLE_CHUNK_SIZE, 4115a2cc190SJeff Kirsher MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE); 4125a2cc190SJeff Kirsher mlx4_free_icm(dev, table->icm[i], use_coherent); 4135a2cc190SJeff Kirsher } 4145a2cc190SJeff Kirsher 4155a2cc190SJeff Kirsher return -ENOMEM; 4165a2cc190SJeff Kirsher } 4175a2cc190SJeff Kirsher 4185a2cc190SJeff Kirsher void mlx4_cleanup_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table) 4195a2cc190SJeff Kirsher { 4205a2cc190SJeff Kirsher int i; 4215a2cc190SJeff Kirsher 4225a2cc190SJeff Kirsher for (i = 0; i < table->num_icm; ++i) 4235a2cc190SJeff Kirsher if (table->icm[i]) { 4245a2cc190SJeff Kirsher mlx4_UNMAP_ICM(dev, table->virt + i * MLX4_TABLE_CHUNK_SIZE, 4255a2cc190SJeff Kirsher MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE); 4265a2cc190SJeff Kirsher mlx4_free_icm(dev, table->icm[i], table->coherent); 4275a2cc190SJeff Kirsher } 4285a2cc190SJeff Kirsher 4295a2cc190SJeff Kirsher kfree(table->icm); 4305a2cc190SJeff Kirsher } 431