b54e4075edc9b4bf8682ccf9472f947692afabac
[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
6 #include "otx2_cryptodev.h"
7 #include "otx2_cryptodev_mbox.h"
8 #include "otx2_dev.h"
9 #include "otx2_mbox.h"
10
11 #include "cpt_pmd_logs.h"
12
13 int
14 otx2_cpt_available_queues_get(const struct rte_cryptodev *dev,
15                               uint16_t *nb_queues)
16 {
17         struct otx2_cpt_vf *vf = dev->data->dev_private;
18         struct otx2_dev *otx2_dev = &vf->otx2_dev;
19         struct free_rsrcs_rsp *rsp;
20         int ret;
21
22         otx2_mbox_alloc_msg_free_rsrc_cnt(otx2_dev->mbox);
23
24         ret = otx2_mbox_process_msg(otx2_dev->mbox, (void *)&rsp);
25         if (ret)
26                 return -EIO;
27
28         *nb_queues = rsp->cpt;
29         return 0;
30 }
31
32 int
33 otx2_cpt_queues_attach(const struct rte_cryptodev *dev, uint8_t nb_queues)
34 {
35         struct otx2_cpt_vf *vf = dev->data->dev_private;
36         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
37         struct rsrc_attach_req *req;
38
39         /* Ask AF to attach required LFs */
40
41         req = otx2_mbox_alloc_msg_attach_resources(mbox);
42
43         /* 1 LF = 1 queue */
44         req->cptlfs = nb_queues;
45
46         if (otx2_mbox_process(mbox) < 0)
47                 return -EIO;
48
49         /* Update number of attached queues */
50         vf->nb_queues = nb_queues;
51
52         return 0;
53 }
54
55 int
56 otx2_cpt_queues_detach(const struct rte_cryptodev *dev)
57 {
58         struct otx2_cpt_vf *vf = dev->data->dev_private;
59         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
60         struct rsrc_detach_req *req;
61
62         req = otx2_mbox_alloc_msg_detach_resources(mbox);
63         req->cptlfs = true;
64         req->partial = true;
65         if (otx2_mbox_process(mbox) < 0)
66                 return -EIO;
67
68         /* Queues have been detached */
69         vf->nb_queues = 0;
70
71         return 0;
72 }
73
74 int
75 otx2_cpt_msix_offsets_get(const struct rte_cryptodev *dev)
76 {
77         struct otx2_cpt_vf *vf = dev->data->dev_private;
78         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
79         struct msix_offset_rsp *rsp;
80         uint32_t i, ret;
81
82         /* Get CPT MSI-X vector offsets */
83
84         otx2_mbox_alloc_msg_msix_offset(mbox);
85
86         ret = otx2_mbox_process_msg(mbox, (void *)&rsp);
87         if (ret)
88                 return ret;
89
90         for (i = 0; i < vf->nb_queues; i++)
91                 vf->lf_msixoff[i] = rsp->cptlf_msixoff[i];
92
93         return 0;
94 }
95
96 static int
97 otx2_cpt_send_mbox_msg(struct otx2_cpt_vf *vf)
98 {
99         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
100         int ret;
101
102         otx2_mbox_msg_send(mbox, 0);
103
104         ret = otx2_mbox_wait_for_rsp(mbox, 0);
105         if (ret < 0) {
106                 CPT_LOG_ERR("Could not get mailbox response");
107                 return ret;
108         }
109
110         return 0;
111 }
112
113 int
114 otx2_cpt_af_reg_read(const struct rte_cryptodev *dev, uint64_t reg,
115                      uint64_t *val)
116 {
117         struct otx2_cpt_vf *vf = dev->data->dev_private;
118         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
119         struct otx2_mbox_dev *mdev = &mbox->dev[0];
120         struct cpt_rd_wr_reg_msg *msg;
121         int ret, off;
122
123         msg = (struct cpt_rd_wr_reg_msg *)
124                         otx2_mbox_alloc_msg_rsp(mbox, 0, sizeof(*msg),
125                                                 sizeof(*msg));
126         if (msg == NULL) {
127                 CPT_LOG_ERR("Could not allocate mailbox message");
128                 return -EFAULT;
129         }
130
131         msg->hdr.id = MBOX_MSG_CPT_RD_WR_REGISTER;
132         msg->hdr.sig = OTX2_MBOX_REQ_SIG;
133         msg->hdr.pcifunc = vf->otx2_dev.pf_func;
134         msg->is_write = 0;
135         msg->reg_offset = reg;
136         msg->ret_val = val;
137
138         ret = otx2_cpt_send_mbox_msg(vf);
139         if (ret < 0)
140                 return ret;
141
142         off = mbox->rx_start +
143                         RTE_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
144         msg = (struct cpt_rd_wr_reg_msg *) ((uintptr_t)mdev->mbase + off);
145
146         *val = msg->val;
147
148         return 0;
149 }
150
151 int
152 otx2_cpt_af_reg_write(const struct rte_cryptodev *dev, uint64_t reg,
153                       uint64_t val)
154 {
155         struct otx2_cpt_vf *vf = dev->data->dev_private;
156         struct otx2_mbox *mbox = vf->otx2_dev.mbox;
157         struct cpt_rd_wr_reg_msg *msg;
158
159         msg = (struct cpt_rd_wr_reg_msg *)
160                         otx2_mbox_alloc_msg_rsp(mbox, 0, sizeof(*msg),
161                                                 sizeof(*msg));
162         if (msg == NULL) {
163                 CPT_LOG_ERR("Could not allocate mailbox message");
164                 return -EFAULT;
165         }
166
167         msg->hdr.id = MBOX_MSG_CPT_RD_WR_REGISTER;
168         msg->hdr.sig = OTX2_MBOX_REQ_SIG;
169         msg->hdr.pcifunc = vf->otx2_dev.pf_func;
170         msg->is_write = 1;
171         msg->reg_offset = reg;
172         msg->val = val;
173
174         return otx2_cpt_send_mbox_msg(vf);
175 }