dma.c 3.1 KB

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