1 /* 2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved. 3 * Copyright (c) 2005 Cisco Systems. All rights reserved. 4 * Copyright (c) 2005 Mellanox Technologies. All rights reserved. 5 * 6 * This software is available to you under a choice of one of two 7 * licenses. You may choose to be licensed under the terms of the GNU 8 * General Public License (GPL) Version 2, available from the file 9 * COPYING in the main directory of this source tree, or the 10 * OpenIB.org BSD license below: 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above 17 * copyright notice, this list of conditions and the following 18 * disclaimer. 19 * 20 * - Redistributions in binary form must reproduce the above 21 * copyright notice, this list of conditions and the following 22 * disclaimer in the documentation and/or other materials 23 * provided with the distribution. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 * SOFTWARE. 33 * 34 * $Id$ 35 */ 36 37 #ifndef MTHCA_MEMFREE_H 38 #define MTHCA_MEMFREE_H 39 40 #include <linux/list.h> 41 #include <linux/pci.h> 42 #include <linux/mutex.h> 43 44 #define MTHCA_ICM_CHUNK_LEN \ 45 ((256 - sizeof (struct list_head) - 2 * sizeof (int)) / \ 46 (sizeof (struct scatterlist))) 47 48 enum { 49 MTHCA_ICM_PAGE_SHIFT = 12, 50 MTHCA_ICM_PAGE_SIZE = 1 << MTHCA_ICM_PAGE_SHIFT, 51 MTHCA_DB_REC_PER_PAGE = MTHCA_ICM_PAGE_SIZE / 8 52 }; 53 54 struct mthca_icm_chunk { 55 struct list_head list; 56 int npages; 57 int nsg; 58 struct scatterlist mem[MTHCA_ICM_CHUNK_LEN]; 59 }; 60 61 struct mthca_icm { 62 struct list_head chunk_list; 63 int refcount; 64 }; 65 66 struct mthca_icm_table { 67 u64 virt; 68 int num_icm; 69 int num_obj; 70 int obj_size; 71 int lowmem; 72 int coherent; 73 struct mutex mutex; 74 struct mthca_icm *icm[0]; 75 }; 76 77 struct mthca_icm_iter { 78 struct mthca_icm *icm; 79 struct mthca_icm_chunk *chunk; 80 int page_idx; 81 }; 82 83 struct mthca_dev; 84 85 struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages, 86 gfp_t gfp_mask, int coherent); 87 void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm, int coherent); 88 89 struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev, 90 u64 virt, int obj_size, 91 int nobj, int reserved, 92 int use_lowmem, int use_coherent); 93 void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table); 94 int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj); 95 void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj); 96 void *mthca_table_find(struct mthca_icm_table *table, int obj, dma_addr_t *dma_handle); 97 int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table, 98 int start, int end); 99 void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table, 100 int start, int end); 101 102 static inline void mthca_icm_first(struct mthca_icm *icm, 103 struct mthca_icm_iter *iter) 104 { 105 iter->icm = icm; 106 iter->chunk = list_empty(&icm->chunk_list) ? 107 NULL : list_entry(icm->chunk_list.next, 108 struct mthca_icm_chunk, list); 109 iter->page_idx = 0; 110 } 111 112 static inline int mthca_icm_last(struct mthca_icm_iter *iter) 113 { 114 return !iter->chunk; 115 } 116 117 static inline void mthca_icm_next(struct mthca_icm_iter *iter) 118 { 119 if (++iter->page_idx >= iter->chunk->nsg) { 120 if (iter->chunk->list.next == &iter->icm->chunk_list) { 121 iter->chunk = NULL; 122 return; 123 } 124 125 iter->chunk = list_entry(iter->chunk->list.next, 126 struct mthca_icm_chunk, list); 127 iter->page_idx = 0; 128 } 129 } 130 131 static inline dma_addr_t mthca_icm_addr(struct mthca_icm_iter *iter) 132 { 133 return sg_dma_address(&iter->chunk->mem[iter->page_idx]); 134 } 135 136 static inline unsigned long mthca_icm_size(struct mthca_icm_iter *iter) 137 { 138 return sg_dma_len(&iter->chunk->mem[iter->page_idx]); 139 } 140 141 struct mthca_db_page { 142 DECLARE_BITMAP(used, MTHCA_DB_REC_PER_PAGE); 143 __be64 *db_rec; 144 dma_addr_t mapping; 145 }; 146 147 struct mthca_db_table { 148 int npages; 149 int max_group1; 150 int min_group2; 151 struct mthca_db_page *page; 152 struct mutex mutex; 153 }; 154 155 enum mthca_db_type { 156 MTHCA_DB_TYPE_INVALID = 0x0, 157 MTHCA_DB_TYPE_CQ_SET_CI = 0x1, 158 MTHCA_DB_TYPE_CQ_ARM = 0x2, 159 MTHCA_DB_TYPE_SQ = 0x3, 160 MTHCA_DB_TYPE_RQ = 0x4, 161 MTHCA_DB_TYPE_SRQ = 0x5, 162 MTHCA_DB_TYPE_GROUP_SEP = 0x7 163 }; 164 165 struct mthca_user_db_table; 166 struct mthca_uar; 167 168 int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar, 169 struct mthca_user_db_table *db_tab, int index, u64 uaddr); 170 void mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar, 171 struct mthca_user_db_table *db_tab, int index); 172 struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev); 173 void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar, 174 struct mthca_user_db_table *db_tab); 175 176 int mthca_init_db_tab(struct mthca_dev *dev); 177 void mthca_cleanup_db_tab(struct mthca_dev *dev); 178 int mthca_alloc_db(struct mthca_dev *dev, enum mthca_db_type type, 179 u32 qn, __be32 **db); 180 void mthca_free_db(struct mthca_dev *dev, int type, int db_index); 181 182 #endif /* MTHCA_MEMFREE_H */ 183