dma.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/dmaengine.h>
  14. #include <crypto/scatterwalk.h>
  15. #include "dma.h"
  16. int qce_dma_request(struct device *dev, struct qce_dma_data *dma)
  17. {
  18. int ret;
  19. dma->txchan = dma_request_slave_channel_reason(dev, "tx");
  20. if (IS_ERR(dma->txchan))
  21. return PTR_ERR(dma->txchan);
  22. dma->rxchan = dma_request_slave_channel_reason(dev, "rx");
  23. if (IS_ERR(dma->rxchan)) {
  24. ret = PTR_ERR(dma->rxchan);
  25. goto error_rx;
  26. }
  27. dma->result_buf = kmalloc(QCE_RESULT_BUF_SZ + QCE_IGNORE_BUF_SZ,
  28. GFP_KERNEL);
  29. if (!dma->result_buf) {
  30. ret = -ENOMEM;
  31. goto error_nomem;
  32. }
  33. dma->ignore_buf = dma->result_buf + QCE_RESULT_BUF_SZ;
  34. return 0;
  35. error_nomem:
  36. dma_release_channel(dma->rxchan);
  37. error_rx:
  38. dma_release_channel(dma->txchan);
  39. return ret;
  40. }
  41. void qce_dma_release(struct qce_dma_data *dma)
  42. {
  43. dma_release_channel(dma->txchan);
  44. dma_release_channel(dma->rxchan);
  45. kfree(dma->result_buf);
  46. }
  47. int qce_mapsg(struct device *dev, struct scatterlist *sg, int nents,
  48. enum dma_data_direction dir, bool chained)
  49. {
  50. int err;
  51. if (chained) {
  52. while (sg) {
  53. err = dma_map_sg(dev, sg, 1, dir);
  54. if (!err)
  55. return -EFAULT;
  56. sg = sg_next(sg);
  57. }
  58. } else {
  59. err = dma_map_sg(dev, sg, nents, dir);
  60. if (!err)
  61. return -EFAULT;
  62. }
  63. return nents;
  64. }
  65. void qce_unmapsg(struct device *dev, struct scatterlist *sg, int nents,
  66. enum dma_data_direction dir, bool chained)
  67. {
  68. if (chained)
  69. while (sg) {
  70. dma_unmap_sg(dev, sg, 1, dir);
  71. sg = sg_next(sg);
  72. }
  73. else
  74. dma_unmap_sg(dev, sg, nents, dir);
  75. }
  76. int qce_countsg(struct scatterlist *sglist, int nbytes, bool *chained)
  77. {
  78. struct scatterlist *sg = sglist;
  79. int nents = 0;
  80. if (chained)
  81. *chained = false;
  82. while (nbytes > 0 && sg) {
  83. nents++;
  84. nbytes -= sg->length;
  85. if (!sg_is_last(sg) && (sg + 1)->length == 0 && chained)
  86. *chained = true;
  87. sg = sg_next(sg);
  88. }
  89. return nents;
  90. }
  91. struct scatterlist *
  92. qce_sgtable_add(struct sg_table *sgt, struct scatterlist *new_sgl)
  93. {
  94. struct scatterlist *sg = sgt->sgl, *sg_last = NULL;
  95. while (sg) {
  96. if (!sg_page(sg))
  97. break;
  98. sg = sg_next(sg);
  99. }
  100. if (!sg)
  101. return ERR_PTR(-EINVAL);
  102. while (new_sgl && sg) {
  103. sg_set_page(sg, sg_page(new_sgl), new_sgl->length,
  104. new_sgl->offset);
  105. sg_last = sg;
  106. sg = sg_next(sg);
  107. new_sgl = sg_next(new_sgl);
  108. }
  109. return sg_last;
  110. }
  111. static int qce_dma_prep_sg(struct dma_chan *chan, struct scatterlist *sg,
  112. int nents, unsigned long flags,
  113. enum dma_transfer_direction dir,
  114. dma_async_tx_callback cb, void *cb_param)
  115. {
  116. struct dma_async_tx_descriptor *desc;
  117. dma_cookie_t cookie;
  118. if (!sg || !nents)
  119. return -EINVAL;
  120. desc = dmaengine_prep_slave_sg(chan, sg, nents, dir, flags);
  121. if (!desc)
  122. return -EINVAL;
  123. desc->callback = cb;
  124. desc->callback_param = cb_param;
  125. cookie = dmaengine_submit(desc);
  126. return dma_submit_error(cookie);
  127. }
  128. int qce_dma_prep_sgs(struct qce_dma_data *dma, struct scatterlist *rx_sg,
  129. int rx_nents, struct scatterlist *tx_sg, int tx_nents,
  130. dma_async_tx_callback cb, void *cb_param)
  131. {
  132. struct dma_chan *rxchan = dma->rxchan;
  133. struct dma_chan *txchan = dma->txchan;
  134. unsigned long flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
  135. int ret;
  136. ret = qce_dma_prep_sg(rxchan, rx_sg, rx_nents, flags, DMA_MEM_TO_DEV,
  137. NULL, NULL);
  138. if (ret)
  139. return ret;
  140. return qce_dma_prep_sg(txchan, tx_sg, tx_nents, flags, DMA_DEV_TO_MEM,
  141. cb, cb_param);
  142. }
  143. void qce_dma_issue_pending(struct qce_dma_data *dma)
  144. {
  145. dma_async_issue_pending(dma->rxchan);
  146. dma_async_issue_pending(dma->txchan);
  147. }
  148. int qce_dma_terminate_all(struct qce_dma_data *dma)
  149. {
  150. int ret;
  151. ret = dmaengine_terminate_all(dma->rxchan);
  152. return ret ?: dmaengine_terminate_all(dma->txchan);
  153. }