dma.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. struct scatterlist *
  48. qce_sgtable_add(struct sg_table *sgt, struct scatterlist *new_sgl)
  49. {
  50. struct scatterlist *sg = sgt->sgl, *sg_last = NULL;
  51. while (sg) {
  52. if (!sg_page(sg))
  53. break;
  54. sg = sg_next(sg);
  55. }
  56. if (!sg)
  57. return ERR_PTR(-EINVAL);
  58. while (new_sgl && sg) {
  59. sg_set_page(sg, sg_page(new_sgl), new_sgl->length,
  60. new_sgl->offset);
  61. sg_last = sg;
  62. sg = sg_next(sg);
  63. new_sgl = sg_next(new_sgl);
  64. }
  65. return sg_last;
  66. }
  67. static int qce_dma_prep_sg(struct dma_chan *chan, struct scatterlist *sg,
  68. int nents, unsigned long flags,
  69. enum dma_transfer_direction dir,
  70. dma_async_tx_callback cb, void *cb_param)
  71. {
  72. struct dma_async_tx_descriptor *desc;
  73. dma_cookie_t cookie;
  74. if (!sg || !nents)
  75. return -EINVAL;
  76. desc = dmaengine_prep_slave_sg(chan, sg, nents, dir, flags);
  77. if (!desc)
  78. return -EINVAL;
  79. desc->callback = cb;
  80. desc->callback_param = cb_param;
  81. cookie = dmaengine_submit(desc);
  82. return dma_submit_error(cookie);
  83. }
  84. int qce_dma_prep_sgs(struct qce_dma_data *dma, struct scatterlist *rx_sg,
  85. int rx_nents, struct scatterlist *tx_sg, int tx_nents,
  86. dma_async_tx_callback cb, void *cb_param)
  87. {
  88. struct dma_chan *rxchan = dma->rxchan;
  89. struct dma_chan *txchan = dma->txchan;
  90. unsigned long flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
  91. int ret;
  92. ret = qce_dma_prep_sg(rxchan, rx_sg, rx_nents, flags, DMA_MEM_TO_DEV,
  93. NULL, NULL);
  94. if (ret)
  95. return ret;
  96. return qce_dma_prep_sg(txchan, tx_sg, tx_nents, flags, DMA_DEV_TO_MEM,
  97. cb, cb_param);
  98. }
  99. void qce_dma_issue_pending(struct qce_dma_data *dma)
  100. {
  101. dma_async_issue_pending(dma->rxchan);
  102. dma_async_issue_pending(dma->txchan);
  103. }
  104. int qce_dma_terminate_all(struct qce_dma_data *dma)
  105. {
  106. int ret;
  107. ret = dmaengine_terminate_all(dma->rxchan);
  108. return ret ?: dmaengine_terminate_all(dma->txchan);
  109. }