133ec1ccbSHans Petter Selasky /*
233ec1ccbSHans Petter Selasky * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
333ec1ccbSHans Petter Selasky * Copyright (c) 2005 Cisco Systems. All rights reserved.
433ec1ccbSHans Petter Selasky * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
533ec1ccbSHans Petter Selasky *
633ec1ccbSHans Petter Selasky * This software is available to you under a choice of one of two
733ec1ccbSHans Petter Selasky * licenses. You may choose to be licensed under the terms of the GNU
833ec1ccbSHans Petter Selasky * General Public License (GPL) Version 2, available from the file
933ec1ccbSHans Petter Selasky * COPYING in the main directory of this source tree, or the
1033ec1ccbSHans Petter Selasky * OpenIB.org BSD license below:
1133ec1ccbSHans Petter Selasky *
1233ec1ccbSHans Petter Selasky * Redistribution and use in source and binary forms, with or
1333ec1ccbSHans Petter Selasky * without modification, are permitted provided that the following
1433ec1ccbSHans Petter Selasky * conditions are met:
1533ec1ccbSHans Petter Selasky *
1633ec1ccbSHans Petter Selasky * - Redistributions of source code must retain the above
1733ec1ccbSHans Petter Selasky * copyright notice, this list of conditions and the following
1833ec1ccbSHans Petter Selasky * disclaimer.
1933ec1ccbSHans Petter Selasky *
2033ec1ccbSHans Petter Selasky * - Redistributions in binary form must reproduce the above
2133ec1ccbSHans Petter Selasky * copyright notice, this list of conditions and the following
2233ec1ccbSHans Petter Selasky * disclaimer in the documentation and/or other materials
2333ec1ccbSHans Petter Selasky * provided with the distribution.
2433ec1ccbSHans Petter Selasky *
2533ec1ccbSHans Petter Selasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2633ec1ccbSHans Petter Selasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2733ec1ccbSHans Petter Selasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2833ec1ccbSHans Petter Selasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2933ec1ccbSHans Petter Selasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3033ec1ccbSHans Petter Selasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3133ec1ccbSHans Petter Selasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3233ec1ccbSHans Petter Selasky * SOFTWARE.
3333ec1ccbSHans Petter Selasky */
3433ec1ccbSHans Petter Selasky
3533ec1ccbSHans Petter Selasky #ifndef MTHCA_MEMFREE_H
3633ec1ccbSHans Petter Selasky #define MTHCA_MEMFREE_H
3733ec1ccbSHans Petter Selasky
3833ec1ccbSHans Petter Selasky #include <linux/list.h>
3933ec1ccbSHans Petter Selasky #include <linux/mutex.h>
4033ec1ccbSHans Petter Selasky
4133ec1ccbSHans Petter Selasky #define MTHCA_ICM_CHUNK_LEN \
4233ec1ccbSHans Petter Selasky ((256 - sizeof (struct list_head) - 2 * sizeof (int)) / \
4333ec1ccbSHans Petter Selasky (sizeof (struct scatterlist)))
4433ec1ccbSHans Petter Selasky
45*29ed5385SMark Johnston #define MTHCA_ICM_PAGE_SHIFT 12
46*29ed5385SMark Johnston #define MTHCA_ICM_PAGE_SIZE (1 << MTHCA_ICM_PAGE_SHIFT)
47*29ed5385SMark Johnston #define MTHCA_DB_REC_PER_PAGE (MTHCA_ICM_PAGE_SIZE / 8)
4833ec1ccbSHans Petter Selasky
4933ec1ccbSHans Petter Selasky struct mthca_icm_chunk {
5033ec1ccbSHans Petter Selasky struct list_head list;
5133ec1ccbSHans Petter Selasky int npages;
5233ec1ccbSHans Petter Selasky int nsg;
5333ec1ccbSHans Petter Selasky struct scatterlist mem[MTHCA_ICM_CHUNK_LEN];
5433ec1ccbSHans Petter Selasky };
5533ec1ccbSHans Petter Selasky
5633ec1ccbSHans Petter Selasky struct mthca_icm {
5733ec1ccbSHans Petter Selasky struct list_head chunk_list;
5833ec1ccbSHans Petter Selasky int refcount;
5933ec1ccbSHans Petter Selasky };
6033ec1ccbSHans Petter Selasky
6133ec1ccbSHans Petter Selasky struct mthca_icm_table {
6233ec1ccbSHans Petter Selasky u64 virt;
6333ec1ccbSHans Petter Selasky int num_icm;
6433ec1ccbSHans Petter Selasky int num_obj;
6533ec1ccbSHans Petter Selasky int obj_size;
6633ec1ccbSHans Petter Selasky int lowmem;
6733ec1ccbSHans Petter Selasky int coherent;
6833ec1ccbSHans Petter Selasky struct mutex mutex;
6933ec1ccbSHans Petter Selasky struct mthca_icm *icm[0];
7033ec1ccbSHans Petter Selasky };
7133ec1ccbSHans Petter Selasky
7233ec1ccbSHans Petter Selasky struct mthca_icm_iter {
7333ec1ccbSHans Petter Selasky struct mthca_icm *icm;
7433ec1ccbSHans Petter Selasky struct mthca_icm_chunk *chunk;
7533ec1ccbSHans Petter Selasky int page_idx;
7633ec1ccbSHans Petter Selasky };
7733ec1ccbSHans Petter Selasky
7833ec1ccbSHans Petter Selasky struct mthca_dev;
7933ec1ccbSHans Petter Selasky
8033ec1ccbSHans Petter Selasky struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
8133ec1ccbSHans Petter Selasky gfp_t gfp_mask, int coherent);
8233ec1ccbSHans Petter Selasky void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm, int coherent);
8333ec1ccbSHans Petter Selasky
8433ec1ccbSHans Petter Selasky struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
8533ec1ccbSHans Petter Selasky u64 virt, int obj_size,
8633ec1ccbSHans Petter Selasky int nobj, int reserved,
8733ec1ccbSHans Petter Selasky int use_lowmem, int use_coherent);
8833ec1ccbSHans Petter Selasky void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table);
8933ec1ccbSHans Petter Selasky int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);
9033ec1ccbSHans Petter Selasky void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);
9133ec1ccbSHans Petter Selasky void *mthca_table_find(struct mthca_icm_table *table, int obj, dma_addr_t *dma_handle);
9233ec1ccbSHans Petter Selasky int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,
9333ec1ccbSHans Petter Selasky int start, int end);
9433ec1ccbSHans Petter Selasky void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,
9533ec1ccbSHans Petter Selasky int start, int end);
9633ec1ccbSHans Petter Selasky
mthca_icm_first(struct mthca_icm * icm,struct mthca_icm_iter * iter)9733ec1ccbSHans Petter Selasky static inline void mthca_icm_first(struct mthca_icm *icm,
9833ec1ccbSHans Petter Selasky struct mthca_icm_iter *iter)
9933ec1ccbSHans Petter Selasky {
10033ec1ccbSHans Petter Selasky iter->icm = icm;
10133ec1ccbSHans Petter Selasky iter->chunk = list_empty(&icm->chunk_list) ?
10233ec1ccbSHans Petter Selasky NULL : list_entry(icm->chunk_list.next,
10333ec1ccbSHans Petter Selasky struct mthca_icm_chunk, list);
10433ec1ccbSHans Petter Selasky iter->page_idx = 0;
10533ec1ccbSHans Petter Selasky }
10633ec1ccbSHans Petter Selasky
mthca_icm_last(struct mthca_icm_iter * iter)10733ec1ccbSHans Petter Selasky static inline int mthca_icm_last(struct mthca_icm_iter *iter)
10833ec1ccbSHans Petter Selasky {
10933ec1ccbSHans Petter Selasky return !iter->chunk;
11033ec1ccbSHans Petter Selasky }
11133ec1ccbSHans Petter Selasky
mthca_icm_next(struct mthca_icm_iter * iter)11233ec1ccbSHans Petter Selasky static inline void mthca_icm_next(struct mthca_icm_iter *iter)
11333ec1ccbSHans Petter Selasky {
11433ec1ccbSHans Petter Selasky if (++iter->page_idx >= iter->chunk->nsg) {
11533ec1ccbSHans Petter Selasky if (iter->chunk->list.next == &iter->icm->chunk_list) {
11633ec1ccbSHans Petter Selasky iter->chunk = NULL;
11733ec1ccbSHans Petter Selasky return;
11833ec1ccbSHans Petter Selasky }
11933ec1ccbSHans Petter Selasky
12033ec1ccbSHans Petter Selasky iter->chunk = list_entry(iter->chunk->list.next,
12133ec1ccbSHans Petter Selasky struct mthca_icm_chunk, list);
12233ec1ccbSHans Petter Selasky iter->page_idx = 0;
12333ec1ccbSHans Petter Selasky }
12433ec1ccbSHans Petter Selasky }
12533ec1ccbSHans Petter Selasky
mthca_icm_addr(struct mthca_icm_iter * iter)12633ec1ccbSHans Petter Selasky static inline dma_addr_t mthca_icm_addr(struct mthca_icm_iter *iter)
12733ec1ccbSHans Petter Selasky {
12833ec1ccbSHans Petter Selasky return sg_dma_address(&iter->chunk->mem[iter->page_idx]);
12933ec1ccbSHans Petter Selasky }
13033ec1ccbSHans Petter Selasky
mthca_icm_size(struct mthca_icm_iter * iter)13133ec1ccbSHans Petter Selasky static inline unsigned long mthca_icm_size(struct mthca_icm_iter *iter)
13233ec1ccbSHans Petter Selasky {
13333ec1ccbSHans Petter Selasky return sg_dma_len(&iter->chunk->mem[iter->page_idx]);
13433ec1ccbSHans Petter Selasky }
13533ec1ccbSHans Petter Selasky
13633ec1ccbSHans Petter Selasky struct mthca_db_page {
13733ec1ccbSHans Petter Selasky DECLARE_BITMAP(used, MTHCA_DB_REC_PER_PAGE);
13833ec1ccbSHans Petter Selasky __be64 *db_rec;
13933ec1ccbSHans Petter Selasky dma_addr_t mapping;
14033ec1ccbSHans Petter Selasky };
14133ec1ccbSHans Petter Selasky
14233ec1ccbSHans Petter Selasky struct mthca_db_table {
14333ec1ccbSHans Petter Selasky int npages;
14433ec1ccbSHans Petter Selasky int max_group1;
14533ec1ccbSHans Petter Selasky int min_group2;
14633ec1ccbSHans Petter Selasky struct mthca_db_page *page;
14733ec1ccbSHans Petter Selasky struct mutex mutex;
14833ec1ccbSHans Petter Selasky };
14933ec1ccbSHans Petter Selasky
15033ec1ccbSHans Petter Selasky enum mthca_db_type {
15133ec1ccbSHans Petter Selasky MTHCA_DB_TYPE_INVALID = 0x0,
15233ec1ccbSHans Petter Selasky MTHCA_DB_TYPE_CQ_SET_CI = 0x1,
15333ec1ccbSHans Petter Selasky MTHCA_DB_TYPE_CQ_ARM = 0x2,
15433ec1ccbSHans Petter Selasky MTHCA_DB_TYPE_SQ = 0x3,
15533ec1ccbSHans Petter Selasky MTHCA_DB_TYPE_RQ = 0x4,
15633ec1ccbSHans Petter Selasky MTHCA_DB_TYPE_SRQ = 0x5,
15733ec1ccbSHans Petter Selasky MTHCA_DB_TYPE_GROUP_SEP = 0x7
15833ec1ccbSHans Petter Selasky };
15933ec1ccbSHans Petter Selasky
16033ec1ccbSHans Petter Selasky struct mthca_user_db_table;
16133ec1ccbSHans Petter Selasky struct mthca_uar;
16233ec1ccbSHans Petter Selasky
16333ec1ccbSHans Petter Selasky int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
16433ec1ccbSHans Petter Selasky struct mthca_user_db_table *db_tab, int index, u64 uaddr);
16533ec1ccbSHans Petter Selasky void mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
16633ec1ccbSHans Petter Selasky struct mthca_user_db_table *db_tab, int index);
16733ec1ccbSHans Petter Selasky struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev);
16833ec1ccbSHans Petter Selasky void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,
16933ec1ccbSHans Petter Selasky struct mthca_user_db_table *db_tab);
17033ec1ccbSHans Petter Selasky
17133ec1ccbSHans Petter Selasky int mthca_init_db_tab(struct mthca_dev *dev);
17233ec1ccbSHans Petter Selasky void mthca_cleanup_db_tab(struct mthca_dev *dev);
17333ec1ccbSHans Petter Selasky int mthca_alloc_db(struct mthca_dev *dev, enum mthca_db_type type,
17433ec1ccbSHans Petter Selasky u32 qn, __be32 **db);
17533ec1ccbSHans Petter Selasky void mthca_free_db(struct mthca_dev *dev, int type, int db_index);
17633ec1ccbSHans Petter Selasky
17733ec1ccbSHans Petter Selasky #endif /* MTHCA_MEMFREE_H */
178