rxe_dma.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2015 System Fabric Works, 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. #include "rxe.h"
  34. #include "rxe_loc.h"
  35. #define DMA_BAD_ADDER ((u64)0)
  36. static int rxe_mapping_error(struct ib_device *dev, u64 dma_addr)
  37. {
  38. return dma_addr == DMA_BAD_ADDER;
  39. }
  40. static u64 rxe_dma_map_single(struct ib_device *dev,
  41. void *cpu_addr, size_t size,
  42. enum dma_data_direction direction)
  43. {
  44. WARN_ON(!valid_dma_direction(direction));
  45. return (uintptr_t)cpu_addr;
  46. }
  47. static void rxe_dma_unmap_single(struct ib_device *dev,
  48. u64 addr, size_t size,
  49. enum dma_data_direction direction)
  50. {
  51. WARN_ON(!valid_dma_direction(direction));
  52. }
  53. static u64 rxe_dma_map_page(struct ib_device *dev,
  54. struct page *page,
  55. unsigned long offset,
  56. size_t size, enum dma_data_direction direction)
  57. {
  58. u64 addr;
  59. WARN_ON(!valid_dma_direction(direction));
  60. if (offset + size > PAGE_SIZE) {
  61. addr = DMA_BAD_ADDER;
  62. goto done;
  63. }
  64. addr = (uintptr_t)page_address(page);
  65. if (addr)
  66. addr += offset;
  67. done:
  68. return addr;
  69. }
  70. static void rxe_dma_unmap_page(struct ib_device *dev,
  71. u64 addr, size_t size,
  72. enum dma_data_direction direction)
  73. {
  74. WARN_ON(!valid_dma_direction(direction));
  75. }
  76. static int rxe_map_sg(struct ib_device *dev, struct scatterlist *sgl,
  77. int nents, enum dma_data_direction direction)
  78. {
  79. struct scatterlist *sg;
  80. u64 addr;
  81. int i;
  82. int ret = nents;
  83. WARN_ON(!valid_dma_direction(direction));
  84. for_each_sg(sgl, sg, nents, i) {
  85. addr = (uintptr_t)page_address(sg_page(sg));
  86. if (!addr) {
  87. ret = 0;
  88. break;
  89. }
  90. sg->dma_address = addr + sg->offset;
  91. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  92. sg->dma_length = sg->length;
  93. #endif
  94. }
  95. return ret;
  96. }
  97. static void rxe_unmap_sg(struct ib_device *dev,
  98. struct scatterlist *sg, int nents,
  99. enum dma_data_direction direction)
  100. {
  101. WARN_ON(!valid_dma_direction(direction));
  102. }
  103. static int rxe_map_sg_attrs(struct ib_device *dev, struct scatterlist *sgl,
  104. int nents, enum dma_data_direction direction,
  105. unsigned long attrs)
  106. {
  107. return rxe_map_sg(dev, sgl, nents, direction);
  108. }
  109. static void rxe_unmap_sg_attrs(struct ib_device *dev,
  110. struct scatterlist *sg, int nents,
  111. enum dma_data_direction direction,
  112. unsigned long attrs)
  113. {
  114. rxe_unmap_sg(dev, sg, nents, direction);
  115. }
  116. static void rxe_sync_single_for_cpu(struct ib_device *dev,
  117. u64 addr,
  118. size_t size, enum dma_data_direction dir)
  119. {
  120. }
  121. static void rxe_sync_single_for_device(struct ib_device *dev,
  122. u64 addr,
  123. size_t size, enum dma_data_direction dir)
  124. {
  125. }
  126. static void *rxe_dma_alloc_coherent(struct ib_device *dev, size_t size,
  127. u64 *dma_handle, gfp_t flag)
  128. {
  129. struct page *p;
  130. void *addr = NULL;
  131. p = alloc_pages(flag, get_order(size));
  132. if (p)
  133. addr = page_address(p);
  134. if (dma_handle)
  135. *dma_handle = (uintptr_t)addr;
  136. return addr;
  137. }
  138. static void rxe_dma_free_coherent(struct ib_device *dev, size_t size,
  139. void *cpu_addr, u64 dma_handle)
  140. {
  141. free_pages((unsigned long)cpu_addr, get_order(size));
  142. }
  143. struct ib_dma_mapping_ops rxe_dma_mapping_ops = {
  144. .mapping_error = rxe_mapping_error,
  145. .map_single = rxe_dma_map_single,
  146. .unmap_single = rxe_dma_unmap_single,
  147. .map_page = rxe_dma_map_page,
  148. .unmap_page = rxe_dma_unmap_page,
  149. .map_sg = rxe_map_sg,
  150. .unmap_sg = rxe_unmap_sg,
  151. .map_sg_attrs = rxe_map_sg_attrs,
  152. .unmap_sg_attrs = rxe_unmap_sg_attrs,
  153. .sync_single_for_cpu = rxe_sync_single_for_cpu,
  154. .sync_single_for_device = rxe_sync_single_for_device,
  155. .alloc_coherent = rxe_dma_alloc_coherent,
  156. .free_coherent = rxe_dma_free_coherent
  157. };