162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
362306a36Sopenharmony_ci * Copyright (c) 2006, 2007 Cisco Systems, Inc.  All rights reserved.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * This software is available to you under a choice of one of two
662306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
762306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
862306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
962306a36Sopenharmony_ci * OpenIB.org BSD license below:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1262306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1362306a36Sopenharmony_ci *     conditions are met:
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1662306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1762306a36Sopenharmony_ci *        disclaimer.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2062306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2162306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2262306a36Sopenharmony_ci *        provided with the distribution.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2562306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2662306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2762306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2862306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2962306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3062306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3162306a36Sopenharmony_ci * SOFTWARE.
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#ifndef MLX4_ICM_H
3562306a36Sopenharmony_ci#define MLX4_ICM_H
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#include <linux/list.h>
3862306a36Sopenharmony_ci#include <linux/pci.h>
3962306a36Sopenharmony_ci#include <linux/mutex.h>
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#define MLX4_ICM_CHUNK_LEN						\
4262306a36Sopenharmony_ci	((256 - sizeof(struct list_head) - 2 * sizeof(int)) /		\
4362306a36Sopenharmony_ci	 (sizeof(struct scatterlist)))
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cienum {
4662306a36Sopenharmony_ci	MLX4_ICM_PAGE_SHIFT	= 12,
4762306a36Sopenharmony_ci	MLX4_ICM_PAGE_SIZE	= 1 << MLX4_ICM_PAGE_SHIFT,
4862306a36Sopenharmony_ci};
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistruct mlx4_icm_buf {
5162306a36Sopenharmony_ci	void			*addr;
5262306a36Sopenharmony_ci	size_t			size;
5362306a36Sopenharmony_ci	dma_addr_t		dma_addr;
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistruct mlx4_icm_chunk {
5762306a36Sopenharmony_ci	struct list_head	list;
5862306a36Sopenharmony_ci	int			npages;
5962306a36Sopenharmony_ci	int			nsg;
6062306a36Sopenharmony_ci	bool			coherent;
6162306a36Sopenharmony_ci	union {
6262306a36Sopenharmony_ci		struct scatterlist	sg[MLX4_ICM_CHUNK_LEN];
6362306a36Sopenharmony_ci		struct mlx4_icm_buf	buf[MLX4_ICM_CHUNK_LEN];
6462306a36Sopenharmony_ci	};
6562306a36Sopenharmony_ci};
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cistruct mlx4_icm {
6862306a36Sopenharmony_ci	struct list_head	chunk_list;
6962306a36Sopenharmony_ci	int			refcount;
7062306a36Sopenharmony_ci};
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_cistruct mlx4_icm_iter {
7362306a36Sopenharmony_ci	struct mlx4_icm	       *icm;
7462306a36Sopenharmony_ci	struct mlx4_icm_chunk  *chunk;
7562306a36Sopenharmony_ci	int			page_idx;
7662306a36Sopenharmony_ci};
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_cistruct mlx4_dev;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistruct mlx4_icm *mlx4_alloc_icm(struct mlx4_dev *dev, int npages,
8162306a36Sopenharmony_ci				gfp_t gfp_mask, int coherent);
8262306a36Sopenharmony_civoid mlx4_free_icm(struct mlx4_dev *dev, struct mlx4_icm *icm, int coherent);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciint mlx4_table_get(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj);
8562306a36Sopenharmony_civoid mlx4_table_put(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj);
8662306a36Sopenharmony_ciint mlx4_table_get_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
8762306a36Sopenharmony_ci			 u32 start, u32 end);
8862306a36Sopenharmony_civoid mlx4_table_put_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
8962306a36Sopenharmony_ci			  u32 start, u32 end);
9062306a36Sopenharmony_ciint mlx4_init_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table,
9162306a36Sopenharmony_ci			u64 virt, int obj_size,	u32 nobj, int reserved,
9262306a36Sopenharmony_ci			int use_lowmem, int use_coherent);
9362306a36Sopenharmony_civoid mlx4_cleanup_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table);
9462306a36Sopenharmony_civoid *mlx4_table_find(struct mlx4_icm_table *table, u32 obj, dma_addr_t *dma_handle);
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_cistatic inline void mlx4_icm_first(struct mlx4_icm *icm,
9762306a36Sopenharmony_ci				  struct mlx4_icm_iter *iter)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci	iter->icm      = icm;
10062306a36Sopenharmony_ci	iter->chunk    = list_empty(&icm->chunk_list) ?
10162306a36Sopenharmony_ci		NULL : list_entry(icm->chunk_list.next,
10262306a36Sopenharmony_ci				  struct mlx4_icm_chunk, list);
10362306a36Sopenharmony_ci	iter->page_idx = 0;
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistatic inline int mlx4_icm_last(struct mlx4_icm_iter *iter)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	return !iter->chunk;
10962306a36Sopenharmony_ci}
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_cistatic inline void mlx4_icm_next(struct mlx4_icm_iter *iter)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	if (++iter->page_idx >= iter->chunk->nsg) {
11462306a36Sopenharmony_ci		if (iter->chunk->list.next == &iter->icm->chunk_list) {
11562306a36Sopenharmony_ci			iter->chunk = NULL;
11662306a36Sopenharmony_ci			return;
11762306a36Sopenharmony_ci		}
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci		iter->chunk = list_entry(iter->chunk->list.next,
12062306a36Sopenharmony_ci					 struct mlx4_icm_chunk, list);
12162306a36Sopenharmony_ci		iter->page_idx = 0;
12262306a36Sopenharmony_ci	}
12362306a36Sopenharmony_ci}
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_cistatic inline dma_addr_t mlx4_icm_addr(struct mlx4_icm_iter *iter)
12662306a36Sopenharmony_ci{
12762306a36Sopenharmony_ci	if (iter->chunk->coherent)
12862306a36Sopenharmony_ci		return iter->chunk->buf[iter->page_idx].dma_addr;
12962306a36Sopenharmony_ci	else
13062306a36Sopenharmony_ci		return sg_dma_address(&iter->chunk->sg[iter->page_idx]);
13162306a36Sopenharmony_ci}
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistatic inline unsigned long mlx4_icm_size(struct mlx4_icm_iter *iter)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	if (iter->chunk->coherent)
13662306a36Sopenharmony_ci		return iter->chunk->buf[iter->page_idx].size;
13762306a36Sopenharmony_ci	else
13862306a36Sopenharmony_ci		return sg_dma_len(&iter->chunk->sg[iter->page_idx]);
13962306a36Sopenharmony_ci}
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ciint mlx4_MAP_ICM_AUX(struct mlx4_dev *dev, struct mlx4_icm *icm);
14262306a36Sopenharmony_ciint mlx4_UNMAP_ICM_AUX(struct mlx4_dev *dev);
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci#endif /* MLX4_ICM_H */
145