Release 4.7 drivers/net/ethernet/mellanox/mlx4/icm.c
  
  
/*
 * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
 * Copyright (c) 2006, 2007 Cisco Systems, Inc.  All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/mlx4/cmd.h>
#include "mlx4.h"
#include "icm.h"
#include "fw.h"
/*
 * We allocate in as big chunks as we can, up to a maximum of 256 KB
 * per chunk.
 */
enum {
	
MLX4_ICM_ALLOC_SIZE	= 1 << 18,
	
MLX4_TABLE_CHUNK_SIZE	= 1 << 18
};
static void mlx4_free_icm_pages(struct mlx4_dev *dev, struct mlx4_icm_chunk *chunk)
{
	int i;
	if (chunk->nsg > 0)
		pci_unmap_sg(dev->persist->pdev, chunk->mem, chunk->npages,
			     PCI_DMA_BIDIRECTIONAL);
	for (i = 0; i < chunk->npages; ++i)
		__free_pages(sg_page(&chunk->mem[i]),
			     get_order(chunk->mem[i].length));
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 76 | 87.36% | 1 | 25.00% | 
| jack morgenstein | jack morgenstein | 5 | 5.75% | 1 | 25.00% | 
| jens axboe | jens axboe | 4 | 4.60% | 1 | 25.00% | 
| yishai hadas | yishai hadas | 2 | 2.30% | 1 | 25.00% | 
 | Total | 87 | 100.00% | 4 | 100.00% | 
static void mlx4_free_icm_coherent(struct mlx4_dev *dev, struct mlx4_icm_chunk *chunk)
{
	int i;
	for (i = 0; i < chunk->npages; ++i)
		dma_free_coherent(&dev->persist->pdev->dev,
				  chunk->mem[i].length,
				  lowmem_page_address(sg_page(&chunk->mem[i])),
				  sg_dma_address(&chunk->mem[i]));
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| jack morgenstein | jack morgenstein | 74 | 92.50% | 1 | 33.33% | 
| jens axboe | jens axboe | 4 | 5.00% | 1 | 33.33% | 
| yishai hadas | yishai hadas | 2 | 2.50% | 1 | 33.33% | 
 | Total | 80 | 100.00% | 3 | 100.00% | 
void mlx4_free_icm(struct mlx4_dev *dev, struct mlx4_icm *icm, int coherent)
{
	struct mlx4_icm_chunk *chunk, *tmp;
	if (!icm)
		return;
	list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
		if (coherent)
			mlx4_free_icm_coherent(dev, chunk);
		else
			mlx4_free_icm_pages(dev, chunk);
		kfree(chunk);
	}
	kfree(icm);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| jack morgenstein | jack morgenstein | 61 | 83.56% | 1 | 50.00% | 
| roland dreier | roland dreier | 12 | 16.44% | 1 | 50.00% | 
 | Total | 73 | 100.00% | 2 | 100.00% | 
static int mlx4_alloc_icm_pages(struct scatterlist *mem, int order,
				gfp_t gfp_mask, int node)
{
	struct page *page;
	page = alloc_pages_node(node, gfp_mask, order);
	if (!page) {
		page = alloc_pages(gfp_mask, order);
		if (!page)
			return -ENOMEM;
	}
	sg_set_page(mem, page, PAGE_SIZE << order, 0);
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| jack morgenstein | jack morgenstein | 43 | 55.84% | 1 | 25.00% | 
| eugenia emantayev | eugenia emantayev | 21 | 27.27% | 1 | 25.00% | 
| jens axboe | jens axboe | 13 | 16.88% | 2 | 50.00% | 
 | Total | 77 | 100.00% | 4 | 100.00% | 
static int mlx4_alloc_icm_coherent(struct device *dev, struct scatterlist *mem,
				    int order, gfp_t gfp_mask)
{
	void *buf = dma_alloc_coherent(dev, PAGE_SIZE << order,
				       &sg_dma_address(mem), gfp_mask);
	if (!buf)
		return -ENOMEM;
	sg_set_buf(mem, buf, PAGE_SIZE << order);
	BUG_ON(mem->offset);
	sg_dma_len(mem) = PAGE_SIZE << order;
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| jack morgenstein | jack morgenstein | 82 | 100.00% | 1 | 100.00% | 
 | Total | 82 | 100.00% | 1 | 100.00% | 
struct mlx4_icm *mlx4_alloc_icm(struct mlx4_dev *dev, int npages,
				gfp_t gfp_mask, int coherent)
{
	struct mlx4_icm *icm;
	struct mlx4_icm_chunk *chunk = NULL;
	int cur_order;
	int ret;
	/* We use sg_set_buf for coherent allocs, which assumes low memory */
	BUG_ON(coherent && (gfp_mask & __GFP_HIGHMEM));
	icm = kmalloc_node(sizeof(*icm),
			   gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN),
			   dev->numa_node);
	if (!icm) {
		icm = kmalloc(sizeof(*icm),
			      gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
		if (!icm)
			return NULL;
	}
	icm->refcount = 0;
	INIT_LIST_HEAD(&icm->chunk_list);
	cur_order = get_order(MLX4_ICM_ALLOC_SIZE);
	while (npages > 0) {
		if (!chunk) {
			chunk = kmalloc_node(sizeof(*chunk),
					     gfp_mask & ~(__GFP_HIGHMEM |
							  __GFP_NOWARN),
					     dev->numa_node);
			if (!chunk) {
				chunk = kmalloc(sizeof(*chunk),
						gfp_mask & ~(__GFP_HIGHMEM |
							     __GFP_NOWARN));
				if (!chunk)
					goto fail;
			}
			sg_init_table(chunk->mem, MLX4_ICM_CHUNK_LEN);
			chunk->npages = 0;
			chunk->nsg    = 0;
			list_add_tail(&chunk->list, &icm->chunk_list);
		}
		while (1 << cur_order > npages)
			--cur_order;
		if (coherent)
			ret = mlx4_alloc_icm_coherent(&dev->persist->pdev->dev,
						      &chunk->mem[chunk->npages],
						      cur_order, gfp_mask);
		else
			ret = mlx4_alloc_icm_pages(&chunk->mem[chunk->npages],
						   cur_order, gfp_mask,
						   dev->numa_node);
		if (ret) {
			if (--cur_order < 0)
				goto fail;
			else
				continue;
		}
		++chunk->npages;
		if (coherent)
			++chunk->nsg;
		else if (chunk->npages == MLX4_ICM_CHUNK_LEN) {
			chunk->nsg = pci_map_sg(dev->persist->pdev, chunk->mem,
						chunk->npages,
						PCI_DMA_BIDIRECTIONAL);
			if (chunk->nsg <= 0)
				goto fail;
		}
		if (chunk->npages == MLX4_ICM_CHUNK_LEN)
			chunk = NULL;
		npages -= 1 << cur_order;
	}
	if (!coherent && chunk) {
		chunk->nsg = pci_map_sg(dev->persist->pdev, chunk->mem,
					chunk->npages,
					PCI_DMA_BIDIRECTIONAL);
		if (chunk->nsg <= 0)
			goto fail;
	}
	return icm;
fail:
	mlx4_free_icm(dev, icm, coherent);
	return NULL;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 300 | 65.08% | 2 | 28.57% | 
| eugenia emantayev | eugenia emantayev | 72 | 15.62% | 1 | 14.29% | 
| jack morgenstein | jack morgenstein | 65 | 14.10% | 1 | 14.29% | 
| sebastien dugue | sebastien dugue | 9 | 1.95% | 1 | 14.29% | 
| jens axboe | jens axboe | 9 | 1.95% | 1 | 14.29% | 
| yishai hadas | yishai hadas | 6 | 1.30% | 1 | 14.29% | 
 | Total | 461 | 100.00% | 7 | 100.00% | 
static int mlx4_MAP_ICM(struct mlx4_dev *dev, struct mlx4_icm *icm, u64 virt)
{
	return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM, icm, virt);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 31 | 100.00% | 1 | 100.00% | 
 | Total | 31 | 100.00% | 1 | 100.00% | 
static int mlx4_UNMAP_ICM(struct mlx4_dev *dev, u64 virt, u32 page_count)
{
	return mlx4_cmd(dev, virt, page_count, 0, MLX4_CMD_UNMAP_ICM,
			MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 32 | 91.43% | 1 | 33.33% | 
| jack morgenstein | jack morgenstein | 2 | 5.71% | 1 | 33.33% | 
| stephen hemminger | stephen hemminger | 1 | 2.86% | 1 | 33.33% | 
 | Total | 35 | 100.00% | 3 | 100.00% | 
int mlx4_MAP_ICM_AUX(struct mlx4_dev *dev, struct mlx4_icm *icm)
{
	return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM_AUX, icm, -1);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 28 | 100.00% | 1 | 100.00% | 
 | Total | 28 | 100.00% | 1 | 100.00% | 
int mlx4_UNMAP_ICM_AUX(struct mlx4_dev *dev)
{
	return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_UNMAP_ICM_AUX,
			MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 26 | 92.86% | 1 | 50.00% | 
| jack morgenstein | jack morgenstein | 2 | 7.14% | 1 | 50.00% | 
 | Total | 28 | 100.00% | 2 | 100.00% | 
int mlx4_table_get(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj,
		   gfp_t gfp)
{
	u32 i = (obj & (table->num_obj - 1)) /
			(MLX4_TABLE_CHUNK_SIZE / table->obj_size);
	int ret = 0;
	mutex_lock(&table->mutex);
	if (table->icm[i]) {
		++table->icm[i]->refcount;
		goto out;
	}
	table->icm[i] = mlx4_alloc_icm(dev, MLX4_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
				       (table->lowmem ? gfp : GFP_HIGHUSER) |
				       __GFP_NOWARN, table->coherent);
	if (!table->icm[i]) {
		ret = -ENOMEM;
		goto out;
	}
	if (mlx4_MAP_ICM(dev, table->icm[i], table->virt +
			 (u64) i * MLX4_TABLE_CHUNK_SIZE)) {
		mlx4_free_icm(dev, table->icm[i], table->coherent);
		table->icm[i] = NULL;
		ret = -ENOMEM;
		goto out;
	}
	++table->icm[i]->refcount;
out:
	mutex_unlock(&table->mutex);
	return ret;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 203 | 93.98% | 2 | 40.00% | 
| jack morgenstein | jack morgenstein | 8 | 3.70% | 1 | 20.00% | 
| jiri kosina | jiri kosina | 3 | 1.39% | 1 | 20.00% | 
| yishai hadas | yishai hadas | 2 | 0.93% | 1 | 20.00% | 
 | Total | 216 | 100.00% | 5 | 100.00% | 
void mlx4_table_put(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj)
{
	u32 i;
	u64 offset;
	i = (obj & (table->num_obj - 1)) / (MLX4_TABLE_CHUNK_SIZE / table->obj_size);
	mutex_lock(&table->mutex);
	if (--table->icm[i]->refcount == 0) {
		offset = (u64) i * MLX4_TABLE_CHUNK_SIZE;
		mlx4_UNMAP_ICM(dev, table->virt + offset,
			       MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
		mlx4_free_icm(dev, table->icm[i], table->coherent);
		table->icm[i] = NULL;
	}
	mutex_unlock(&table->mutex);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 108 | 85.04% | 1 | 33.33% | 
| yishai hadas | yishai hadas | 15 | 11.81% | 1 | 33.33% | 
| jack morgenstein | jack morgenstein | 4 | 3.15% | 1 | 33.33% | 
 | Total | 127 | 100.00% | 3 | 100.00% | 
void *mlx4_table_find(struct mlx4_icm_table *table, u32 obj,
			dma_addr_t *dma_handle)
{
	int offset, dma_offset, i;
	u64 idx;
	struct mlx4_icm_chunk *chunk;
	struct mlx4_icm *icm;
	struct page *page = NULL;
	if (!table->lowmem)
		return NULL;
	mutex_lock(&table->mutex);
	idx = (u64) (obj & (table->num_obj - 1)) * table->obj_size;
	icm = table->icm[idx / MLX4_TABLE_CHUNK_SIZE];
	dma_offset = offset = idx % MLX4_TABLE_CHUNK_SIZE;
	if (!icm)
		goto out;
	list_for_each_entry(chunk, &icm->chunk_list, list) {
		for (i = 0; i < chunk->npages; ++i) {
			if (dma_handle && dma_offset >= 0) {
				if (sg_dma_len(&chunk->mem[i]) > dma_offset)
					*dma_handle = sg_dma_address(&chunk->mem[i]) +
						dma_offset;
				dma_offset -= sg_dma_len(&chunk->mem[i]);
			}
			/*
                         * DMA mapping can merge pages but not split them,
                         * so if we found the page, dma_handle has already
                         * been assigned to.
                         */
			if (chunk->mem[i].length > offset) {
				page = sg_page(&chunk->mem[i]);
				goto out;
			}
			offset -= chunk->mem[i].length;
		}
	}
out:
	mutex_unlock(&table->mutex);
	return page ? lowmem_page_address(page) + offset : NULL;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 177 | 68.87% | 1 | 25.00% | 
| jack morgenstein | jack morgenstein | 67 | 26.07% | 1 | 25.00% | 
| yishai hadas | yishai hadas | 9 | 3.50% | 1 | 25.00% | 
| jens axboe | jens axboe | 4 | 1.56% | 1 | 25.00% | 
 | Total | 257 | 100.00% | 4 | 100.00% | 
int mlx4_table_get_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
			 u32 start, u32 end)
{
	int inc = MLX4_TABLE_CHUNK_SIZE / table->obj_size;
	int err;
	u32 i;
	for (i = start; i <= end; i += inc) {
		err = mlx4_table_get(dev, table, i, GFP_KERNEL);
		if (err)
			goto fail;
	}
	return 0;
fail:
	while (i > start) {
		i -= inc;
		mlx4_table_put(dev, table, i);
	}
	return err;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 94 | 93.07% | 1 | 33.33% | 
| yishai hadas | yishai hadas | 5 | 4.95% | 1 | 33.33% | 
| jiri kosina | jiri kosina | 2 | 1.98% | 1 | 33.33% | 
 | Total | 101 | 100.00% | 3 | 100.00% | 
void mlx4_table_put_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
			  u32 start, u32 end)
{
	u32 i;
	for (i = start; i <= end; i += MLX4_TABLE_CHUNK_SIZE / table->obj_size)
		mlx4_table_put(dev, table, i);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 48 | 94.12% | 1 | 50.00% | 
| yishai hadas | yishai hadas | 3 | 5.88% | 1 | 50.00% | 
 | Total | 51 | 100.00% | 2 | 100.00% | 
int 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)
{
	int obj_per_chunk;
	int num_icm;
	unsigned chunk_size;
	int i;
	u64 size;
	obj_per_chunk = MLX4_TABLE_CHUNK_SIZE / obj_size;
	num_icm = (nobj + obj_per_chunk - 1) / obj_per_chunk;
	table->icm      = kcalloc(num_icm, sizeof *table->icm, GFP_KERNEL);
	if (!table->icm)
		return -ENOMEM;
	table->virt     = virt;
	table->num_icm  = num_icm;
	table->num_obj  = nobj;
	table->obj_size = obj_size;
	table->lowmem   = use_lowmem;
	table->coherent = use_coherent;
	mutex_init(&table->mutex);
	size = (u64) nobj * obj_size;
	for (i = 0; i * MLX4_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) {
		chunk_size = MLX4_TABLE_CHUNK_SIZE;
		if ((i + 1) * MLX4_TABLE_CHUNK_SIZE > size)
			chunk_size = PAGE_ALIGN(size -
					i * MLX4_TABLE_CHUNK_SIZE);
		table->icm[i] = mlx4_alloc_icm(dev, chunk_size >> PAGE_SHIFT,
					       (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
					       __GFP_NOWARN, use_coherent);
		if (!table->icm[i])
			goto err;
		if (mlx4_MAP_ICM(dev, table->icm[i], virt + i * MLX4_TABLE_CHUNK_SIZE)) {
			mlx4_free_icm(dev, table->icm[i], use_coherent);
			table->icm[i] = NULL;
			goto err;
		}
		/*
                 * Add a reference to this ICM chunk so that it never
                 * gets freed (since it contains reserved firmware objects).
                 */
		++table->icm[i]->refcount;
	}
	return 0;
err:
	for (i = 0; i < num_icm; ++i)
		if (table->icm[i]) {
			mlx4_UNMAP_ICM(dev, virt + i * MLX4_TABLE_CHUNK_SIZE,
				       MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
			mlx4_free_icm(dev, table->icm[i], use_coherent);
		}
	kfree(table->icm);
	return -ENOMEM;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 325 | 89.78% | 1 | 25.00% | 
| jack morgenstein | jack morgenstein | 15 | 4.14% | 1 | 25.00% | 
| yishai hadas | yishai hadas | 15 | 4.14% | 1 | 25.00% | 
| dotan barak | dotan barak | 7 | 1.93% | 1 | 25.00% | 
 | Total | 362 | 100.00% | 4 | 100.00% | 
void mlx4_cleanup_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table)
{
	int i;
	for (i = 0; i < table->num_icm; ++i)
		if (table->icm[i]) {
			mlx4_UNMAP_ICM(dev, table->virt + i * MLX4_TABLE_CHUNK_SIZE,
				       MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
			mlx4_free_icm(dev, table->icm[i], table->coherent);
		}
	kfree(table->icm);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 80 | 95.24% | 1 | 50.00% | 
| jack morgenstein | jack morgenstein | 4 | 4.76% | 1 | 50.00% | 
 | Total | 84 | 100.00% | 2 | 100.00% | 
Overall Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| roland dreier | roland dreier | 1571 | 70.73% | 3 | 15.79% | 
| jack morgenstein | jack morgenstein | 436 | 19.63% | 4 | 21.05% | 
| eugenia emantayev | eugenia emantayev | 93 | 4.19% | 1 | 5.26% | 
| yishai hadas | yishai hadas | 59 | 2.66% | 3 | 15.79% | 
| jens axboe | jens axboe | 34 | 1.53% | 2 | 10.53% | 
| sebastien dugue | sebastien dugue | 9 | 0.41% | 1 | 5.26% | 
| dotan barak | dotan barak | 7 | 0.32% | 1 | 5.26% | 
| jiri kosina | jiri kosina | 5 | 0.23% | 1 | 5.26% | 
| al viro | al viro | 3 | 0.14% | 1 | 5.26% | 
| tejun heo | tejun heo | 3 | 0.14% | 1 | 5.26% | 
| stephen hemminger | stephen hemminger | 1 | 0.05% | 1 | 5.26% | 
 | Total | 2221 | 100.00% | 19 | 100.00% | 
  
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.