1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved. 3 */ 4 5 #include <rte_string_fns.h> 6 #include <bus_pci_driver.h> 7 #include <bus_vdev_driver.h> 8 #include <rte_common.h> 9 #include <rte_cryptodev.h> 10 #include <cryptodev_pmd.h> 11 #include <rte_pci.h> 12 #include <dev_driver.h> 13 #include <rte_malloc.h> 14 15 #include "ccp_crypto.h" 16 #include "ccp_dev.h" 17 #include "ccp_pmd_private.h" 18 19 /** 20 * Global static parameter used to find if CCP device is already initialized. 21 */ 22 static unsigned int ccp_pmd_init_done; 23 uint8_t ccp_cryptodev_driver_id; 24 uint8_t cryptodev_cnt; 25 extern void *sha_ctx; 26 27 struct ccp_pmd_init_params { 28 struct rte_cryptodev_pmd_init_params def_p; 29 bool auth_opt; 30 }; 31 32 #define CCP_CRYPTODEV_PARAM_NAME ("name") 33 #define CCP_CRYPTODEV_PARAM_SOCKET_ID ("socket_id") 34 #define CCP_CRYPTODEV_PARAM_MAX_NB_QP ("max_nb_queue_pairs") 35 #define CCP_CRYPTODEV_PARAM_AUTH_OPT ("ccp_auth_opt") 36 37 const char *ccp_pmd_valid_params[] = { 38 CCP_CRYPTODEV_PARAM_NAME, 39 CCP_CRYPTODEV_PARAM_SOCKET_ID, 40 CCP_CRYPTODEV_PARAM_MAX_NB_QP, 41 CCP_CRYPTODEV_PARAM_AUTH_OPT, 42 }; 43 44 /** ccp pmd auth option */ 45 enum ccp_pmd_auth_opt { 46 CCP_PMD_AUTH_OPT_CCP = 0, 47 CCP_PMD_AUTH_OPT_CPU, 48 }; 49 50 static struct ccp_session * 51 get_ccp_session(struct ccp_qp *qp, struct rte_crypto_op *op) 52 { 53 struct ccp_session *sess = NULL; 54 55 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) { 56 if (unlikely(op->sym->session == NULL)) 57 return NULL; 58 59 sess = CRYPTODEV_GET_SYM_SESS_PRIV(op->sym->session); 60 } else if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) { 61 struct rte_cryptodev_sym_session *_sess; 62 struct ccp_private *internals; 63 64 if (rte_mempool_get(qp->sess_mp, (void **)&_sess)) 65 return NULL; 66 67 sess = (void *)_sess->driver_priv_data; 68 69 internals = (struct ccp_private *)qp->dev->data->dev_private; 70 if (unlikely(ccp_set_session_parameters(sess, op->sym->xform, 71 internals) != 0)) { 72 rte_mempool_put(qp->sess_mp, _sess); 73 sess = NULL; 74 } 75 op->sym->session = _sess; 76 } 77 78 return sess; 79 } 80 81 static uint16_t 82 ccp_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops, 83 uint16_t nb_ops) 84 { 85 struct ccp_session *sess = NULL; 86 struct ccp_qp *qp = queue_pair; 87 struct ccp_queue *cmd_q; 88 struct rte_cryptodev *dev = qp->dev; 89 uint16_t i, enq_cnt = 0, slots_req = 0; 90 uint16_t tmp_ops = nb_ops, b_idx, cur_ops = 0; 91 92 if (nb_ops == 0) 93 return 0; 94 95 if (unlikely(rte_ring_full(qp->processed_pkts) != 0)) 96 return 0; 97 if (tmp_ops >= cryptodev_cnt) 98 cur_ops = nb_ops / cryptodev_cnt + (nb_ops)%cryptodev_cnt; 99 else 100 cur_ops = tmp_ops; 101 while (tmp_ops) { 102 b_idx = nb_ops - tmp_ops; 103 slots_req = 0; 104 if (cur_ops <= tmp_ops) { 105 tmp_ops -= cur_ops; 106 } else { 107 cur_ops = tmp_ops; 108 tmp_ops = 0; 109 } 110 for (i = 0; i < cur_ops; i++) { 111 sess = get_ccp_session(qp, ops[i + b_idx]); 112 if (unlikely(sess == NULL) && (i == 0)) { 113 qp->qp_stats.enqueue_err_count++; 114 return 0; 115 } else if (sess == NULL) { 116 cur_ops = i; 117 break; 118 } 119 slots_req += ccp_compute_slot_count(sess); 120 } 121 122 cmd_q = ccp_allot_queue(dev, slots_req); 123 if (unlikely(cmd_q == NULL)) 124 return 0; 125 enq_cnt += process_ops_to_enqueue(qp, ops, cmd_q, cur_ops, 126 nb_ops, slots_req, b_idx); 127 i++; 128 } 129 130 qp->qp_stats.enqueued_count += enq_cnt; 131 return enq_cnt; 132 } 133 134 static uint16_t 135 ccp_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops, 136 uint16_t nb_ops) 137 { 138 struct ccp_qp *qp = queue_pair; 139 uint16_t nb_dequeued = 0, i, total_nb_ops; 140 141 nb_dequeued = process_ops_to_dequeue(qp, ops, nb_ops, &total_nb_ops); 142 143 if (total_nb_ops) { 144 while (nb_dequeued != total_nb_ops) { 145 nb_dequeued = process_ops_to_dequeue(qp, 146 ops, nb_ops, &total_nb_ops); 147 } 148 } 149 150 /* Free session if a session-less crypto op */ 151 for (i = 0; i < nb_dequeued; i++) 152 if (unlikely(ops[i]->sess_type == 153 RTE_CRYPTO_OP_SESSIONLESS)) { 154 struct ccp_session *sess = 155 CRYPTODEV_GET_SYM_SESS_PRIV(ops[i]->sym->session); 156 157 memset(sess, 0, sizeof(*sess)); 158 rte_mempool_put(qp->sess_mp, 159 ops[i]->sym->session); 160 ops[i]->sym->session = NULL; 161 } 162 qp->qp_stats.dequeued_count += nb_dequeued; 163 164 return nb_dequeued; 165 } 166 167 /* 168 * The set of PCI devices this driver supports 169 */ 170 static struct rte_pci_id ccp_pci_id[] = { 171 { 172 RTE_PCI_DEVICE(0x1022, 0x1456), /* AMD CCP-5a */ 173 }, 174 { 175 RTE_PCI_DEVICE(0x1022, 0x1468), /* AMD CCP-5b */ 176 }, 177 { 178 RTE_PCI_DEVICE(0x1022, 0x15df), /* AMD CCP RV */ 179 }, 180 {.device_id = 0}, 181 }; 182 183 /** Remove ccp pmd */ 184 static int 185 cryptodev_ccp_remove(struct rte_pci_device *pci_dev) 186 { 187 char name[RTE_CRYPTODEV_NAME_MAX_LEN]; 188 struct rte_cryptodev *dev; 189 190 if (pci_dev == NULL) 191 return -EINVAL; 192 193 rte_pci_device_name(&pci_dev->addr, name, sizeof(name)); 194 195 if (name[0] == '\0') 196 return -EINVAL; 197 198 dev = rte_cryptodev_pmd_get_named_dev(name); 199 if (dev == NULL) 200 return -ENODEV; 201 202 ccp_pmd_init_done = 0; 203 rte_free(sha_ctx); 204 205 RTE_LOG(INFO, PMD, "Closing ccp device %s on numa socket %u\n", 206 name, rte_socket_id()); 207 208 return rte_cryptodev_pmd_destroy(dev); 209 } 210 211 /** Create crypto device */ 212 static int 213 cryptodev_ccp_create(const char *name, 214 struct rte_pci_device *pci_dev, 215 struct ccp_pmd_init_params *init_params, 216 struct rte_pci_driver *pci_drv) 217 { 218 struct rte_cryptodev *dev; 219 struct ccp_private *internals; 220 221 if (init_params->def_p.name[0] == '\0') 222 strlcpy(init_params->def_p.name, name, 223 sizeof(init_params->def_p.name)); 224 225 dev = rte_cryptodev_pmd_create(init_params->def_p.name, 226 &pci_dev->device, 227 &init_params->def_p); 228 if (dev == NULL) { 229 CCP_LOG_ERR("failed to create cryptodev vdev"); 230 goto init_error; 231 } 232 233 cryptodev_cnt = ccp_probe_devices(pci_dev, ccp_pci_id); 234 235 if (cryptodev_cnt == 0) { 236 CCP_LOG_ERR("failed to detect CCP crypto device"); 237 goto init_error; 238 } 239 240 printf("CCP : Crypto device count = %d\n", cryptodev_cnt); 241 dev->device = &pci_dev->device; 242 dev->device->driver = &pci_drv->driver; 243 dev->driver_id = ccp_cryptodev_driver_id; 244 245 /* register rx/tx burst functions for data path */ 246 dev->dev_ops = ccp_pmd_ops; 247 dev->enqueue_burst = ccp_pmd_enqueue_burst; 248 dev->dequeue_burst = ccp_pmd_dequeue_burst; 249 250 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO | 251 RTE_CRYPTODEV_FF_HW_ACCELERATED | 252 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING | 253 RTE_CRYPTODEV_FF_SYM_SESSIONLESS; 254 255 internals = dev->data->dev_private; 256 257 internals->max_nb_qpairs = init_params->def_p.max_nb_queue_pairs; 258 internals->auth_opt = init_params->auth_opt; 259 internals->crypto_num_dev = cryptodev_cnt; 260 261 rte_cryptodev_pmd_probing_finish(dev); 262 263 return 0; 264 265 init_error: 266 CCP_LOG_ERR("driver %s: %s() failed", 267 init_params->def_p.name, __func__); 268 cryptodev_ccp_remove(pci_dev); 269 270 return -EFAULT; 271 } 272 273 /** Probe ccp pmd */ 274 static int 275 cryptodev_ccp_probe(struct rte_pci_driver *pci_drv __rte_unused, 276 struct rte_pci_device *pci_dev) 277 { 278 int rc = 0; 279 char name[RTE_CRYPTODEV_NAME_MAX_LEN]; 280 struct ccp_pmd_init_params init_params = { 281 .def_p = { 282 "", 283 sizeof(struct ccp_private), 284 rte_socket_id(), 285 CCP_PMD_MAX_QUEUE_PAIRS 286 }, 287 .auth_opt = CCP_PMD_AUTH_OPT_CCP, 288 }; 289 290 sha_ctx = (void *)rte_malloc(NULL, SHA512_DIGEST_SIZE, 64); 291 if (ccp_pmd_init_done) { 292 RTE_LOG(INFO, PMD, "CCP PMD already initialized\n"); 293 return -EFAULT; 294 } 295 rte_pci_device_name(&pci_dev->addr, name, sizeof(name)); 296 if (name[0] == '\0') 297 return -EINVAL; 298 299 init_params.def_p.max_nb_queue_pairs = CCP_PMD_MAX_QUEUE_PAIRS; 300 301 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name, 302 init_params.def_p.socket_id); 303 RTE_LOG(INFO, PMD, "Max number of queue pairs = %d\n", 304 init_params.def_p.max_nb_queue_pairs); 305 RTE_LOG(INFO, PMD, "Authentication offload to %s\n", 306 ((init_params.auth_opt == 0) ? "CCP" : "CPU")); 307 308 rte_pci_device_name(&pci_dev->addr, name, sizeof(name)); 309 310 rc = cryptodev_ccp_create(name, pci_dev, &init_params, pci_drv); 311 if (rc) 312 return rc; 313 ccp_pmd_init_done = 1; 314 return 0; 315 } 316 317 static struct rte_pci_driver cryptodev_ccp_pmd_drv = { 318 .id_table = ccp_pci_id, 319 .drv_flags = RTE_PCI_DRV_NEED_MAPPING, 320 .probe = cryptodev_ccp_probe, 321 .remove = cryptodev_ccp_remove 322 }; 323 324 static struct cryptodev_driver ccp_crypto_drv; 325 326 RTE_PMD_REGISTER_PCI(CRYPTODEV_NAME_CCP_PMD, cryptodev_ccp_pmd_drv); 327 RTE_PMD_REGISTER_KMOD_DEP(CRYPTODEV_NAME_CCP_PMD, "* igb_uio | uio_pci_generic | vfio-pci"); 328 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_CCP_PMD, 329 "max_nb_queue_pairs=<int> " 330 "socket_id=<int> " 331 "ccp_auth_opt=<int>"); 332 RTE_PMD_REGISTER_CRYPTO_DRIVER(ccp_crypto_drv, cryptodev_ccp_pmd_drv.driver, 333 ccp_cryptodev_driver_id); 334