2 * Copyright (c) 2005, 2006, 2007, 2008, 2014 Mellanox Technologies. All rights reserved.
3 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 #include <linux/list.h>
38 #include <linux/pci.h>
39 #include <linux/mutex.h>
40 #include <linux/scatterlist.h>
42 #define MLX4_ICM_CHUNK_LEN \
43 ((256 - sizeof (struct list_head) - 2 * sizeof (int)) / \
44 (sizeof (struct scatterlist)))
47 MLX4_ICM_PAGE_SHIFT = 12,
48 MLX4_ICM_PAGE_SIZE = 1 << MLX4_ICM_PAGE_SHIFT,
51 struct mlx4_icm_chunk {
52 struct list_head list;
55 struct scatterlist mem[MLX4_ICM_CHUNK_LEN];
59 struct list_head chunk_list;
63 struct mlx4_icm_iter {
65 struct mlx4_icm_chunk *chunk;
71 struct mlx4_icm *mlx4_alloc_icm(struct mlx4_dev *dev, int npages,
72 gfp_t gfp_mask, int coherent);
73 void mlx4_free_icm(struct mlx4_dev *dev, struct mlx4_icm *icm, int coherent);
75 int mlx4_table_get(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj,
77 void mlx4_table_put(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj);
78 int mlx4_table_get_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
80 void mlx4_table_put_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
82 int mlx4_init_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table,
83 u64 virt, int obj_size, u32 nobj, int reserved,
84 int use_lowmem, int use_coherent);
85 void mlx4_cleanup_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table);
86 void *mlx4_table_find(struct mlx4_icm_table *table, u32 obj, dma_addr_t *dma_handle);
88 static inline void mlx4_icm_first(struct mlx4_icm *icm,
89 struct mlx4_icm_iter *iter)
92 iter->chunk = list_empty(&icm->chunk_list) ?
93 NULL : list_entry(icm->chunk_list.next,
94 struct mlx4_icm_chunk, list);
98 static inline int mlx4_icm_last(struct mlx4_icm_iter *iter)
103 static inline void mlx4_icm_next(struct mlx4_icm_iter *iter)
105 if (++iter->page_idx >= iter->chunk->nsg) {
106 if (iter->chunk->list.next == &iter->icm->chunk_list) {
111 iter->chunk = list_entry(iter->chunk->list.next,
112 struct mlx4_icm_chunk, list);
117 static inline dma_addr_t mlx4_icm_addr(struct mlx4_icm_iter *iter)
119 return sg_dma_address(&iter->chunk->mem[iter->page_idx]);
122 static inline unsigned long mlx4_icm_size(struct mlx4_icm_iter *iter)
124 return sg_dma_len(&iter->chunk->mem[iter->page_idx]);
127 #endif /* MLX4_ICM_H */