Path: blob/master/drivers/infiniband/hw/mthca/mthca_memfree.h
15112 views
/*1* Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.2* Copyright (c) 2005 Cisco Systems. All rights reserved.3* Copyright (c) 2005 Mellanox Technologies. All rights reserved.4*5* This software is available to you under a choice of one of two6* licenses. You may choose to be licensed under the terms of the GNU7* General Public License (GPL) Version 2, available from the file8* COPYING in the main directory of this source tree, or the9* OpenIB.org BSD license below:10*11* Redistribution and use in source and binary forms, with or12* without modification, are permitted provided that the following13* conditions are met:14*15* - Redistributions of source code must retain the above16* copyright notice, this list of conditions and the following17* disclaimer.18*19* - Redistributions in binary form must reproduce the above20* copyright notice, this list of conditions and the following21* disclaimer in the documentation and/or other materials22* provided with the distribution.23*24* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,25* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF26* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND27* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS28* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN29* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN30* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE31* SOFTWARE.32*/3334#ifndef MTHCA_MEMFREE_H35#define MTHCA_MEMFREE_H3637#include <linux/list.h>38#include <linux/mutex.h>3940#define MTHCA_ICM_CHUNK_LEN \41((256 - sizeof (struct list_head) - 2 * sizeof (int)) / \42(sizeof (struct scatterlist)))4344enum {45MTHCA_ICM_PAGE_SHIFT = 12,46MTHCA_ICM_PAGE_SIZE = 1 << MTHCA_ICM_PAGE_SHIFT,47MTHCA_DB_REC_PER_PAGE = MTHCA_ICM_PAGE_SIZE / 848};4950struct mthca_icm_chunk {51struct list_head list;52int npages;53int nsg;54struct scatterlist mem[MTHCA_ICM_CHUNK_LEN];55};5657struct mthca_icm {58struct list_head chunk_list;59int refcount;60};6162struct mthca_icm_table {63u64 virt;64int num_icm;65int num_obj;66int obj_size;67int lowmem;68int coherent;69struct mutex mutex;70struct mthca_icm *icm[0];71};7273struct mthca_icm_iter {74struct mthca_icm *icm;75struct mthca_icm_chunk *chunk;76int page_idx;77};7879struct mthca_dev;8081struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,82gfp_t gfp_mask, int coherent);83void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm, int coherent);8485struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,86u64 virt, int obj_size,87int nobj, int reserved,88int use_lowmem, int use_coherent);89void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table);90int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);91void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj);92void *mthca_table_find(struct mthca_icm_table *table, int obj, dma_addr_t *dma_handle);93int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,94int start, int end);95void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,96int start, int end);9798static inline void mthca_icm_first(struct mthca_icm *icm,99struct mthca_icm_iter *iter)100{101iter->icm = icm;102iter->chunk = list_empty(&icm->chunk_list) ?103NULL : list_entry(icm->chunk_list.next,104struct mthca_icm_chunk, list);105iter->page_idx = 0;106}107108static inline int mthca_icm_last(struct mthca_icm_iter *iter)109{110return !iter->chunk;111}112113static inline void mthca_icm_next(struct mthca_icm_iter *iter)114{115if (++iter->page_idx >= iter->chunk->nsg) {116if (iter->chunk->list.next == &iter->icm->chunk_list) {117iter->chunk = NULL;118return;119}120121iter->chunk = list_entry(iter->chunk->list.next,122struct mthca_icm_chunk, list);123iter->page_idx = 0;124}125}126127static inline dma_addr_t mthca_icm_addr(struct mthca_icm_iter *iter)128{129return sg_dma_address(&iter->chunk->mem[iter->page_idx]);130}131132static inline unsigned long mthca_icm_size(struct mthca_icm_iter *iter)133{134return sg_dma_len(&iter->chunk->mem[iter->page_idx]);135}136137struct mthca_db_page {138DECLARE_BITMAP(used, MTHCA_DB_REC_PER_PAGE);139__be64 *db_rec;140dma_addr_t mapping;141};142143struct mthca_db_table {144int npages;145int max_group1;146int min_group2;147struct mthca_db_page *page;148struct mutex mutex;149};150151enum mthca_db_type {152MTHCA_DB_TYPE_INVALID = 0x0,153MTHCA_DB_TYPE_CQ_SET_CI = 0x1,154MTHCA_DB_TYPE_CQ_ARM = 0x2,155MTHCA_DB_TYPE_SQ = 0x3,156MTHCA_DB_TYPE_RQ = 0x4,157MTHCA_DB_TYPE_SRQ = 0x5,158MTHCA_DB_TYPE_GROUP_SEP = 0x7159};160161struct mthca_user_db_table;162struct mthca_uar;163164int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar,165struct mthca_user_db_table *db_tab, int index, u64 uaddr);166void mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar,167struct mthca_user_db_table *db_tab, int index);168struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev);169void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,170struct mthca_user_db_table *db_tab);171172int mthca_init_db_tab(struct mthca_dev *dev);173void mthca_cleanup_db_tab(struct mthca_dev *dev);174int mthca_alloc_db(struct mthca_dev *dev, enum mthca_db_type type,175u32 qn, __be32 **db);176void mthca_free_db(struct mthca_dev *dev, int type, int db_index);177178#endif /* MTHCA_MEMFREE_H */179180181