1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2018 Cavium, Inc 3 */ 4 5 #include <rte_alarm.h> 6 #include <bus_pci_driver.h> 7 #include <rte_cryptodev.h> 8 #include <cryptodev_pmd.h> 9 #include <rte_eventdev.h> 10 #include <rte_event_crypto_adapter.h> 11 #include <rte_errno.h> 12 #include <rte_malloc.h> 13 #include <rte_mempool.h> 14 15 #include "otx_cryptodev.h" 16 #include "otx_cryptodev_capabilities.h" 17 #include "otx_cryptodev_hw_access.h" 18 #include "otx_cryptodev_mbox.h" 19 #include "otx_cryptodev_ops.h" 20 21 #include "cpt_pmd_logs.h" 22 #include "cpt_pmd_ops_helper.h" 23 #include "cpt_ucode.h" 24 #include "cpt_ucode_asym.h" 25 26 #include "ssovf_worker.h" 27 28 static uint64_t otx_fpm_iova[CPT_EC_ID_PMAX]; 29 30 /* Forward declarations */ 31 32 static int 33 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id); 34 35 /* Alarm routines */ 36 37 static void 38 otx_cpt_alarm_cb(void *arg) 39 { 40 struct cpt_vf *cptvf = arg; 41 otx_cpt_poll_misc(cptvf); 42 rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000, 43 otx_cpt_alarm_cb, cptvf); 44 } 45 46 static int 47 otx_cpt_periodic_alarm_start(void *arg) 48 { 49 return rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000, 50 otx_cpt_alarm_cb, arg); 51 } 52 53 static int 54 otx_cpt_periodic_alarm_stop(void *arg) 55 { 56 return rte_eal_alarm_cancel(otx_cpt_alarm_cb, arg); 57 } 58 59 /* PMD ops */ 60 61 static int 62 otx_cpt_dev_config(struct rte_cryptodev *dev, 63 struct rte_cryptodev_config *config __rte_unused) 64 { 65 int ret = 0; 66 67 CPT_PMD_INIT_FUNC_TRACE(); 68 69 if (dev->feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO) 70 /* Initialize shared FPM table */ 71 ret = cpt_fpm_init(otx_fpm_iova); 72 73 return ret; 74 } 75 76 static int 77 otx_cpt_dev_start(struct rte_cryptodev *c_dev) 78 { 79 void *cptvf = c_dev->data->dev_private; 80 81 CPT_PMD_INIT_FUNC_TRACE(); 82 83 return otx_cpt_start_device(cptvf); 84 } 85 86 static void 87 otx_cpt_dev_stop(struct rte_cryptodev *c_dev) 88 { 89 void *cptvf = c_dev->data->dev_private; 90 91 CPT_PMD_INIT_FUNC_TRACE(); 92 93 if (c_dev->feature_flags & RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO) 94 cpt_fpm_clear(); 95 96 otx_cpt_stop_device(cptvf); 97 } 98 99 static int 100 otx_cpt_dev_close(struct rte_cryptodev *c_dev) 101 { 102 void *cptvf = c_dev->data->dev_private; 103 int i, ret; 104 105 CPT_PMD_INIT_FUNC_TRACE(); 106 107 for (i = 0; i < c_dev->data->nb_queue_pairs; i++) { 108 ret = otx_cpt_que_pair_release(c_dev, i); 109 if (ret) 110 return ret; 111 } 112 113 otx_cpt_periodic_alarm_stop(cptvf); 114 otx_cpt_deinit_device(cptvf); 115 116 return 0; 117 } 118 119 static void 120 otx_cpt_dev_info_get(struct rte_cryptodev *dev, struct rte_cryptodev_info *info) 121 { 122 CPT_PMD_INIT_FUNC_TRACE(); 123 if (info != NULL) { 124 info->max_nb_queue_pairs = CPT_NUM_QS_PER_VF; 125 info->feature_flags = dev->feature_flags; 126 info->capabilities = otx_get_capabilities(info->feature_flags); 127 info->sym.max_nb_sessions = 0; 128 info->driver_id = otx_cryptodev_driver_id; 129 info->min_mbuf_headroom_req = OTX_CPT_MIN_HEADROOM_REQ; 130 info->min_mbuf_tailroom_req = OTX_CPT_MIN_TAILROOM_REQ; 131 } 132 } 133 134 static int 135 otx_cpt_que_pair_setup(struct rte_cryptodev *dev, 136 uint16_t que_pair_id, 137 const struct rte_cryptodev_qp_conf *qp_conf, 138 int socket_id __rte_unused) 139 { 140 struct cpt_instance *instance = NULL; 141 struct rte_pci_device *pci_dev; 142 int ret = -1; 143 144 CPT_PMD_INIT_FUNC_TRACE(); 145 146 if (dev->data->queue_pairs[que_pair_id] != NULL) { 147 ret = otx_cpt_que_pair_release(dev, que_pair_id); 148 if (ret) 149 return ret; 150 } 151 152 if (qp_conf->nb_descriptors > DEFAULT_CMD_QLEN) { 153 CPT_LOG_INFO("Number of descriptors too big %d, using default " 154 "queue length of %d", qp_conf->nb_descriptors, 155 DEFAULT_CMD_QLEN); 156 } 157 158 pci_dev = RTE_DEV_TO_PCI(dev->device); 159 160 if (pci_dev->mem_resource[0].addr == NULL) { 161 CPT_LOG_ERR("PCI mem address null"); 162 return -EIO; 163 } 164 165 ret = otx_cpt_get_resource(dev, 0, &instance, que_pair_id); 166 if (ret != 0 || instance == NULL) { 167 CPT_LOG_ERR("Error getting instance handle from device %s : " 168 "ret = %d", dev->data->name, ret); 169 return ret; 170 } 171 172 instance->queue_id = que_pair_id; 173 instance->sess_mp = qp_conf->mp_session; 174 dev->data->queue_pairs[que_pair_id] = instance; 175 176 return 0; 177 } 178 179 static int 180 otx_cpt_que_pair_release(struct rte_cryptodev *dev, uint16_t que_pair_id) 181 { 182 struct cpt_instance *instance = dev->data->queue_pairs[que_pair_id]; 183 int ret; 184 185 CPT_PMD_INIT_FUNC_TRACE(); 186 187 ret = otx_cpt_put_resource(instance); 188 if (ret != 0) { 189 CPT_LOG_ERR("Error putting instance handle of device %s : " 190 "ret = %d", dev->data->name, ret); 191 return ret; 192 } 193 194 dev->data->queue_pairs[que_pair_id] = NULL; 195 196 return 0; 197 } 198 199 static unsigned int 200 otx_cpt_get_session_size(struct rte_cryptodev *dev __rte_unused) 201 { 202 return cpt_get_session_size(); 203 } 204 205 static int 206 sym_xform_verify(struct rte_crypto_sym_xform *xform) 207 { 208 if (xform->next) { 209 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH && 210 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER && 211 xform->next->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT && 212 (xform->auth.algo != RTE_CRYPTO_AUTH_SHA1_HMAC || 213 xform->next->cipher.algo != RTE_CRYPTO_CIPHER_AES_CBC)) 214 return -ENOTSUP; 215 216 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && 217 xform->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT && 218 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH && 219 (xform->cipher.algo != RTE_CRYPTO_CIPHER_AES_CBC || 220 xform->next->auth.algo != RTE_CRYPTO_AUTH_SHA1_HMAC)) 221 return -ENOTSUP; 222 223 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && 224 xform->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC && 225 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH && 226 xform->next->auth.algo == RTE_CRYPTO_AUTH_SHA1) 227 return -ENOTSUP; 228 229 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH && 230 xform->auth.algo == RTE_CRYPTO_AUTH_SHA1 && 231 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER && 232 xform->next->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC) 233 return -ENOTSUP; 234 235 } else { 236 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH && 237 xform->auth.algo == RTE_CRYPTO_AUTH_NULL && 238 xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY) 239 return -ENOTSUP; 240 } 241 return 0; 242 } 243 244 static int 245 sym_session_configure(struct rte_crypto_sym_xform *xform, 246 struct rte_cryptodev_sym_session *sess) 247 { 248 struct rte_crypto_sym_xform *temp_xform = xform; 249 struct cpt_sess_misc *misc; 250 vq_cmd_word3_t vq_cmd_w3; 251 void *priv = CRYPTODEV_GET_SYM_SESS_PRIV(sess); 252 int ret; 253 254 ret = sym_xform_verify(xform); 255 if (unlikely(ret)) 256 return ret; 257 258 memset(priv, 0, sizeof(struct cpt_sess_misc) + 259 offsetof(struct cpt_ctx, mc_ctx)); 260 261 misc = priv; 262 263 for ( ; xform != NULL; xform = xform->next) { 264 switch (xform->type) { 265 case RTE_CRYPTO_SYM_XFORM_AEAD: 266 ret = fill_sess_aead(xform, misc); 267 break; 268 case RTE_CRYPTO_SYM_XFORM_CIPHER: 269 ret = fill_sess_cipher(xform, misc); 270 break; 271 case RTE_CRYPTO_SYM_XFORM_AUTH: 272 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) 273 ret = fill_sess_gmac(xform, misc); 274 else 275 ret = fill_sess_auth(xform, misc); 276 break; 277 default: 278 ret = -1; 279 } 280 281 if (ret) 282 goto priv_put; 283 } 284 285 if ((GET_SESS_FC_TYPE(misc) == HASH_HMAC) && 286 cpt_mac_len_verify(&temp_xform->auth)) { 287 CPT_LOG_ERR("MAC length is not supported"); 288 struct cpt_ctx *ctx = SESS_PRIV(misc); 289 if (ctx->auth_key != NULL) { 290 rte_free(ctx->auth_key); 291 ctx->auth_key = NULL; 292 } 293 ret = -ENOTSUP; 294 goto priv_put; 295 } 296 297 misc->ctx_dma_addr = CRYPTODEV_GET_SYM_SESS_PRIV_IOVA(sess) + 298 sizeof(struct cpt_sess_misc); 299 300 vq_cmd_w3.u64 = 0; 301 vq_cmd_w3.s.grp = 0; 302 vq_cmd_w3.s.cptr = misc->ctx_dma_addr + offsetof(struct cpt_ctx, 303 mc_ctx); 304 305 misc->cpt_inst_w7 = vq_cmd_w3.u64; 306 307 return 0; 308 309 priv_put: 310 return -ENOTSUP; 311 } 312 313 static void 314 sym_session_clear(struct rte_cryptodev_sym_session *sess) 315 { 316 void *priv = CRYPTODEV_GET_SYM_SESS_PRIV(sess); 317 struct cpt_sess_misc *misc; 318 struct cpt_ctx *ctx; 319 320 if (priv == NULL) 321 return; 322 323 misc = priv; 324 ctx = SESS_PRIV(misc); 325 326 rte_free(ctx->auth_key); 327 } 328 329 static int 330 otx_cpt_session_cfg(struct rte_cryptodev *dev __rte_unused, 331 struct rte_crypto_sym_xform *xform, 332 struct rte_cryptodev_sym_session *sess) 333 { 334 CPT_PMD_INIT_FUNC_TRACE(); 335 336 return sym_session_configure(xform, sess); 337 } 338 339 340 static void 341 otx_cpt_session_clear(struct rte_cryptodev *dev __rte_unused, 342 struct rte_cryptodev_sym_session *sess) 343 { 344 CPT_PMD_INIT_FUNC_TRACE(); 345 346 return sym_session_clear(sess); 347 } 348 349 static unsigned int 350 otx_cpt_asym_session_size_get(struct rte_cryptodev *dev __rte_unused) 351 { 352 return sizeof(struct cpt_asym_sess_misc); 353 } 354 355 static int 356 otx_cpt_asym_session_cfg(struct rte_cryptodev *dev __rte_unused, 357 struct rte_crypto_asym_xform *xform __rte_unused, 358 struct rte_cryptodev_asym_session *sess) 359 { 360 struct cpt_asym_sess_misc *priv = (struct cpt_asym_sess_misc *) 361 sess->sess_private_data; 362 int ret; 363 364 CPT_PMD_INIT_FUNC_TRACE(); 365 366 ret = cpt_fill_asym_session_parameters(priv, xform); 367 if (ret) { 368 CPT_LOG_ERR("Could not configure session parameters"); 369 return ret; 370 } 371 372 priv->cpt_inst_w7 = 0; 373 374 return 0; 375 } 376 377 static void 378 otx_cpt_asym_session_clear(struct rte_cryptodev *dev, 379 struct rte_cryptodev_asym_session *sess) 380 { 381 struct cpt_asym_sess_misc *priv; 382 383 CPT_PMD_INIT_FUNC_TRACE(); 384 385 priv = (struct cpt_asym_sess_misc *) sess->sess_private_data; 386 387 if (priv == NULL) 388 return; 389 390 /* Free resources allocated during session configure */ 391 cpt_free_asym_session_parameters(priv); 392 memset(priv, 0, otx_cpt_asym_session_size_get(dev)); 393 } 394 395 static __rte_always_inline void * __rte_hot 396 otx_cpt_request_enqueue(struct cpt_instance *instance, 397 void *req, uint64_t cpt_inst_w7) 398 { 399 struct cpt_request_info *user_req = (struct cpt_request_info *)req; 400 401 fill_cpt_inst(instance, req, cpt_inst_w7); 402 403 CPT_LOG_DP_DEBUG("req: %p op: %p ", req, user_req->op); 404 405 /* Fill time_out cycles */ 406 user_req->time_out = rte_get_timer_cycles() + 407 DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz(); 408 user_req->extra_time = 0; 409 410 /* Default mode of software queue */ 411 mark_cpt_inst(instance); 412 413 CPT_LOG_DP_DEBUG("Submitted NB cmd with request: %p " 414 "op: %p", user_req, user_req->op); 415 return req; 416 } 417 418 static __rte_always_inline void * __rte_hot 419 otx_cpt_enq_single_asym(struct cpt_instance *instance, 420 struct rte_crypto_op *op) 421 { 422 struct cpt_qp_meta_info *minfo = &instance->meta_info; 423 struct rte_crypto_asym_op *asym_op = op->asym; 424 struct asym_op_params params = {0}; 425 struct cpt_asym_sess_misc *sess; 426 uintptr_t *cop; 427 void *mdata; 428 void *req; 429 int ret; 430 431 if (unlikely(rte_mempool_get(minfo->pool, &mdata) < 0)) { 432 CPT_LOG_DP_ERR("Could not allocate meta buffer for request"); 433 rte_errno = ENOMEM; 434 return NULL; 435 } 436 437 sess = (struct cpt_asym_sess_misc *) 438 asym_op->session->sess_private_data; 439 440 /* Store phys_addr of the mdata to meta_buf */ 441 params.meta_buf = rte_mempool_virt2iova(mdata); 442 443 cop = mdata; 444 cop[0] = (uintptr_t)mdata; 445 cop[1] = (uintptr_t)op; 446 cop[2] = cop[3] = 0ULL; 447 448 params.req = RTE_PTR_ADD(cop, 4 * sizeof(uintptr_t)); 449 params.req->op = cop; 450 451 /* Adjust meta_buf by crypto_op data and request_info struct */ 452 params.meta_buf += (4 * sizeof(uintptr_t)) + 453 sizeof(struct cpt_request_info); 454 455 switch (sess->xfrm_type) { 456 case RTE_CRYPTO_ASYM_XFORM_MODEX: 457 ret = cpt_modex_prep(¶ms, &sess->mod_ctx); 458 if (unlikely(ret)) 459 goto req_fail; 460 break; 461 case RTE_CRYPTO_ASYM_XFORM_RSA: 462 ret = cpt_enqueue_rsa_op(op, ¶ms, sess); 463 if (unlikely(ret)) 464 goto req_fail; 465 break; 466 case RTE_CRYPTO_ASYM_XFORM_ECDSA: 467 ret = cpt_enqueue_ecdsa_op(op, ¶ms, sess, otx_fpm_iova); 468 if (unlikely(ret)) 469 goto req_fail; 470 break; 471 case RTE_CRYPTO_ASYM_XFORM_ECPM: 472 ret = cpt_ecpm_prep(&asym_op->ecpm, ¶ms, 473 sess->ec_ctx.curveid); 474 if (unlikely(ret)) 475 goto req_fail; 476 break; 477 478 default: 479 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS; 480 rte_errno = EINVAL; 481 goto req_fail; 482 } 483 484 req = otx_cpt_request_enqueue(instance, params.req, sess->cpt_inst_w7); 485 if (unlikely(req == NULL)) { 486 CPT_LOG_DP_ERR("Could not enqueue crypto req"); 487 goto req_fail; 488 } 489 490 return req; 491 492 req_fail: 493 free_op_meta(mdata, minfo->pool); 494 495 return NULL; 496 } 497 498 static __rte_always_inline void * __rte_hot 499 otx_cpt_enq_single_sym(struct cpt_instance *instance, 500 struct rte_crypto_op *op) 501 { 502 struct cpt_sess_misc *sess; 503 struct rte_crypto_sym_op *sym_op = op->sym; 504 struct cpt_request_info *prep_req; 505 void *mdata = NULL; 506 int ret = 0; 507 void *req; 508 uint64_t cpt_op; 509 510 sess = CRYPTODEV_GET_SYM_SESS_PRIV(sym_op->session); 511 cpt_op = sess->cpt_op; 512 513 if (likely(cpt_op & CPT_OP_CIPHER_MASK)) 514 ret = fill_fc_params(op, sess, &instance->meta_info, &mdata, 515 (void **)&prep_req); 516 else 517 ret = fill_digest_params(op, sess, &instance->meta_info, 518 &mdata, (void **)&prep_req); 519 520 if (unlikely(ret)) { 521 CPT_LOG_DP_ERR("prep crypto req : op %p, cpt_op 0x%x " 522 "ret 0x%x", op, (unsigned int)cpt_op, ret); 523 return NULL; 524 } 525 526 /* Enqueue prepared instruction to h/w */ 527 req = otx_cpt_request_enqueue(instance, prep_req, sess->cpt_inst_w7); 528 if (unlikely(req == NULL)) 529 /* Buffer allocated for request preparation need to be freed */ 530 free_op_meta(mdata, instance->meta_info.pool); 531 532 return req; 533 } 534 535 static __rte_always_inline void * __rte_hot 536 otx_cpt_enq_single_sym_sessless(struct cpt_instance *instance, 537 struct rte_crypto_op *op) 538 { 539 struct rte_crypto_sym_op *sym_op = op->sym; 540 struct rte_cryptodev_sym_session *sess; 541 void *req; 542 int ret; 543 544 /* Create temporary session */ 545 if (rte_mempool_get(instance->sess_mp, (void **)&sess) < 0) { 546 rte_errno = ENOMEM; 547 return NULL; 548 } 549 550 ret = sym_session_configure(sym_op->xform, sess); 551 if (ret) 552 goto sess_put; 553 554 sym_op->session = sess; 555 556 /* Enqueue op with the tmp session set */ 557 req = otx_cpt_enq_single_sym(instance, op); 558 if (unlikely(req == NULL)) 559 goto sess_put; 560 561 return req; 562 563 sess_put: 564 rte_mempool_put(instance->sess_mp, sess); 565 return NULL; 566 } 567 568 #define OP_TYPE_SYM 0 569 #define OP_TYPE_ASYM 1 570 571 static __rte_always_inline void *__rte_hot 572 otx_cpt_enq_single(struct cpt_instance *inst, 573 struct rte_crypto_op *op, 574 const uint8_t op_type) 575 { 576 /* Check for the type */ 577 578 if (op_type == OP_TYPE_SYM) { 579 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) 580 return otx_cpt_enq_single_sym(inst, op); 581 else 582 return otx_cpt_enq_single_sym_sessless(inst, op); 583 } 584 585 if (op_type == OP_TYPE_ASYM) { 586 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) 587 return otx_cpt_enq_single_asym(inst, op); 588 } 589 590 /* Should not reach here */ 591 rte_errno = ENOTSUP; 592 return NULL; 593 } 594 595 static __rte_always_inline uint16_t __rte_hot 596 otx_cpt_pkt_enqueue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops, 597 const uint8_t op_type) 598 { 599 struct cpt_instance *instance = (struct cpt_instance *)qptr; 600 uint16_t count, free_slots; 601 void *req; 602 struct cpt_vf *cptvf = (struct cpt_vf *)instance; 603 struct pending_queue *pqueue = &cptvf->pqueue; 604 605 free_slots = pending_queue_free_slots(pqueue, DEFAULT_CMD_QLEN, 606 DEFAULT_CMD_QRSVD_SLOTS); 607 if (nb_ops > free_slots) 608 nb_ops = free_slots; 609 610 count = 0; 611 while (likely(count < nb_ops)) { 612 613 /* Enqueue single op */ 614 req = otx_cpt_enq_single(instance, ops[count], op_type); 615 616 if (unlikely(req == NULL)) 617 break; 618 619 pending_queue_push(pqueue, req, count, DEFAULT_CMD_QLEN); 620 count++; 621 } 622 623 if (likely(count)) { 624 pending_queue_commit(pqueue, count, DEFAULT_CMD_QLEN); 625 otx_cpt_ring_dbell(instance, count); 626 } 627 return count; 628 } 629 630 static uint16_t 631 otx_cpt_enqueue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops) 632 { 633 return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_ASYM); 634 } 635 636 static uint16_t 637 otx_cpt_enqueue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops) 638 { 639 return otx_cpt_pkt_enqueue(qptr, ops, nb_ops, OP_TYPE_SYM); 640 } 641 642 static __rte_always_inline void 643 submit_request_to_sso(struct ssows *ws, uintptr_t req, 644 struct rte_event *rsp_info) 645 { 646 uint64_t add_work; 647 648 add_work = rsp_info->flow_id | (RTE_EVENT_TYPE_CRYPTODEV << 28) | 649 (rsp_info->sub_event_type << 20) | 650 ((uint64_t)(rsp_info->sched_type) << 32); 651 652 if (!rsp_info->sched_type) 653 ssows_head_wait(ws); 654 655 rte_atomic_thread_fence(rte_memory_order_release); 656 ssovf_store_pair(add_work, req, ws->grps[rsp_info->queue_id]); 657 } 658 659 uint16_t __rte_hot 660 otx_crypto_adapter_enqueue(void *port, struct rte_crypto_op *op) 661 { 662 union rte_event_crypto_metadata *ec_mdata; 663 struct cpt_instance *instance; 664 struct cpt_request_info *req; 665 struct rte_event *rsp_info; 666 uint8_t op_type, cdev_id; 667 uint16_t qp_id; 668 669 ec_mdata = rte_cryptodev_session_event_mdata_get(op); 670 if (unlikely(ec_mdata == NULL)) { 671 rte_errno = EINVAL; 672 return 0; 673 } 674 675 cdev_id = ec_mdata->request_info.cdev_id; 676 qp_id = ec_mdata->request_info.queue_pair_id; 677 rsp_info = &ec_mdata->response_info; 678 instance = rte_cryptodevs[cdev_id].data->queue_pairs[qp_id]; 679 680 if (unlikely(!instance->ca_enabled)) { 681 rte_errno = EINVAL; 682 return 0; 683 } 684 685 op_type = op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC ? OP_TYPE_SYM : 686 OP_TYPE_ASYM; 687 req = otx_cpt_enq_single(instance, op, op_type); 688 if (unlikely(req == NULL)) 689 return 0; 690 691 otx_cpt_ring_dbell(instance, 1); 692 req->qp = instance; 693 submit_request_to_sso(port, (uintptr_t)req, rsp_info); 694 695 return 1; 696 } 697 698 static inline void 699 otx_cpt_asym_rsa_op(struct rte_crypto_op *cop, struct cpt_request_info *req, 700 struct rte_crypto_rsa_xform *rsa_ctx) 701 702 { 703 struct rte_crypto_rsa_op_param *rsa = &cop->asym->rsa; 704 705 switch (rsa->op_type) { 706 case RTE_CRYPTO_ASYM_OP_ENCRYPT: 707 rsa->cipher.length = rsa_ctx->n.length; 708 memcpy(rsa->cipher.data, req->rptr, rsa->cipher.length); 709 break; 710 case RTE_CRYPTO_ASYM_OP_DECRYPT: 711 if (rsa_ctx->padding.type == RTE_CRYPTO_RSA_PADDING_NONE) 712 rsa->message.length = rsa_ctx->n.length; 713 else { 714 /* Get length of decrypted output */ 715 rsa->message.length = rte_cpu_to_be_16 716 (*((uint16_t *)req->rptr)); 717 718 /* Offset data pointer by length fields */ 719 req->rptr += 2; 720 } 721 memcpy(rsa->message.data, req->rptr, rsa->message.length); 722 break; 723 case RTE_CRYPTO_ASYM_OP_SIGN: 724 rsa->sign.length = rsa_ctx->n.length; 725 memcpy(rsa->sign.data, req->rptr, rsa->sign.length); 726 break; 727 case RTE_CRYPTO_ASYM_OP_VERIFY: 728 if (rsa_ctx->padding.type == RTE_CRYPTO_RSA_PADDING_NONE) 729 rsa->sign.length = rsa_ctx->n.length; 730 else { 731 /* Get length of decrypted output */ 732 rsa->sign.length = rte_cpu_to_be_16 733 (*((uint16_t *)req->rptr)); 734 735 /* Offset data pointer by length fields */ 736 req->rptr += 2; 737 } 738 memcpy(rsa->sign.data, req->rptr, rsa->sign.length); 739 740 if (memcmp(rsa->sign.data, rsa->message.data, 741 rsa->message.length)) { 742 CPT_LOG_DP_ERR("RSA verification failed"); 743 cop->status = RTE_CRYPTO_OP_STATUS_ERROR; 744 } 745 break; 746 default: 747 CPT_LOG_DP_DEBUG("Invalid RSA operation type"); 748 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS; 749 break; 750 } 751 } 752 753 static __rte_always_inline void 754 otx_cpt_asym_dequeue_ecdsa_op(struct rte_crypto_ecdsa_op_param *ecdsa, 755 struct cpt_request_info *req, 756 struct cpt_asym_ec_ctx *ec) 757 758 { 759 int prime_len = ec_grp[ec->curveid].prime.length; 760 761 if (ecdsa->op_type == RTE_CRYPTO_ASYM_OP_VERIFY) 762 return; 763 764 /* Separate out sign r and s components */ 765 memcpy(ecdsa->r.data, req->rptr, prime_len); 766 memcpy(ecdsa->s.data, req->rptr + RTE_ALIGN_CEIL(prime_len, 8), 767 prime_len); 768 ecdsa->r.length = prime_len; 769 ecdsa->s.length = prime_len; 770 } 771 772 static __rte_always_inline void 773 otx_cpt_asym_dequeue_ecpm_op(struct rte_crypto_ecpm_op_param *ecpm, 774 struct cpt_request_info *req, 775 struct cpt_asym_ec_ctx *ec) 776 { 777 int prime_len = ec_grp[ec->curveid].prime.length; 778 779 memcpy(ecpm->r.x.data, req->rptr, prime_len); 780 memcpy(ecpm->r.y.data, req->rptr + RTE_ALIGN_CEIL(prime_len, 8), 781 prime_len); 782 ecpm->r.x.length = prime_len; 783 ecpm->r.y.length = prime_len; 784 } 785 786 static __rte_always_inline void __rte_hot 787 otx_cpt_asym_post_process(struct rte_crypto_op *cop, 788 struct cpt_request_info *req) 789 { 790 struct rte_crypto_asym_op *op = cop->asym; 791 struct cpt_asym_sess_misc *sess; 792 793 sess = (struct cpt_asym_sess_misc *) op->session->sess_private_data; 794 795 switch (sess->xfrm_type) { 796 case RTE_CRYPTO_ASYM_XFORM_RSA: 797 otx_cpt_asym_rsa_op(cop, req, &sess->rsa_ctx); 798 break; 799 case RTE_CRYPTO_ASYM_XFORM_MODEX: 800 op->modex.result.length = sess->mod_ctx.modulus.length; 801 memcpy(op->modex.result.data, req->rptr, 802 op->modex.result.length); 803 break; 804 case RTE_CRYPTO_ASYM_XFORM_ECDSA: 805 otx_cpt_asym_dequeue_ecdsa_op(&op->ecdsa, req, &sess->ec_ctx); 806 break; 807 case RTE_CRYPTO_ASYM_XFORM_ECPM: 808 otx_cpt_asym_dequeue_ecpm_op(&op->ecpm, req, &sess->ec_ctx); 809 break; 810 default: 811 CPT_LOG_DP_DEBUG("Invalid crypto xform type"); 812 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS; 813 break; 814 } 815 } 816 817 static __rte_always_inline void __rte_hot 818 otx_cpt_dequeue_post_process(struct rte_crypto_op *cop, uintptr_t *rsp, 819 const uint8_t op_type) 820 { 821 /* H/w has returned success */ 822 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS; 823 824 /* Perform further post processing */ 825 826 if ((op_type == OP_TYPE_SYM) && 827 (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)) { 828 /* Check if auth verify need to be completed */ 829 if (unlikely(rsp[2])) 830 compl_auth_verify(cop, (uint8_t *)rsp[2], rsp[3]); 831 return; 832 } 833 834 if ((op_type == OP_TYPE_ASYM) && 835 (cop->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC)) { 836 rsp = RTE_PTR_ADD(rsp, 4 * sizeof(uintptr_t)); 837 otx_cpt_asym_post_process(cop, (struct cpt_request_info *)rsp); 838 } 839 840 return; 841 } 842 843 static inline void 844 free_sym_session_data(const struct cpt_instance *instance, 845 struct rte_crypto_op *cop) 846 { 847 void *sess_private_data_t = CRYPTODEV_GET_SYM_SESS_PRIV(cop->sym->session); 848 849 memset(sess_private_data_t, 0, cpt_get_session_size()); 850 rte_mempool_put(instance->sess_mp, cop->sym->session); 851 cop->sym->session = NULL; 852 } 853 854 static __rte_always_inline struct rte_crypto_op * 855 otx_cpt_process_response(const struct cpt_instance *instance, uintptr_t *rsp, 856 uint8_t cc, const uint8_t op_type) 857 { 858 struct rte_crypto_op *cop; 859 void *metabuf; 860 861 metabuf = (void *)rsp[0]; 862 cop = (void *)rsp[1]; 863 864 /* Check completion code */ 865 if (likely(cc == 0)) { 866 /* H/w success pkt. Post process */ 867 otx_cpt_dequeue_post_process(cop, rsp, op_type); 868 } else if (cc == ERR_GC_ICV_MISCOMPARE) { 869 /* auth data mismatch */ 870 cop->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED; 871 } else { 872 /* Error */ 873 cop->status = RTE_CRYPTO_OP_STATUS_ERROR; 874 } 875 876 if (unlikely(cop->sess_type == RTE_CRYPTO_OP_SESSIONLESS)) 877 free_sym_session_data(instance, cop); 878 free_op_meta(metabuf, instance->meta_info.pool); 879 880 return cop; 881 } 882 883 static __rte_always_inline uint16_t __rte_hot 884 otx_cpt_pkt_dequeue(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops, 885 const uint8_t op_type) 886 { 887 struct cpt_instance *instance = (struct cpt_instance *)qptr; 888 struct cpt_request_info *user_req; 889 struct cpt_vf *cptvf = (struct cpt_vf *)instance; 890 uint8_t cc[nb_ops]; 891 int i, count, pcount; 892 uint8_t ret; 893 int nb_completed; 894 struct pending_queue *pqueue = &cptvf->pqueue; 895 896 pcount = pending_queue_level(pqueue, DEFAULT_CMD_QLEN); 897 898 /* Ensure pcount isn't read before data lands */ 899 rte_atomic_thread_fence(rte_memory_order_acquire); 900 901 count = (nb_ops > pcount) ? pcount : nb_ops; 902 903 for (i = 0; i < count; i++) { 904 pending_queue_peek(pqueue, (void **) &user_req, 905 DEFAULT_CMD_QLEN, i + 1 < count); 906 907 ret = check_nb_command_id(user_req, instance); 908 909 if (unlikely(ret == ERR_REQ_PENDING)) { 910 /* Stop checking for completions */ 911 break; 912 } 913 914 /* Return completion code and op handle */ 915 cc[i] = ret; 916 ops[i] = user_req->op; 917 918 CPT_LOG_DP_DEBUG("Request %p Op %p completed with code %d", 919 user_req, user_req->op, ret); 920 921 pending_queue_pop(pqueue, DEFAULT_CMD_QLEN); 922 } 923 924 nb_completed = i; 925 926 for (i = 0; i < nb_completed; i++) { 927 if (likely((i + 1) < nb_completed)) 928 rte_prefetch0(ops[i+1]); 929 930 ops[i] = otx_cpt_process_response(instance, (void *)ops[i], 931 cc[i], op_type); 932 } 933 934 return nb_completed; 935 } 936 937 static uint16_t 938 otx_cpt_dequeue_asym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops) 939 { 940 return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_ASYM); 941 } 942 943 static uint16_t 944 otx_cpt_dequeue_sym(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops) 945 { 946 return otx_cpt_pkt_dequeue(qptr, ops, nb_ops, OP_TYPE_SYM); 947 } 948 949 uintptr_t __rte_hot 950 otx_crypto_adapter_dequeue(uintptr_t get_work1) 951 { 952 const struct cpt_instance *instance; 953 struct cpt_request_info *req; 954 struct rte_crypto_op *cop; 955 uint8_t cc, op_type; 956 uintptr_t *rsp; 957 958 req = (struct cpt_request_info *)get_work1; 959 instance = req->qp; 960 rsp = req->op; 961 cop = (void *)rsp[1]; 962 op_type = cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC ? OP_TYPE_SYM : 963 OP_TYPE_ASYM; 964 965 do { 966 cc = check_nb_command_id( 967 req, (struct cpt_instance *)(uintptr_t)instance); 968 } while (cc == ERR_REQ_PENDING); 969 970 cop = otx_cpt_process_response(instance, (void *)req->op, cc, op_type); 971 972 return (uintptr_t)(cop); 973 } 974 975 static struct rte_cryptodev_ops cptvf_ops = { 976 /* Device related operations */ 977 .dev_configure = otx_cpt_dev_config, 978 .dev_start = otx_cpt_dev_start, 979 .dev_stop = otx_cpt_dev_stop, 980 .dev_close = otx_cpt_dev_close, 981 .dev_infos_get = otx_cpt_dev_info_get, 982 983 .stats_get = NULL, 984 .stats_reset = NULL, 985 .queue_pair_setup = otx_cpt_que_pair_setup, 986 .queue_pair_release = otx_cpt_que_pair_release, 987 988 /* Crypto related operations */ 989 .sym_session_get_size = otx_cpt_get_session_size, 990 .sym_session_configure = otx_cpt_session_cfg, 991 .sym_session_clear = otx_cpt_session_clear, 992 993 .asym_session_get_size = otx_cpt_asym_session_size_get, 994 .asym_session_configure = otx_cpt_asym_session_cfg, 995 .asym_session_clear = otx_cpt_asym_session_clear, 996 }; 997 998 int 999 otx_cpt_dev_create(struct rte_cryptodev *c_dev) 1000 { 1001 struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device); 1002 struct cpt_vf *cptvf = NULL; 1003 void *reg_base; 1004 char dev_name[32]; 1005 int ret; 1006 1007 if (pdev->mem_resource[0].phys_addr == 0ULL) 1008 return -EIO; 1009 1010 /* for secondary processes, we don't initialise any further as primary 1011 * has already done this work. 1012 */ 1013 if (rte_eal_process_type() != RTE_PROC_PRIMARY) 1014 return 0; 1015 1016 cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem", 1017 sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE, 1018 rte_socket_id()); 1019 1020 if (cptvf == NULL) { 1021 CPT_LOG_ERR("Cannot allocate memory for device private data"); 1022 return -ENOMEM; 1023 } 1024 1025 snprintf(dev_name, 32, "%02x:%02x.%x", 1026 pdev->addr.bus, pdev->addr.devid, pdev->addr.function); 1027 1028 reg_base = pdev->mem_resource[0].addr; 1029 if (!reg_base) { 1030 CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name); 1031 ret = -ENODEV; 1032 goto fail; 1033 } 1034 1035 ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name); 1036 if (ret) { 1037 CPT_LOG_ERR("Failed to init cptvf %s", dev_name); 1038 ret = -EIO; 1039 goto fail; 1040 } 1041 1042 switch (cptvf->vftype) { 1043 case OTX_CPT_VF_TYPE_AE: 1044 /* Set asymmetric cpt feature flags */ 1045 c_dev->feature_flags = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO | 1046 RTE_CRYPTODEV_FF_HW_ACCELERATED | 1047 RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT; 1048 break; 1049 case OTX_CPT_VF_TYPE_SE: 1050 /* Set symmetric cpt feature flags */ 1051 c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO | 1052 RTE_CRYPTODEV_FF_HW_ACCELERATED | 1053 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING | 1054 RTE_CRYPTODEV_FF_IN_PLACE_SGL | 1055 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT | 1056 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT | 1057 RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT | 1058 RTE_CRYPTODEV_FF_SYM_SESSIONLESS | 1059 RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED; 1060 break; 1061 default: 1062 /* Feature not supported. Abort */ 1063 CPT_LOG_ERR("VF type not supported by %s", dev_name); 1064 ret = -EIO; 1065 goto deinit_dev; 1066 } 1067 1068 /* Start off timer for mailbox interrupts */ 1069 otx_cpt_periodic_alarm_start(cptvf); 1070 1071 c_dev->dev_ops = &cptvf_ops; 1072 1073 if (c_dev->feature_flags & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO) { 1074 c_dev->enqueue_burst = otx_cpt_enqueue_sym; 1075 c_dev->dequeue_burst = otx_cpt_dequeue_sym; 1076 } else { 1077 c_dev->enqueue_burst = otx_cpt_enqueue_asym; 1078 c_dev->dequeue_burst = otx_cpt_dequeue_asym; 1079 } 1080 1081 /* Save dev private data */ 1082 c_dev->data->dev_private = cptvf; 1083 1084 return 0; 1085 1086 deinit_dev: 1087 otx_cpt_deinit_device(cptvf); 1088 1089 fail: 1090 if (cptvf) { 1091 /* Free private data allocated */ 1092 rte_free(cptvf); 1093 } 1094 1095 return ret; 1096 } 1097