ipath_dma.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2006 QLogic, Corporation. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/scatterlist.h>
  33. #include <linux/gfp.h>
  34. #include <rdma/ib_verbs.h>
  35. #include "ipath_verbs.h"
  36. #define BAD_DMA_ADDRESS ((u64) 0)
  37. /*
  38. * The following functions implement driver specific replacements
  39. * for the ib_dma_*() functions.
  40. *
  41. * These functions return kernel virtual addresses instead of
  42. * device bus addresses since the driver uses the CPU to copy
  43. * data instead of using hardware DMA.
  44. */
  45. static int ipath_mapping_error(struct ib_device *dev, u64 dma_addr)
  46. {
  47. return dma_addr == BAD_DMA_ADDRESS;
  48. }
  49. static u64 ipath_dma_map_single(struct ib_device *dev,
  50. void *cpu_addr, size_t size,
  51. enum dma_data_direction direction)
  52. {
  53. BUG_ON(!valid_dma_direction(direction));
  54. return (u64) cpu_addr;
  55. }
  56. static void ipath_dma_unmap_single(struct ib_device *dev,
  57. u64 addr, size_t size,
  58. enum dma_data_direction direction)
  59. {
  60. BUG_ON(!valid_dma_direction(direction));
  61. }
  62. static u64 ipath_dma_map_page(struct ib_device *dev,
  63. struct page *page,
  64. unsigned long offset,
  65. size_t size,
  66. enum dma_data_direction direction)
  67. {
  68. u64 addr;
  69. BUG_ON(!valid_dma_direction(direction));
  70. if (offset + size > PAGE_SIZE) {
  71. addr = BAD_DMA_ADDRESS;
  72. goto done;
  73. }
  74. addr = (u64) page_address(page);
  75. if (addr)
  76. addr += offset;
  77. /* TODO: handle highmem pages */
  78. done:
  79. return addr;
  80. }
  81. static void ipath_dma_unmap_page(struct ib_device *dev,
  82. u64 addr, size_t size,
  83. enum dma_data_direction direction)
  84. {
  85. BUG_ON(!valid_dma_direction(direction));
  86. }
  87. static int ipath_map_sg(struct ib_device *dev, struct scatterlist *sgl,
  88. int nents, enum dma_data_direction direction)
  89. {
  90. struct scatterlist *sg;
  91. u64 addr;
  92. int i;
  93. int ret = nents;
  94. BUG_ON(!valid_dma_direction(direction));
  95. for_each_sg(sgl, sg, nents, i) {
  96. addr = (u64) page_address(sg_page(sg));
  97. /* TODO: handle highmem pages */
  98. if (!addr) {
  99. ret = 0;
  100. break;
  101. }
  102. }
  103. return ret;
  104. }
  105. static void ipath_unmap_sg(struct ib_device *dev,
  106. struct scatterlist *sg, int nents,
  107. enum dma_data_direction direction)
  108. {
  109. BUG_ON(!valid_dma_direction(direction));
  110. }
  111. static u64 ipath_sg_dma_address(struct ib_device *dev, struct scatterlist *sg)
  112. {
  113. u64 addr = (u64) page_address(sg_page(sg));
  114. if (addr)
  115. addr += sg->offset;
  116. return addr;
  117. }
  118. static unsigned int ipath_sg_dma_len(struct ib_device *dev,
  119. struct scatterlist *sg)
  120. {
  121. return sg->length;
  122. }
  123. static void ipath_sync_single_for_cpu(struct ib_device *dev,
  124. u64 addr,
  125. size_t size,
  126. enum dma_data_direction dir)
  127. {
  128. }
  129. static void ipath_sync_single_for_device(struct ib_device *dev,
  130. u64 addr,
  131. size_t size,
  132. enum dma_data_direction dir)
  133. {
  134. }
  135. static void *ipath_dma_alloc_coherent(struct ib_device *dev, size_t size,
  136. u64 *dma_handle, gfp_t flag)
  137. {
  138. struct page *p;
  139. void *addr = NULL;
  140. p = alloc_pages(flag, get_order(size));
  141. if (p)
  142. addr = page_address(p);
  143. if (dma_handle)
  144. *dma_handle = (u64) addr;
  145. return addr;
  146. }
  147. static void ipath_dma_free_coherent(struct ib_device *dev, size_t size,
  148. void *cpu_addr, u64 dma_handle)
  149. {
  150. free_pages((unsigned long) cpu_addr, get_order(size));
  151. }
  152. struct ib_dma_mapping_ops ipath_dma_mapping_ops = {
  153. ipath_mapping_error,
  154. ipath_dma_map_single,
  155. ipath_dma_unmap_single,
  156. ipath_dma_map_page,
  157. ipath_dma_unmap_page,
  158. ipath_map_sg,
  159. ipath_unmap_sg,
  160. ipath_sg_dma_address,
  161. ipath_sg_dma_len,
  162. ipath_sync_single_for_cpu,
  163. ipath_sync_single_for_device,
  164. ipath_dma_alloc_coherent,
  165. ipath_dma_free_coherent
  166. };