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 /*
46885892fbSEric Dumazet  * We allocate in as big chunks as we can, up to a maximum of 256 KB
47885892fbSEric Dumazet  * per chunk. Note that the chunks are not necessarily in contiguous
48885892fbSEric Dumazet  * physical memory.
495a2cc190SJeff Kirsher  */
505a2cc190SJeff Kirsher enum {
51885892fbSEric Dumazet 	MLX4_ICM_ALLOC_SIZE	= 1 << 18,
52885892fbSEric Dumazet 	MLX4_TABLE_CHUNK_SIZE	= 1 << 18,
535a2cc190SJeff Kirsher };
545a2cc190SJeff Kirsher 
mlx4_free_icm_pages(struct mlx4_dev * dev,struct mlx4_icm_chunk * chunk)555a2cc190SJeff Kirsher static void mlx4_free_icm_pages(struct mlx4_dev *dev, struct mlx4_icm_chunk *chunk)
565a2cc190SJeff Kirsher {
575a2cc190SJeff Kirsher 	int i;
585a2cc190SJeff Kirsher 
595a2cc190SJeff Kirsher 	if (chunk->nsg > 0)
6001cd364aSStephen Warren 		dma_unmap_sg(&dev->persist->pdev->dev, chunk->sg, chunk->npages,
6101cd364aSStephen Warren 			     DMA_BIDIRECTIONAL);
625a2cc190SJeff Kirsher 
635a2cc190SJeff Kirsher 	for (i = 0; i < chunk->npages; ++i)
64f65e192aSStephen Warren 		__free_pages(sg_page(&chunk->sg[i]),
65f65e192aSStephen Warren 			     get_order(chunk->sg[i].length));
665a2cc190SJeff Kirsher }
675a2cc190SJeff Kirsher 
mlx4_free_icm_coherent(struct mlx4_dev * dev,struct mlx4_icm_chunk * chunk)685a2cc190SJeff Kirsher static void mlx4_free_icm_coherent(struct mlx4_dev *dev, struct mlx4_icm_chunk *chunk)
695a2cc190SJeff Kirsher {
705a2cc190SJeff Kirsher 	int i;
715a2cc190SJeff Kirsher 
725a2cc190SJeff Kirsher 	for (i = 0; i < chunk->npages; ++i)
73872bf2fbSYishai Hadas 		dma_free_coherent(&dev->persist->pdev->dev,
74f65e192aSStephen Warren 				  chunk->buf[i].size,
75f65e192aSStephen Warren 				  chunk->buf[i].addr,
76f65e192aSStephen Warren 				  chunk->buf[i].dma_addr);
775a2cc190SJeff Kirsher }
785a2cc190SJeff Kirsher 
mlx4_free_icm(struct mlx4_dev * dev,struct mlx4_icm * icm,int coherent)795a2cc190SJeff Kirsher void mlx4_free_icm(struct mlx4_dev *dev, struct mlx4_icm *icm, int coherent)
805a2cc190SJeff Kirsher {
815a2cc190SJeff Kirsher 	struct mlx4_icm_chunk *chunk, *tmp;
825a2cc190SJeff Kirsher 
835a2cc190SJeff Kirsher 	if (!icm)
845a2cc190SJeff Kirsher 		return;
855a2cc190SJeff Kirsher 
865a2cc190SJeff Kirsher 	list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
875a2cc190SJeff Kirsher 		if (coherent)
885a2cc190SJeff Kirsher 			mlx4_free_icm_coherent(dev, chunk);
895a2cc190SJeff Kirsher 		else
905a2cc190SJeff Kirsher 			mlx4_free_icm_pages(dev, chunk);
915a2cc190SJeff Kirsher 
925a2cc190SJeff Kirsher 		kfree(chunk);
935a2cc190SJeff Kirsher 	}
945a2cc190SJeff Kirsher 
955a2cc190SJeff Kirsher 	kfree(icm);
965a2cc190SJeff Kirsher }
975a2cc190SJeff Kirsher 
mlx4_alloc_icm_pages(struct scatterlist * mem,int order,gfp_t gfp_mask,int node)986e7136edSEugenia Emantayev static int mlx4_alloc_icm_pages(struct scatterlist *mem, int order,
996e7136edSEugenia Emantayev 				gfp_t gfp_mask, int node)
1005a2cc190SJeff Kirsher {
1015a2cc190SJeff Kirsher 	struct page *page;
1025a2cc190SJeff Kirsher 
1036e7136edSEugenia Emantayev 	page = alloc_pages_node(node, gfp_mask, order);
1046e7136edSEugenia Emantayev 	if (!page) {
1055a2cc190SJeff Kirsher 		page = alloc_pages(gfp_mask, order);
1065a2cc190SJeff Kirsher 		if (!page)
1075a2cc190SJeff Kirsher 			return -ENOMEM;
1086e7136edSEugenia Emantayev 	}
1095a2cc190SJeff Kirsher 
1105a2cc190SJeff Kirsher 	sg_set_page(mem, page, PAGE_SIZE << order, 0);
1115a2cc190SJeff Kirsher 	return 0;
1125a2cc190SJeff Kirsher }
1135a2cc190SJeff Kirsher 
mlx4_alloc_icm_coherent(struct device * dev,struct mlx4_icm_buf * buf,int order,gfp_t gfp_mask)114f65e192aSStephen Warren static int mlx4_alloc_icm_coherent(struct device *dev, struct mlx4_icm_buf *buf,
1155a2cc190SJeff Kirsher 				   int order, gfp_t gfp_mask)
1165a2cc190SJeff Kirsher {
117f65e192aSStephen Warren 	buf->addr = dma_alloc_coherent(dev, PAGE_SIZE << order,
118f65e192aSStephen Warren 				       &buf->dma_addr, gfp_mask);
119f65e192aSStephen Warren 	if (!buf->addr)
1205a2cc190SJeff Kirsher 		return -ENOMEM;
1215a2cc190SJeff Kirsher 
122f65e192aSStephen Warren 	if (offset_in_page(buf->addr)) {
123f65e192aSStephen Warren 		dma_free_coherent(dev, PAGE_SIZE << order, buf->addr,
124f65e192aSStephen Warren 				  buf->dma_addr);
125c1d5f8ffSLeon Romanovsky 		return -ENOMEM;
126c1d5f8ffSLeon Romanovsky 	}
127c1d5f8ffSLeon Romanovsky 
128f65e192aSStephen Warren 	buf->size = PAGE_SIZE << order;
1295a2cc190SJeff Kirsher 	return 0;
1305a2cc190SJeff Kirsher }
1315a2cc190SJeff Kirsher 
mlx4_alloc_icm(struct mlx4_dev * dev,int npages,gfp_t gfp_mask,int coherent)1325a2cc190SJeff Kirsher struct mlx4_icm *mlx4_alloc_icm(struct mlx4_dev *dev, int npages,
1335a2cc190SJeff Kirsher 				gfp_t gfp_mask, int coherent)
1345a2cc190SJeff Kirsher {
1355a2cc190SJeff Kirsher 	struct mlx4_icm *icm;
1365a2cc190SJeff Kirsher 	struct mlx4_icm_chunk *chunk = NULL;
1375a2cc190SJeff Kirsher 	int cur_order;
138885892fbSEric Dumazet 	gfp_t mask;
1395a2cc190SJeff Kirsher 	int ret;
1405a2cc190SJeff Kirsher 
1415a2cc190SJeff Kirsher 	/* We use sg_set_buf for coherent allocs, which assumes low memory */
1425a2cc190SJeff Kirsher 	BUG_ON(coherent && (gfp_mask & __GFP_HIGHMEM));
1435a2cc190SJeff Kirsher 
1446e7136edSEugenia Emantayev 	icm = kmalloc_node(sizeof(*icm),
1456e7136edSEugenia Emantayev 			   gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN),
1466e7136edSEugenia Emantayev 			   dev->numa_node);
1476e7136edSEugenia Emantayev 	if (!icm) {
1486e7136edSEugenia Emantayev 		icm = kmalloc(sizeof(*icm),
1496e7136edSEugenia Emantayev 			      gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
1505a2cc190SJeff Kirsher 		if (!icm)
1515a2cc190SJeff Kirsher 			return NULL;
1526e7136edSEugenia Emantayev 	}
1535a2cc190SJeff Kirsher 
1545a2cc190SJeff Kirsher 	icm->refcount = 0;
1555a2cc190SJeff Kirsher 	INIT_LIST_HEAD(&icm->chunk_list);
1565a2cc190SJeff Kirsher 
1575a2cc190SJeff Kirsher 	cur_order = get_order(MLX4_ICM_ALLOC_SIZE);
1585a2cc190SJeff Kirsher 
1595a2cc190SJeff Kirsher 	while (npages > 0) {
1605a2cc190SJeff Kirsher 		if (!chunk) {
161f65e192aSStephen Warren 			chunk = kzalloc_node(sizeof(*chunk),
1626e7136edSEugenia Emantayev 					     gfp_mask & ~(__GFP_HIGHMEM |
1636e7136edSEugenia Emantayev 							  __GFP_NOWARN),
1646e7136edSEugenia Emantayev 					     dev->numa_node);
1656e7136edSEugenia Emantayev 			if (!chunk) {
166f65e192aSStephen Warren 				chunk = kzalloc(sizeof(*chunk),
1676e7136edSEugenia Emantayev 						gfp_mask & ~(__GFP_HIGHMEM |
1686e7136edSEugenia Emantayev 							     __GFP_NOWARN));
1695a2cc190SJeff Kirsher 				if (!chunk)
1705a2cc190SJeff Kirsher 					goto fail;
1716e7136edSEugenia Emantayev 			}
172f65e192aSStephen Warren 			chunk->coherent = coherent;
1735a2cc190SJeff Kirsher 
174f65e192aSStephen Warren 			if (!coherent)
175f65e192aSStephen Warren 				sg_init_table(chunk->sg, MLX4_ICM_CHUNK_LEN);
1765a2cc190SJeff Kirsher 			list_add_tail(&chunk->list, &icm->chunk_list);
1775a2cc190SJeff Kirsher 		}
1785a2cc190SJeff Kirsher 
1795a2cc190SJeff Kirsher 		while (1 << cur_order > npages)
1805a2cc190SJeff Kirsher 			--cur_order;
1815a2cc190SJeff Kirsher 
182885892fbSEric Dumazet 		mask = gfp_mask;
183885892fbSEric Dumazet 		if (cur_order)
184885892fbSEric Dumazet 			mask &= ~__GFP_DIRECT_RECLAIM;
185885892fbSEric Dumazet 
1865a2cc190SJeff Kirsher 		if (coherent)
187872bf2fbSYishai Hadas 			ret = mlx4_alloc_icm_coherent(&dev->persist->pdev->dev,
188f65e192aSStephen Warren 						&chunk->buf[chunk->npages],
189885892fbSEric Dumazet 						cur_order, mask);
1905a2cc190SJeff Kirsher 		else
191f65e192aSStephen Warren 			ret = mlx4_alloc_icm_pages(&chunk->sg[chunk->npages],
192885892fbSEric Dumazet 						   cur_order, mask,
1936e7136edSEugenia Emantayev 						   dev->numa_node);
1945a2cc190SJeff Kirsher 
1955a2cc190SJeff Kirsher 		if (ret) {
1965a2cc190SJeff Kirsher 			if (--cur_order < 0)
1975a2cc190SJeff Kirsher 				goto fail;
1985a2cc190SJeff Kirsher 			else
1995a2cc190SJeff Kirsher 				continue;
2005a2cc190SJeff Kirsher 		}
2015a2cc190SJeff Kirsher 
2025a2cc190SJeff Kirsher 		++chunk->npages;
2035a2cc190SJeff Kirsher 
2045a2cc190SJeff Kirsher 		if (coherent)
2055a2cc190SJeff Kirsher 			++chunk->nsg;
2065a2cc190SJeff Kirsher 		else if (chunk->npages == MLX4_ICM_CHUNK_LEN) {
20701cd364aSStephen Warren 			chunk->nsg = dma_map_sg(&dev->persist->pdev->dev,
20801cd364aSStephen Warren 						chunk->sg, chunk->npages,
20901cd364aSStephen Warren 						DMA_BIDIRECTIONAL);
2105a2cc190SJeff Kirsher 
211*0c1f77d8SJack Wang 			if (!chunk->nsg)
2125a2cc190SJeff Kirsher 				goto fail;
2135a2cc190SJeff Kirsher 		}
2145a2cc190SJeff Kirsher 
2155a2cc190SJeff Kirsher 		if (chunk->npages == MLX4_ICM_CHUNK_LEN)
2165a2cc190SJeff Kirsher 			chunk = NULL;
2175a2cc190SJeff Kirsher 
2185a2cc190SJeff Kirsher 		npages -= 1 << cur_order;
2195a2cc190SJeff Kirsher 	}
2205a2cc190SJeff Kirsher 
2215a2cc190SJeff Kirsher 	if (!coherent && chunk) {
22201cd364aSStephen Warren 		chunk->nsg = dma_map_sg(&dev->persist->pdev->dev, chunk->sg,
22301cd364aSStephen Warren 					chunk->npages, DMA_BIDIRECTIONAL);
2245a2cc190SJeff Kirsher 
225*0c1f77d8SJack Wang 		if (!chunk->nsg)
2265a2cc190SJeff Kirsher 			goto fail;
2275a2cc190SJeff Kirsher 	}
2285a2cc190SJeff Kirsher 
2295a2cc190SJeff Kirsher 	return icm;
2305a2cc190SJeff Kirsher 
2315a2cc190SJeff Kirsher fail:
2325a2cc190SJeff Kirsher 	mlx4_free_icm(dev, icm, coherent);
2335a2cc190SJeff Kirsher 	return NULL;
2345a2cc190SJeff Kirsher }
2355a2cc190SJeff Kirsher 
mlx4_MAP_ICM(struct mlx4_dev * dev,struct mlx4_icm * icm,u64 virt)2365a2cc190SJeff Kirsher static int mlx4_MAP_ICM(struct mlx4_dev *dev, struct mlx4_icm *icm, u64 virt)
2375a2cc190SJeff Kirsher {
2385a2cc190SJeff Kirsher 	return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM, icm, virt);
2395a2cc190SJeff Kirsher }
2405a2cc190SJeff Kirsher 
mlx4_UNMAP_ICM(struct mlx4_dev * dev,u64 virt,u32 page_count)2415a2cc190SJeff Kirsher static int mlx4_UNMAP_ICM(struct mlx4_dev *dev, u64 virt, u32 page_count)
2425a2cc190SJeff Kirsher {
2435a2cc190SJeff Kirsher 	return mlx4_cmd(dev, virt, page_count, 0, MLX4_CMD_UNMAP_ICM,
244f9baff50SJack Morgenstein 			MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
2455a2cc190SJeff Kirsher }
2465a2cc190SJeff Kirsher 
mlx4_MAP_ICM_AUX(struct mlx4_dev * dev,struct mlx4_icm * icm)2475a2cc190SJeff Kirsher int mlx4_MAP_ICM_AUX(struct mlx4_dev *dev, struct mlx4_icm *icm)
2485a2cc190SJeff Kirsher {
2495a2cc190SJeff Kirsher 	return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM_AUX, icm, -1);
2505a2cc190SJeff Kirsher }
2515a2cc190SJeff Kirsher 
mlx4_UNMAP_ICM_AUX(struct mlx4_dev * dev)2525a2cc190SJeff Kirsher int mlx4_UNMAP_ICM_AUX(struct mlx4_dev *dev)
2535a2cc190SJeff Kirsher {
254f9baff50SJack Morgenstein 	return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_UNMAP_ICM_AUX,
255f9baff50SJack Morgenstein 			MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
2565a2cc190SJeff Kirsher }
2575a2cc190SJeff Kirsher 
mlx4_table_get(struct mlx4_dev * dev,struct mlx4_icm_table * table,u32 obj)2588900b894SLeon Romanovsky int mlx4_table_get(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj)
2595a2cc190SJeff Kirsher {
260dd03e734SYishai Hadas 	u32 i = (obj & (table->num_obj - 1)) /
261dd03e734SYishai Hadas 			(MLX4_TABLE_CHUNK_SIZE / table->obj_size);
2625a2cc190SJeff Kirsher 	int ret = 0;
2635a2cc190SJeff Kirsher 
2645a2cc190SJeff Kirsher 	mutex_lock(&table->mutex);
2655a2cc190SJeff Kirsher 
2665a2cc190SJeff Kirsher 	if (table->icm[i]) {
2675a2cc190SJeff Kirsher 		++table->icm[i]->refcount;
2685a2cc190SJeff Kirsher 		goto out;
2695a2cc190SJeff Kirsher 	}
2705a2cc190SJeff Kirsher 
2715a2cc190SJeff Kirsher 	table->icm[i] = mlx4_alloc_icm(dev, MLX4_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
2728900b894SLeon Romanovsky 				       (table->lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
2735a2cc190SJeff Kirsher 				       __GFP_NOWARN, table->coherent);
2745a2cc190SJeff Kirsher 	if (!table->icm[i]) {
2755a2cc190SJeff Kirsher 		ret = -ENOMEM;
2765a2cc190SJeff Kirsher 		goto out;
2775a2cc190SJeff Kirsher 	}
2785a2cc190SJeff Kirsher 
2795a2cc190SJeff Kirsher 	if (mlx4_MAP_ICM(dev, table->icm[i], table->virt +
2805a2cc190SJeff Kirsher 			 (u64) i * MLX4_TABLE_CHUNK_SIZE)) {
2815a2cc190SJeff Kirsher 		mlx4_free_icm(dev, table->icm[i], table->coherent);
2825a2cc190SJeff Kirsher 		table->icm[i] = NULL;
2835a2cc190SJeff Kirsher 		ret = -ENOMEM;
2845a2cc190SJeff Kirsher 		goto out;
2855a2cc190SJeff Kirsher 	}
2865a2cc190SJeff Kirsher 
2875a2cc190SJeff Kirsher 	++table->icm[i]->refcount;
2885a2cc190SJeff Kirsher 
2895a2cc190SJeff Kirsher out:
2905a2cc190SJeff Kirsher 	mutex_unlock(&table->mutex);
2915a2cc190SJeff Kirsher 	return ret;
2925a2cc190SJeff Kirsher }
2935a2cc190SJeff Kirsher 
mlx4_table_put(struct mlx4_dev * dev,struct mlx4_icm_table * table,u32 obj)294dd03e734SYishai Hadas void mlx4_table_put(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj)
2955a2cc190SJeff Kirsher {
296dd03e734SYishai Hadas 	u32 i;
297dd03e734SYishai Hadas 	u64 offset;
2985a2cc190SJeff Kirsher 
2995a2cc190SJeff Kirsher 	i = (obj & (table->num_obj - 1)) / (MLX4_TABLE_CHUNK_SIZE / table->obj_size);
3005a2cc190SJeff Kirsher 
3015a2cc190SJeff Kirsher 	mutex_lock(&table->mutex);
3025a2cc190SJeff Kirsher 
3035a2cc190SJeff Kirsher 	if (--table->icm[i]->refcount == 0) {
304dd03e734SYishai Hadas 		offset = (u64) i * MLX4_TABLE_CHUNK_SIZE;
305dd03e734SYishai Hadas 		mlx4_UNMAP_ICM(dev, table->virt + offset,
3065a2cc190SJeff Kirsher 			       MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
3075a2cc190SJeff Kirsher 		mlx4_free_icm(dev, table->icm[i], table->coherent);
3085a2cc190SJeff Kirsher 		table->icm[i] = NULL;
3095a2cc190SJeff Kirsher 	}
3105a2cc190SJeff Kirsher 
3115a2cc190SJeff Kirsher 	mutex_unlock(&table->mutex);
3125a2cc190SJeff Kirsher }
3135a2cc190SJeff Kirsher 
mlx4_table_find(struct mlx4_icm_table * table,u32 obj,dma_addr_t * dma_handle)314dd03e734SYishai Hadas void *mlx4_table_find(struct mlx4_icm_table *table, u32 obj,
315dd03e734SYishai Hadas 			dma_addr_t *dma_handle)
3165a2cc190SJeff Kirsher {
317dd03e734SYishai Hadas 	int offset, dma_offset, i;
318dd03e734SYishai Hadas 	u64 idx;
3195a2cc190SJeff Kirsher 	struct mlx4_icm_chunk *chunk;
3205a2cc190SJeff Kirsher 	struct mlx4_icm *icm;
321f65e192aSStephen Warren 	void *addr = NULL;
3225a2cc190SJeff Kirsher 
3235a2cc190SJeff Kirsher 	if (!table->lowmem)
3245a2cc190SJeff Kirsher 		return NULL;
3255a2cc190SJeff Kirsher 
3265a2cc190SJeff Kirsher 	mutex_lock(&table->mutex);
3275a2cc190SJeff Kirsher 
328dd03e734SYishai Hadas 	idx = (u64) (obj & (table->num_obj - 1)) * table->obj_size;
3295a2cc190SJeff Kirsher 	icm = table->icm[idx / MLX4_TABLE_CHUNK_SIZE];
3305a2cc190SJeff Kirsher 	dma_offset = offset = idx % MLX4_TABLE_CHUNK_SIZE;
3315a2cc190SJeff Kirsher 
3325a2cc190SJeff Kirsher 	if (!icm)
3335a2cc190SJeff Kirsher 		goto out;
3345a2cc190SJeff Kirsher 
3355a2cc190SJeff Kirsher 	list_for_each_entry(chunk, &icm->chunk_list, list) {
3365a2cc190SJeff Kirsher 		for (i = 0; i < chunk->npages; ++i) {
337f65e192aSStephen Warren 			dma_addr_t dma_addr;
338f65e192aSStephen Warren 			size_t len;
339f65e192aSStephen Warren 
340f65e192aSStephen Warren 			if (table->coherent) {
341f65e192aSStephen Warren 				len = chunk->buf[i].size;
342f65e192aSStephen Warren 				dma_addr = chunk->buf[i].dma_addr;
343f65e192aSStephen Warren 				addr = chunk->buf[i].addr;
344f65e192aSStephen Warren 			} else {
345f65e192aSStephen Warren 				struct page *page;
346f65e192aSStephen Warren 
347f65e192aSStephen Warren 				len = sg_dma_len(&chunk->sg[i]);
348f65e192aSStephen Warren 				dma_addr = sg_dma_address(&chunk->sg[i]);
349f65e192aSStephen Warren 
350f65e192aSStephen Warren 				/* XXX: we should never do this for highmem
351f65e192aSStephen Warren 				 * allocation.  This function either needs
352f65e192aSStephen Warren 				 * to be split, or the kernel virtual address
353f65e192aSStephen Warren 				 * return needs to be made optional.
354f65e192aSStephen Warren 				 */
355f65e192aSStephen Warren 				page = sg_page(&chunk->sg[i]);
356f65e192aSStephen Warren 				addr = lowmem_page_address(page);
3575a2cc190SJeff Kirsher 			}
358f65e192aSStephen Warren 
359f65e192aSStephen Warren 			if (dma_handle && dma_offset >= 0) {
360f65e192aSStephen Warren 				if (len > dma_offset)
361f65e192aSStephen Warren 					*dma_handle = dma_addr + dma_offset;
362f65e192aSStephen Warren 				dma_offset -= len;
363f65e192aSStephen Warren 			}
364f65e192aSStephen Warren 
3655a2cc190SJeff Kirsher 			/*
3665a2cc190SJeff Kirsher 			 * DMA mapping can merge pages but not split them,
3675a2cc190SJeff Kirsher 			 * so if we found the page, dma_handle has already
3685a2cc190SJeff Kirsher 			 * been assigned to.
3695a2cc190SJeff Kirsher 			 */
370f65e192aSStephen Warren 			if (len > offset)
3715a2cc190SJeff Kirsher 				goto out;
372f65e192aSStephen Warren 			offset -= len;
3735a2cc190SJeff Kirsher 		}
3745a2cc190SJeff Kirsher 	}
3755a2cc190SJeff Kirsher 
376f65e192aSStephen Warren 	addr = NULL;
3775a2cc190SJeff Kirsher out:
3785a2cc190SJeff Kirsher 	mutex_unlock(&table->mutex);
379f65e192aSStephen Warren 	return addr ? addr + offset : NULL;
3805a2cc190SJeff Kirsher }
3815a2cc190SJeff Kirsher 
mlx4_table_get_range(struct mlx4_dev * dev,struct mlx4_icm_table * table,u32 start,u32 end)3825a2cc190SJeff Kirsher int mlx4_table_get_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
383dd03e734SYishai Hadas 			 u32 start, u32 end)
3845a2cc190SJeff Kirsher {
3855a2cc190SJeff Kirsher 	int inc = MLX4_TABLE_CHUNK_SIZE / table->obj_size;
386dd03e734SYishai Hadas 	int err;
387dd03e734SYishai Hadas 	u32 i;
3885a2cc190SJeff Kirsher 
3895a2cc190SJeff Kirsher 	for (i = start; i <= end; i += inc) {
3908900b894SLeon Romanovsky 		err = mlx4_table_get(dev, table, i);
3915a2cc190SJeff Kirsher 		if (err)
3925a2cc190SJeff Kirsher 			goto fail;
3935a2cc190SJeff Kirsher 	}
3945a2cc190SJeff Kirsher 
3955a2cc190SJeff Kirsher 	return 0;
3965a2cc190SJeff Kirsher 
3975a2cc190SJeff Kirsher fail:
3985a2cc190SJeff Kirsher 	while (i > start) {
3995a2cc190SJeff Kirsher 		i -= inc;
4005a2cc190SJeff Kirsher 		mlx4_table_put(dev, table, i);
4015a2cc190SJeff Kirsher 	}
4025a2cc190SJeff Kirsher 
4035a2cc190SJeff Kirsher 	return err;
4045a2cc190SJeff Kirsher }
4055a2cc190SJeff Kirsher 
mlx4_table_put_range(struct mlx4_dev * dev,struct mlx4_icm_table * table,u32 start,u32 end)4065a2cc190SJeff Kirsher void mlx4_table_put_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
407dd03e734SYishai Hadas 			  u32 start, u32 end)
4085a2cc190SJeff Kirsher {
409dd03e734SYishai Hadas 	u32 i;
4105a2cc190SJeff Kirsher 
4115a2cc190SJeff Kirsher 	for (i = start; i <= end; i += MLX4_TABLE_CHUNK_SIZE / table->obj_size)
4125a2cc190SJeff Kirsher 		mlx4_table_put(dev, table, i);
4135a2cc190SJeff Kirsher }
4145a2cc190SJeff Kirsher 
mlx4_init_icm_table(struct mlx4_dev * dev,struct mlx4_icm_table * table,u64 virt,int obj_size,u32 nobj,int reserved,int use_lowmem,int use_coherent)4155a2cc190SJeff Kirsher int mlx4_init_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table,
4163de819e6SYishai Hadas 			u64 virt, int obj_size,	u32 nobj, int reserved,
4175a2cc190SJeff Kirsher 			int use_lowmem, int use_coherent)
4185a2cc190SJeff Kirsher {
4195a2cc190SJeff Kirsher 	int obj_per_chunk;
4205a2cc190SJeff Kirsher 	int num_icm;
4215a2cc190SJeff Kirsher 	unsigned chunk_size;
4225a2cc190SJeff Kirsher 	int i;
4233de819e6SYishai Hadas 	u64 size;
4245a2cc190SJeff Kirsher 
4255a2cc190SJeff Kirsher 	obj_per_chunk = MLX4_TABLE_CHUNK_SIZE / obj_size;
4261383cb81SQing Huang 	if (WARN_ON(!obj_per_chunk))
4271383cb81SQing Huang 		return -EINVAL;
428f8a1988fSzhong jiang 	num_icm = DIV_ROUND_UP(nobj, obj_per_chunk);
4295a2cc190SJeff Kirsher 
430778e1cddSKees Cook 	table->icm      = kvcalloc(num_icm, sizeof(*table->icm), GFP_KERNEL);
4315a2cc190SJeff Kirsher 	if (!table->icm)
4325a2cc190SJeff Kirsher 		return -ENOMEM;
4335a2cc190SJeff Kirsher 	table->virt     = virt;
4345a2cc190SJeff Kirsher 	table->num_icm  = num_icm;
4355a2cc190SJeff Kirsher 	table->num_obj  = nobj;
4365a2cc190SJeff Kirsher 	table->obj_size = obj_size;
4375a2cc190SJeff Kirsher 	table->lowmem   = use_lowmem;
4385a2cc190SJeff Kirsher 	table->coherent = use_coherent;
4395a2cc190SJeff Kirsher 	mutex_init(&table->mutex);
4405a2cc190SJeff Kirsher 
4413de819e6SYishai Hadas 	size = (u64) nobj * obj_size;
4425a2cc190SJeff Kirsher 	for (i = 0; i * MLX4_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) {
4435a2cc190SJeff Kirsher 		chunk_size = MLX4_TABLE_CHUNK_SIZE;
4443de819e6SYishai Hadas 		if ((i + 1) * MLX4_TABLE_CHUNK_SIZE > size)
4453de819e6SYishai Hadas 			chunk_size = PAGE_ALIGN(size -
4463de819e6SYishai Hadas 					i * MLX4_TABLE_CHUNK_SIZE);
4475a2cc190SJeff Kirsher 
4485a2cc190SJeff Kirsher 		table->icm[i] = mlx4_alloc_icm(dev, chunk_size >> PAGE_SHIFT,
4495a2cc190SJeff Kirsher 					       (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
4505a2cc190SJeff Kirsher 					       __GFP_NOWARN, use_coherent);
4515a2cc190SJeff Kirsher 		if (!table->icm[i])
4525a2cc190SJeff Kirsher 			goto err;
4535a2cc190SJeff Kirsher 		if (mlx4_MAP_ICM(dev, table->icm[i], virt + i * MLX4_TABLE_CHUNK_SIZE)) {
4545a2cc190SJeff Kirsher 			mlx4_free_icm(dev, table->icm[i], use_coherent);
4555a2cc190SJeff Kirsher 			table->icm[i] = NULL;
4565a2cc190SJeff Kirsher 			goto err;
4575a2cc190SJeff Kirsher 		}
4585a2cc190SJeff Kirsher 
4595a2cc190SJeff Kirsher 		/*
4605a2cc190SJeff Kirsher 		 * Add a reference to this ICM chunk so that it never
4615a2cc190SJeff Kirsher 		 * gets freed (since it contains reserved firmware objects).
4625a2cc190SJeff Kirsher 		 */
4635a2cc190SJeff Kirsher 		++table->icm[i]->refcount;
4645a2cc190SJeff Kirsher 	}
4655a2cc190SJeff Kirsher 
4665a2cc190SJeff Kirsher 	return 0;
4675a2cc190SJeff Kirsher 
4685a2cc190SJeff Kirsher err:
4695a2cc190SJeff Kirsher 	for (i = 0; i < num_icm; ++i)
4705a2cc190SJeff Kirsher 		if (table->icm[i]) {
4715a2cc190SJeff Kirsher 			mlx4_UNMAP_ICM(dev, virt + i * MLX4_TABLE_CHUNK_SIZE,
4725a2cc190SJeff Kirsher 				       MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
4735a2cc190SJeff Kirsher 			mlx4_free_icm(dev, table->icm[i], use_coherent);
4745a2cc190SJeff Kirsher 		}
4755a2cc190SJeff Kirsher 
4761383cb81SQing Huang 	kvfree(table->icm);
477240a9207SDotan Barak 
4785a2cc190SJeff Kirsher 	return -ENOMEM;
4795a2cc190SJeff Kirsher }
4805a2cc190SJeff Kirsher 
mlx4_cleanup_icm_table(struct mlx4_dev * dev,struct mlx4_icm_table * table)4815a2cc190SJeff Kirsher void mlx4_cleanup_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table)
4825a2cc190SJeff Kirsher {
4835a2cc190SJeff Kirsher 	int i;
4845a2cc190SJeff Kirsher 
4855a2cc190SJeff Kirsher 	for (i = 0; i < table->num_icm; ++i)
4865a2cc190SJeff Kirsher 		if (table->icm[i]) {
4875a2cc190SJeff Kirsher 			mlx4_UNMAP_ICM(dev, table->virt + i * MLX4_TABLE_CHUNK_SIZE,
4885a2cc190SJeff Kirsher 				       MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
4895a2cc190SJeff Kirsher 			mlx4_free_icm(dev, table->icm[i], table->coherent);
4905a2cc190SJeff Kirsher 		}
4915a2cc190SJeff Kirsher 
4921383cb81SQing Huang 	kvfree(table->icm);
4935a2cc190SJeff Kirsher }
494