dma.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/dmaengine.h>
  6. #include <crypto/scatterwalk.h>
  7. #include "dma.h"
  8. int qce_dma_request(struct device *dev, struct qce_dma_data *dma)
  9. {
  10. int ret;
  11. dma->txchan = dma_request_chan(dev, "tx");
  12. if (IS_ERR(dma->txchan))
  13. return PTR_ERR(dma->txchan);
  14. dma->rxchan = dma_request_chan(dev, "rx");
  15. if (IS_ERR(dma->rxchan)) {
  16. ret = PTR_ERR(dma->rxchan);
  17. goto error_rx;
  18. }
  19. dma->result_buf = kmalloc(QCE_RESULT_BUF_SZ + QCE_IGNORE_BUF_SZ,
  20. GFP_KERNEL);
  21. if (!dma->result_buf) {
  22. ret = -ENOMEM;
  23. goto error_nomem;
  24. }
  25. dma->ignore_buf = dma->result_buf + QCE_RESULT_BUF_SZ;
  26. return 0;
  27. error_nomem:
  28. dma_release_channel(dma->rxchan);
  29. error_rx:
  30. dma_release_channel(dma->txchan);
  31. return ret;
  32. }
  33. void qce_dma_release(struct qce_dma_data *dma)
  34. {
  35. dma_release_channel(dma->txchan);
  36. dma_release_channel(dma->rxchan);
  37. kfree(dma->result_buf);
  38. }
  39. struct scatterlist *
  40. qce_sgtable_add(struct sg_table *sgt, struct scatterlist *new_sgl,
  41. unsigned int max_len)
  42. {
  43. struct scatterlist *sg = sgt->sgl, *sg_last = NULL;
  44. unsigned int new_len;
  45. while (sg) {
  46. if (!sg_page(sg))
  47. break;
  48. sg = sg_next(sg);
  49. }
  50. if (!sg)
  51. return ERR_PTR(-EINVAL);
  52. while (new_sgl && sg && max_len) {
  53. new_len = new_sgl->length > max_len ? max_len : new_sgl->length;
  54. sg_set_page(sg, sg_page(new_sgl), new_len, new_sgl->offset);
  55. sg_last = sg;
  56. sg = sg_next(sg);
  57. new_sgl = sg_next(new_sgl);
  58. max_len -= new_len;
  59. }
  60. return sg_last;
  61. }
  62. static int qce_dma_prep_sg(struct dma_chan *chan, struct scatterlist *sg,
  63. int nents, unsigned long flags,
  64. enum dma_transfer_direction dir,
  65. dma_async_tx_callback cb, void *cb_param)
  66. {
  67. struct dma_async_tx_descriptor *desc;
  68. dma_cookie_t cookie;
  69. if (!sg || !nents)
  70. return -EINVAL;
  71. desc = dmaengine_prep_slave_sg(chan, sg, nents, dir, flags);
  72. if (!desc)
  73. return -EINVAL;
  74. desc->callback = cb;
  75. desc->callback_param = cb_param;
  76. cookie = dmaengine_submit(desc);
  77. return dma_submit_error(cookie);
  78. }
  79. int qce_dma_prep_sgs(struct qce_dma_data *dma, struct scatterlist *rx_sg,
  80. int rx_nents, struct scatterlist *tx_sg, int tx_nents,
  81. dma_async_tx_callback cb, void *cb_param)
  82. {
  83. struct dma_chan *rxchan = dma->rxchan;
  84. struct dma_chan *txchan = dma->txchan;
  85. unsigned long flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
  86. int ret;
  87. ret = qce_dma_prep_sg(rxchan, rx_sg, rx_nents, flags, DMA_MEM_TO_DEV,
  88. NULL, NULL);
  89. if (ret)
  90. return ret;
  91. return qce_dma_prep_sg(txchan, tx_sg, tx_nents, flags, DMA_DEV_TO_MEM,
  92. cb, cb_param);
  93. }
  94. void qce_dma_issue_pending(struct qce_dma_data *dma)
  95. {
  96. dma_async_issue_pending(dma->rxchan);
  97. dma_async_issue_pending(dma->txchan);
  98. }
  99. int qce_dma_terminate_all(struct qce_dma_data *dma)
  100. {
  101. int ret;
  102. ret = dmaengine_terminate_all(dma->rxchan);
  103. return ret ?: dmaengine_terminate_all(dma->txchan);
  104. }