162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
362306a36Sopenharmony_ci * Copyright (c) 2005 Cisco Systems.  All rights reserved.
462306a36Sopenharmony_ci * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This software is available to you under a choice of one of two
762306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
862306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
962306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
1062306a36Sopenharmony_ci * OpenIB.org BSD license below:
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1362306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1462306a36Sopenharmony_ci *     conditions are met:
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1762306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1862306a36Sopenharmony_ci *        disclaimer.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2162306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2262306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2362306a36Sopenharmony_ci *        provided with the distribution.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2662306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2762306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2862306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2962306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3062306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3162306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3262306a36Sopenharmony_ci * SOFTWARE.
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#ifndef MTHCA_MEMFREE_H
3662306a36Sopenharmony_ci#define MTHCA_MEMFREE_H
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#include <linux/list.h>
3962306a36Sopenharmony_ci#include <linux/mutex.h>
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#define MTHCA_ICM_CHUNK_LEN \
4262306a36Sopenharmony_ci	((256 - sizeof (struct list_head) - 2 * sizeof (int)) /		\
4362306a36Sopenharmony_ci	 (sizeof (struct scatterlist)))
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cienum {
4662306a36Sopenharmony_ci	MTHCA_ICM_PAGE_SHIFT	= 12,
4762306a36Sopenharmony_ci	MTHCA_ICM_PAGE_SIZE	= 1 << MTHCA_ICM_PAGE_SHIFT,
4862306a36Sopenharmony_ci	MTHCA_DB_REC_PER_PAGE	= MTHCA_ICM_PAGE_SIZE / 8
4962306a36Sopenharmony_ci};
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cistruct mthca_icm_chunk {
5262306a36Sopenharmony_ci	struct list_head   list;
5362306a36Sopenharmony_ci	int                npages;
5462306a36Sopenharmony_ci	int                nsg;
5562306a36Sopenharmony_ci	struct scatterlist mem[MTHCA_ICM_CHUNK_LEN];
5662306a36Sopenharmony_ci};
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_cistruct mthca_icm {
5962306a36Sopenharmony_ci	struct list_head chunk_list;
6062306a36Sopenharmony_ci	int              refcount;
6162306a36Sopenharmony_ci};
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_cistruct mthca_icm_table {
6462306a36Sopenharmony_ci	u64               virt;
6562306a36Sopenharmony_ci	int               num_icm;
6662306a36Sopenharmony_ci	int               num_obj;
6762306a36Sopenharmony_ci	int               obj_size;
6862306a36Sopenharmony_ci	int               lowmem;
6962306a36Sopenharmony_ci	int               coherent;
7062306a36Sopenharmony_ci	struct mutex      mutex;
7162306a36Sopenharmony_ci	struct mthca_icm *icm[];
7262306a36Sopenharmony_ci};
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cistruct mthca_icm_iter {
7562306a36Sopenharmony_ci	struct mthca_icm       *icm;
7662306a36Sopenharmony_ci	struct mthca_icm_chunk *chunk;
7762306a36Sopenharmony_ci	int                     page_idx;
7862306a36Sopenharmony_ci};
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistruct mthca_dev;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistruct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
8362306a36Sopenharmony_ci				  gfp_t gfp_mask, int coherent);
8462306a36Sopenharmony_civoid mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm, int coherent);
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_cistruct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
8762306a36Sopenharmony_ci					      u64 virt, int obj_size,
8862306a36Sopenharmony_ci					      int nobj, int reserved,
8962306a36Sopenharmony_ci					      int use_lowmem, int use_coherent);
9062306a36Sopenharmony_civoid mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table);
9162306a36Sopenharmony_ciint mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);
9262306a36Sopenharmony_civoid mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);
9362306a36Sopenharmony_civoid *mthca_table_find(struct mthca_icm_table *table, int obj, dma_addr_t *dma_handle);
9462306a36Sopenharmony_ciint mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,
9562306a36Sopenharmony_ci			  int start, int end);
9662306a36Sopenharmony_civoid mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,
9762306a36Sopenharmony_ci			   int start, int end);
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_cistatic inline void mthca_icm_first(struct mthca_icm *icm,
10062306a36Sopenharmony_ci				   struct mthca_icm_iter *iter)
10162306a36Sopenharmony_ci{
10262306a36Sopenharmony_ci	iter->icm      = icm;
10362306a36Sopenharmony_ci	iter->chunk    = list_empty(&icm->chunk_list) ?
10462306a36Sopenharmony_ci		NULL : list_entry(icm->chunk_list.next,
10562306a36Sopenharmony_ci				  struct mthca_icm_chunk, list);
10662306a36Sopenharmony_ci	iter->page_idx = 0;
10762306a36Sopenharmony_ci}
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_cistatic inline int mthca_icm_last(struct mthca_icm_iter *iter)
11062306a36Sopenharmony_ci{
11162306a36Sopenharmony_ci	return !iter->chunk;
11262306a36Sopenharmony_ci}
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cistatic inline void mthca_icm_next(struct mthca_icm_iter *iter)
11562306a36Sopenharmony_ci{
11662306a36Sopenharmony_ci	if (++iter->page_idx >= iter->chunk->nsg) {
11762306a36Sopenharmony_ci		if (iter->chunk->list.next == &iter->icm->chunk_list) {
11862306a36Sopenharmony_ci			iter->chunk = NULL;
11962306a36Sopenharmony_ci			return;
12062306a36Sopenharmony_ci		}
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci		iter->chunk = list_entry(iter->chunk->list.next,
12362306a36Sopenharmony_ci					 struct mthca_icm_chunk, list);
12462306a36Sopenharmony_ci		iter->page_idx = 0;
12562306a36Sopenharmony_ci	}
12662306a36Sopenharmony_ci}
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_cistatic inline dma_addr_t mthca_icm_addr(struct mthca_icm_iter *iter)
12962306a36Sopenharmony_ci{
13062306a36Sopenharmony_ci	return sg_dma_address(&iter->chunk->mem[iter->page_idx]);
13162306a36Sopenharmony_ci}
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistatic inline unsigned long mthca_icm_size(struct mthca_icm_iter *iter)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	return sg_dma_len(&iter->chunk->mem[iter->page_idx]);
13662306a36Sopenharmony_ci}
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_cistruct mthca_db_page {
13962306a36Sopenharmony_ci	DECLARE_BITMAP(used, MTHCA_DB_REC_PER_PAGE);
14062306a36Sopenharmony_ci	__be64    *db_rec;
14162306a36Sopenharmony_ci	dma_addr_t mapping;
14262306a36Sopenharmony_ci};
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_cistruct mthca_db_table {
14562306a36Sopenharmony_ci	int 	       	      npages;
14662306a36Sopenharmony_ci	int 	       	      max_group1;
14762306a36Sopenharmony_ci	int 	       	      min_group2;
14862306a36Sopenharmony_ci	struct mthca_db_page *page;
14962306a36Sopenharmony_ci	struct mutex          mutex;
15062306a36Sopenharmony_ci};
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_cienum mthca_db_type {
15362306a36Sopenharmony_ci	MTHCA_DB_TYPE_INVALID   = 0x0,
15462306a36Sopenharmony_ci	MTHCA_DB_TYPE_CQ_SET_CI = 0x1,
15562306a36Sopenharmony_ci	MTHCA_DB_TYPE_CQ_ARM    = 0x2,
15662306a36Sopenharmony_ci	MTHCA_DB_TYPE_SQ        = 0x3,
15762306a36Sopenharmony_ci	MTHCA_DB_TYPE_RQ        = 0x4,
15862306a36Sopenharmony_ci	MTHCA_DB_TYPE_SRQ       = 0x5,
15962306a36Sopenharmony_ci	MTHCA_DB_TYPE_GROUP_SEP = 0x7
16062306a36Sopenharmony_ci};
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cistruct mthca_user_db_table;
16362306a36Sopenharmony_cistruct mthca_uar;
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciint mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
16662306a36Sopenharmony_ci		      struct mthca_user_db_table *db_tab, int index, u64 uaddr);
16762306a36Sopenharmony_civoid mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
16862306a36Sopenharmony_ci			 struct mthca_user_db_table *db_tab, int index);
16962306a36Sopenharmony_cistruct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev);
17062306a36Sopenharmony_civoid mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,
17162306a36Sopenharmony_ci			       struct mthca_user_db_table *db_tab);
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ciint mthca_init_db_tab(struct mthca_dev *dev);
17462306a36Sopenharmony_civoid mthca_cleanup_db_tab(struct mthca_dev *dev);
17562306a36Sopenharmony_ciint mthca_alloc_db(struct mthca_dev *dev, enum mthca_db_type type,
17662306a36Sopenharmony_ci		   u32 qn, __be32 **db);
17762306a36Sopenharmony_civoid mthca_free_db(struct mthca_dev *dev, int type, int db_index);
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci#endif /* MTHCA_MEMFREE_H */
180