qib_dma.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2006, 2009, 2010 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/types.h>
  33. #include <linux/scatterlist.h>
  34. #include "qib_verbs.h"
  35. #define BAD_DMA_ADDRESS ((u64) 0)
  36. /*
  37. * The following functions implement driver specific replacements
  38. * for the ib_dma_*() functions.
  39. *
  40. * These functions return kernel virtual addresses instead of
  41. * device bus addresses since the driver uses the CPU to copy
  42. * data instead of using hardware DMA.
  43. */
  44. static int qib_mapping_error(struct ib_device *dev, u64 dma_addr)
  45. {
  46. return dma_addr == BAD_DMA_ADDRESS;
  47. }
  48. static u64 qib_dma_map_single(struct ib_device *dev, void *cpu_addr,
  49. size_t size, enum dma_data_direction direction)
  50. {
  51. BUG_ON(!valid_dma_direction(direction));
  52. return (u64) cpu_addr;
  53. }
  54. static void qib_dma_unmap_single(struct ib_device *dev, u64 addr, size_t size,
  55. enum dma_data_direction direction)
  56. {
  57. BUG_ON(!valid_dma_direction(direction));
  58. }
  59. static u64 qib_dma_map_page(struct ib_device *dev, struct page *page,
  60. unsigned long offset, size_t size,
  61. enum dma_data_direction direction)
  62. {
  63. u64 addr;
  64. BUG_ON(!valid_dma_direction(direction));
  65. if (offset + size > PAGE_SIZE) {
  66. addr = BAD_DMA_ADDRESS;
  67. goto done;
  68. }
  69. addr = (u64) page_address(page);
  70. if (addr)
  71. addr += offset;
  72. /* TODO: handle highmem pages */
  73. done:
  74. return addr;
  75. }
  76. static void qib_dma_unmap_page(struct ib_device *dev, u64 addr, size_t size,
  77. enum dma_data_direction direction)
  78. {
  79. BUG_ON(!valid_dma_direction(direction));
  80. }
  81. static int qib_map_sg(struct ib_device *dev, struct scatterlist *sgl,
  82. int nents, enum dma_data_direction direction)
  83. {
  84. struct scatterlist *sg;
  85. u64 addr;
  86. int i;
  87. int ret = nents;
  88. BUG_ON(!valid_dma_direction(direction));
  89. for_each_sg(sgl, sg, nents, i) {
  90. addr = (u64) page_address(sg_page(sg));
  91. /* TODO: handle highmem pages */
  92. if (!addr) {
  93. ret = 0;
  94. break;
  95. }
  96. sg->dma_address = addr + sg->offset;
  97. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  98. sg->dma_length = sg->length;
  99. #endif
  100. }
  101. return ret;
  102. }
  103. static void qib_unmap_sg(struct ib_device *dev,
  104. struct scatterlist *sg, int nents,
  105. enum dma_data_direction direction)
  106. {
  107. BUG_ON(!valid_dma_direction(direction));
  108. }
  109. static void qib_sync_single_for_cpu(struct ib_device *dev, u64 addr,
  110. size_t size, enum dma_data_direction dir)
  111. {
  112. }
  113. static void qib_sync_single_for_device(struct ib_device *dev, u64 addr,
  114. size_t size,
  115. enum dma_data_direction dir)
  116. {
  117. }
  118. static void *qib_dma_alloc_coherent(struct ib_device *dev, size_t size,
  119. u64 *dma_handle, gfp_t flag)
  120. {
  121. struct page *p;
  122. void *addr = NULL;
  123. p = alloc_pages(flag, get_order(size));
  124. if (p)
  125. addr = page_address(p);
  126. if (dma_handle)
  127. *dma_handle = (u64) addr;
  128. return addr;
  129. }
  130. static void qib_dma_free_coherent(struct ib_device *dev, size_t size,
  131. void *cpu_addr, u64 dma_handle)
  132. {
  133. free_pages((unsigned long) cpu_addr, get_order(size));
  134. }
  135. struct ib_dma_mapping_ops qib_dma_mapping_ops = {
  136. .mapping_error = qib_mapping_error,
  137. .map_single = qib_dma_map_single,
  138. .unmap_single = qib_dma_unmap_single,
  139. .map_page = qib_dma_map_page,
  140. .unmap_page = qib_dma_unmap_page,
  141. .map_sg = qib_map_sg,
  142. .unmap_sg = qib_unmap_sg,
  143. .sync_single_for_cpu = qib_sync_single_for_cpu,
  144. .sync_single_for_device = qib_sync_single_for_device,
  145. .alloc_coherent = qib_dma_alloc_coherent,
  146. .free_coherent = qib_dma_free_coherent
  147. };