sleep in control plane thread
[dpdk.git] / drivers / crypto / octeontx2 / otx2_cryptodev_mbox.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (C) 2019 Marvell International Ltd.
3  */
4 #include <rte_cryptodev.h>
5 #include <rte_ethdev.h>
6
7 #include "otx2_cryptodev.h"
8 #include "otx2_cryptodev_hw_access.h"
9 #include "otx2_cryptodev_mbox.h"
10 #include "otx2_dev.h"
11 #include "otx2_ethdev.h"
12 #include "otx2_sec_idev.h"
13 #include "otx2_mbox.h"
14
15 #include "cpt_pmd_logs.h"
16
17 int
18 otx2_cpt_available_queues_get(const struct rte_cryptodev *dev,
19                               uint16_t *nb_queues)
20 {
21         struct otx2_cpt_vf *vf = dev->data->dev_private;
22         struct otx2_dev *otx2_dev = &vf->otx2_dev;
23         struct free_rsrcs_rsp *rsp;
24         int ret;
25
26         otx2_mbox_alloc_msg_free_rsrc_cnt(otx2_dev->mbox);
27
28         ret = otx2_mbox_process_msg(otx2_dev->mbox, (void *)&rsp);
29         if (ret)
30                 return -EIO;
31
32         *nb_queues = rsp->cpt;
33         return 0;
34 }
35
36 int
37 otx2_cpt_queues_attach(const struct rte_cryptodev *dev, uint8_t nb_queues)
38 {
39         struct otx2_cpt_vf *vf = dev->data->dev_private;
40         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
41         struct rsrc_attach_req *req;
42
43         /* Ask AF to attach required LFs */
44
45         req = otx2_mbox_alloc_msg_attach_resources(mbox);
46
47         /* 1 LF = 1 queue */
48         req->cptlfs = nb_queues;
49
50         if (otx2_mbox_process(mbox) < 0)
51                 return -EIO;
52
53         /* Update number of attached queues */
54         vf->nb_queues = nb_queues;
55
56         return 0;
57 }
58
59 int
60 otx2_cpt_queues_detach(const struct rte_cryptodev *dev)
61 {
62         struct otx2_cpt_vf *vf = dev->data->dev_private;
63         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
64         struct rsrc_detach_req *req;
65
66         req = otx2_mbox_alloc_msg_detach_resources(mbox);
67         req->cptlfs = true;
68         req->partial = true;
69         if (otx2_mbox_process(mbox) < 0)
70                 return -EIO;
71
72         /* Queues have been detached */
73         vf->nb_queues = 0;
74
75         return 0;
76 }
77
78 int
79 otx2_cpt_msix_offsets_get(const struct rte_cryptodev *dev)
80 {
81         struct otx2_cpt_vf *vf = dev->data->dev_private;
82         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
83         struct msix_offset_rsp *rsp;
84         uint32_t i, ret;
85
86         /* Get CPT MSI-X vector offsets */
87
88         otx2_mbox_alloc_msg_msix_offset(mbox);
89
90         ret = otx2_mbox_process_msg(mbox, (void *)&rsp);
91         if (ret)
92                 return ret;
93
94         for (i = 0; i < vf->nb_queues; i++)
95                 vf->lf_msixoff[i] = rsp->cptlf_msixoff[i];
96
97         return 0;
98 }
99
100 static int
101 otx2_cpt_send_mbox_msg(struct otx2_cpt_vf *vf)
102 {
103         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
104         int ret;
105
106         otx2_mbox_msg_send(mbox, 0);
107
108         ret = otx2_mbox_wait_for_rsp(mbox, 0);
109         if (ret < 0) {
110                 CPT_LOG_ERR("Could not get mailbox response");
111                 return ret;
112         }
113
114         return 0;
115 }
116
117 int
118 otx2_cpt_af_reg_read(const struct rte_cryptodev *dev, uint64_t reg,
119                      uint64_t *val)
120 {
121         struct otx2_cpt_vf *vf = dev->data->dev_private;
122         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
123         struct otx2_mbox_dev *mdev = &mbox->dev[0];
124         struct cpt_rd_wr_reg_msg *msg;
125         int ret, off;
126
127         msg = (struct cpt_rd_wr_reg_msg *)
128                         otx2_mbox_alloc_msg_rsp(mbox, 0, sizeof(*msg),
129                                                 sizeof(*msg));
130         if (msg == NULL) {
131                 CPT_LOG_ERR("Could not allocate mailbox message");
132                 return -EFAULT;
133         }
134
135         msg->hdr.id = MBOX_MSG_CPT_RD_WR_REGISTER;
136         msg->hdr.sig = OTX2_MBOX_REQ_SIG;
137         msg->hdr.pcifunc = vf->otx2_dev.pf_func;
138         msg->is_write = 0;
139         msg->reg_offset = reg;
140         msg->ret_val = val;
141
142         ret = otx2_cpt_send_mbox_msg(vf);
143         if (ret < 0)
144                 return ret;
145
146         off = mbox->rx_start +
147                         RTE_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
148         msg = (struct cpt_rd_wr_reg_msg *) ((uintptr_t)mdev->mbase + off);
149
150         *val = msg->val;
151
152         return 0;
153 }
154
155 int
156 otx2_cpt_af_reg_write(const struct rte_cryptodev *dev, uint64_t reg,
157                       uint64_t val)
158 {
159         struct otx2_cpt_vf *vf = dev->data->dev_private;
160         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
161         struct cpt_rd_wr_reg_msg *msg;
162
163         msg = (struct cpt_rd_wr_reg_msg *)
164                         otx2_mbox_alloc_msg_rsp(mbox, 0, sizeof(*msg),
165                                                 sizeof(*msg));
166         if (msg == NULL) {
167                 CPT_LOG_ERR("Could not allocate mailbox message");
168                 return -EFAULT;
169         }
170
171         msg->hdr.id = MBOX_MSG_CPT_RD_WR_REGISTER;
172         msg->hdr.sig = OTX2_MBOX_REQ_SIG;
173         msg->hdr.pcifunc = vf->otx2_dev.pf_func;
174         msg->is_write = 1;
175         msg->reg_offset = reg;
176         msg->val = val;
177
178         return otx2_cpt_send_mbox_msg(vf);
179 }
180
181 int
182 otx2_cpt_inline_init(const struct rte_cryptodev *dev)
183 {
184         struct otx2_cpt_vf *vf = dev->data->dev_private;
185         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
186         struct cpt_rx_inline_lf_cfg_msg *msg;
187         int ret;
188
189         msg = otx2_mbox_alloc_msg_cpt_rx_inline_lf_cfg(mbox);
190         msg->sso_pf_func = otx2_sso_pf_func_get();
191
192         otx2_mbox_msg_send(mbox, 0);
193         ret = otx2_mbox_process(mbox);
194         if (ret < 0)
195                 return -EIO;
196
197         return 0;
198 }
199
200 int
201 otx2_cpt_qp_ethdev_bind(const struct rte_cryptodev *dev, struct otx2_cpt_qp *qp,
202                         uint16_t port_id)
203 {
204         struct rte_eth_dev *eth_dev = &rte_eth_devices[port_id];
205         struct otx2_cpt_vf *vf = dev->data->dev_private;
206         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
207         struct cpt_inline_ipsec_cfg_msg *msg;
208         struct otx2_eth_dev *otx2_eth_dev;
209         int ret;
210
211         if (!otx2_eth_dev_is_sec_capable(&rte_eth_devices[port_id]))
212                 return -EINVAL;
213
214         otx2_eth_dev = otx2_eth_pmd_priv(eth_dev);
215
216         msg = otx2_mbox_alloc_msg_cpt_inline_ipsec_cfg(mbox);
217         msg->dir = CPT_INLINE_OUTBOUND;
218         msg->enable = 1;
219         msg->slot = qp->id;
220
221         msg->nix_pf_func = otx2_eth_dev->pf_func;
222
223         otx2_mbox_msg_send(mbox, 0);
224         ret = otx2_mbox_process(mbox);
225         if (ret < 0)
226                 return -EIO;
227
228         return 0;
229 }