icm.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
  3. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  4. *
  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:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  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.
  23. *
  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
  31. * SOFTWARE.
  32. */
  33. #ifndef MLX4_ICM_H
  34. #define MLX4_ICM_H
  35. #include <linux/list.h>
  36. #include <linux/pci.h>
  37. #include <linux/mutex.h>
  38. #define MLX4_ICM_CHUNK_LEN \
  39. ((256 - sizeof(struct list_head) - 2 * sizeof(int)) / \
  40. (sizeof(struct scatterlist)))
  41. enum {
  42. MLX4_ICM_PAGE_SHIFT = 12,
  43. MLX4_ICM_PAGE_SIZE = 1 << MLX4_ICM_PAGE_SHIFT,
  44. };
  45. struct mlx4_icm_buf {
  46. void *addr;
  47. size_t size;
  48. dma_addr_t dma_addr;
  49. };
  50. struct mlx4_icm_chunk {
  51. struct list_head list;
  52. int npages;
  53. int nsg;
  54. bool coherent;
  55. union {
  56. struct scatterlist sg[MLX4_ICM_CHUNK_LEN];
  57. struct mlx4_icm_buf buf[MLX4_ICM_CHUNK_LEN];
  58. };
  59. };
  60. struct mlx4_icm {
  61. struct list_head chunk_list;
  62. int refcount;
  63. };
  64. struct mlx4_icm_iter {
  65. struct mlx4_icm *icm;
  66. struct mlx4_icm_chunk *chunk;
  67. int page_idx;
  68. };
  69. struct mlx4_dev;
  70. struct mlx4_icm *mlx4_alloc_icm(struct mlx4_dev *dev, int npages,
  71. gfp_t gfp_mask, int coherent);
  72. void mlx4_free_icm(struct mlx4_dev *dev, struct mlx4_icm *icm, int coherent);
  73. int mlx4_table_get(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj);
  74. void mlx4_table_put(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj);
  75. int mlx4_table_get_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
  76. u32 start, u32 end);
  77. void mlx4_table_put_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
  78. u32 start, u32 end);
  79. int mlx4_init_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table,
  80. u64 virt, int obj_size, u32 nobj, int reserved,
  81. int use_lowmem, int use_coherent);
  82. void mlx4_cleanup_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table);
  83. void *mlx4_table_find(struct mlx4_icm_table *table, u32 obj, dma_addr_t *dma_handle);
  84. static inline void mlx4_icm_first(struct mlx4_icm *icm,
  85. struct mlx4_icm_iter *iter)
  86. {
  87. iter->icm = icm;
  88. iter->chunk = list_empty(&icm->chunk_list) ?
  89. NULL : list_entry(icm->chunk_list.next,
  90. struct mlx4_icm_chunk, list);
  91. iter->page_idx = 0;
  92. }
  93. static inline int mlx4_icm_last(struct mlx4_icm_iter *iter)
  94. {
  95. return !iter->chunk;
  96. }
  97. static inline void mlx4_icm_next(struct mlx4_icm_iter *iter)
  98. {
  99. if (++iter->page_idx >= iter->chunk->nsg) {
  100. if (iter->chunk->list.next == &iter->icm->chunk_list) {
  101. iter->chunk = NULL;
  102. return;
  103. }
  104. iter->chunk = list_entry(iter->chunk->list.next,
  105. struct mlx4_icm_chunk, list);
  106. iter->page_idx = 0;
  107. }
  108. }
  109. static inline dma_addr_t mlx4_icm_addr(struct mlx4_icm_iter *iter)
  110. {
  111. if (iter->chunk->coherent)
  112. return iter->chunk->buf[iter->page_idx].dma_addr;
  113. else
  114. return sg_dma_address(&iter->chunk->sg[iter->page_idx]);
  115. }
  116. static inline unsigned long mlx4_icm_size(struct mlx4_icm_iter *iter)
  117. {
  118. if (iter->chunk->coherent)
  119. return iter->chunk->buf[iter->page_idx].size;
  120. else
  121. return sg_dma_len(&iter->chunk->sg[iter->page_idx]);
  122. }
  123. int mlx4_MAP_ICM_AUX(struct mlx4_dev *dev, struct mlx4_icm *icm);
  124. int mlx4_UNMAP_ICM_AUX(struct mlx4_dev *dev);
  125. #endif /* MLX4_ICM_H */