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