crypto/qat: fix session initialization
[dpdk.git] / drivers / crypto / qat / qat_crypto.c
index e075076..d92de35 100644 (file)
@@ -214,25 +214,25 @@ adf_modulo(uint32_t data, uint32_t shift);
 
 static inline int
 qat_write_hw_desc_entry(struct rte_crypto_op *op, uint8_t *out_msg,
-               struct qat_crypto_op_cookie *qat_op_cookie);
+               struct qat_crypto_op_cookie *qat_op_cookie, struct qat_qp *qp);
 
-void qat_crypto_sym_clear_session(struct rte_cryptodev *dev,
-               void *session)
+void
+qat_crypto_sym_clear_session(struct rte_cryptodev *dev,
+               struct rte_cryptodev_sym_session *sess)
 {
-       struct qat_session *sess = session;
-       phys_addr_t cd_paddr;
-
        PMD_INIT_FUNC_TRACE();
-       if (sess) {
-               if (sess->bpi_ctx) {
-                       bpi_cipher_ctx_free(sess->bpi_ctx);
-                       sess->bpi_ctx = NULL;
-               }
-               cd_paddr = sess->cd_paddr;
-               memset(sess, 0, qat_crypto_sym_get_session_private_size(dev));
-               sess->cd_paddr = cd_paddr;
-       } else
-               PMD_DRV_LOG(ERR, "NULL session");
+       uint8_t index = dev->driver_id;
+       void *sess_priv = get_session_private_data(sess, index);
+       struct qat_session *s = (struct qat_session *)sess_priv;
+
+       if (sess_priv) {
+               if (s->bpi_ctx)
+                       bpi_cipher_ctx_free(s->bpi_ctx);
+               memset(s, 0, qat_crypto_sym_get_session_private_size(dev));
+               struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
+               set_session_private_data(sess, index, NULL);
+               rte_mempool_put(sess_mp, sess_priv);
+       }
 }
 
 static int
@@ -295,11 +295,12 @@ qat_get_cipher_xform(struct rte_crypto_sym_xform *xform)
 
        return NULL;
 }
-void *
+
+int
 qat_crypto_sym_configure_session_cipher(struct rte_cryptodev *dev,
-               struct rte_crypto_sym_xform *xform, void *session_private)
+               struct rte_crypto_sym_xform *xform,
+               struct qat_session *session)
 {
-       struct qat_session *session = session_private;
        struct qat_pmd_private *internals = dev->data->dev_private;
        struct rte_crypto_cipher_xform *cipher_xform = NULL;
 
@@ -440,19 +441,47 @@ qat_crypto_sym_configure_session_cipher(struct rte_cryptodev *dev,
                                                cipher_xform->key.length))
                goto error_out;
 
-       return session;
+       return 0;
 
 error_out:
        if (session->bpi_ctx) {
                bpi_cipher_ctx_free(session->bpi_ctx);
                session->bpi_ctx = NULL;
        }
-       return NULL;
+       return -1;
 }
 
-
-void *
+int
 qat_crypto_sym_configure_session(struct rte_cryptodev *dev,
+               struct rte_crypto_sym_xform *xform,
+               struct rte_cryptodev_sym_session *sess,
+               struct rte_mempool *mempool)
+{
+       void *sess_private_data;
+
+       if (rte_mempool_get(mempool, &sess_private_data)) {
+               CDEV_LOG_ERR(
+                       "Couldn't get object from session mempool");
+               return -1;
+       }
+
+       if (qat_crypto_set_session_parameters(dev, xform, sess_private_data) != 0) {
+               PMD_DRV_LOG(ERR, "Crypto QAT PMD: failed to configure "
+                               "session parameters");
+
+               /* Return session to mempool */
+               rte_mempool_put(mempool, sess_private_data);
+               return -1;
+       }
+
+       set_session_private_data(sess, dev->driver_id,
+               sess_private_data);
+
+       return 0;
+}
+
+int
+qat_crypto_set_session_parameters(struct rte_cryptodev *dev,
                struct rte_crypto_sym_xform *xform, void *session_private)
 {
        struct qat_session *session = session_private;
@@ -460,40 +489,54 @@ qat_crypto_sym_configure_session(struct rte_cryptodev *dev,
        int qat_cmd_id;
        PMD_INIT_FUNC_TRACE();
 
+       /* Set context descriptor physical address */
+       session->cd_paddr = rte_mempool_virt2phy(NULL, session) +
+                       offsetof(struct qat_session, cd);
+
+       session->min_qat_dev_gen = QAT_GEN1;
+
        /* Get requested QAT command id */
        qat_cmd_id = qat_get_cmd_id(xform);
        if (qat_cmd_id < 0 || qat_cmd_id >= ICP_QAT_FW_LA_CMD_DELIMITER) {
                PMD_DRV_LOG(ERR, "Unsupported xform chain requested");
-               goto error_out;
+               return -1;
        }
        session->qat_cmd = (enum icp_qat_fw_la_cmd_id)qat_cmd_id;
        switch (session->qat_cmd) {
        case ICP_QAT_FW_LA_CMD_CIPHER:
-       session = qat_crypto_sym_configure_session_cipher(dev, xform, session);
+               if (qat_crypto_sym_configure_session_cipher(dev, xform, session) < 0)
+                       return -1;
                break;
        case ICP_QAT_FW_LA_CMD_AUTH:
-       session = qat_crypto_sym_configure_session_auth(dev, xform, session);
+               if (qat_crypto_sym_configure_session_auth(dev, xform, session) < 0)
+                       return -1;
                break;
        case ICP_QAT_FW_LA_CMD_CIPHER_HASH:
-               if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
-                       session = qat_crypto_sym_configure_session_aead(xform,
-                                       session);
-               else {
-                       session = qat_crypto_sym_configure_session_cipher(dev,
-                                       xform, session);
-                       session = qat_crypto_sym_configure_session_auth(dev,
-                                       xform, session);
+               if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
+                       if (qat_crypto_sym_configure_session_aead(xform,
+                                       session) < 0)
+                               return -1;
+               } else {
+                       if (qat_crypto_sym_configure_session_cipher(dev,
+                                       xform, session) < 0)
+                               return -1;
+                       if (qat_crypto_sym_configure_session_auth(dev,
+                                       xform, session) < 0)
+                               return -1;
                }
                break;
        case ICP_QAT_FW_LA_CMD_HASH_CIPHER:
-               if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
-                       session = qat_crypto_sym_configure_session_aead(xform,
-                                       session);
-               else {
-                       session = qat_crypto_sym_configure_session_auth(dev,
-                                       xform, session);
-                       session = qat_crypto_sym_configure_session_cipher(dev,
-                                       xform, session);
+               if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
+                       if (qat_crypto_sym_configure_session_aead(xform,
+                                       session) < 0)
+                               return -1;
+               } else {
+                       if (qat_crypto_sym_configure_session_auth(dev,
+                                       xform, session) < 0)
+                               return -1;
+                       if (qat_crypto_sym_configure_session_cipher(dev,
+                                       xform, session) < 0)
+                               return -1;
                }
                break;
        case ICP_QAT_FW_LA_CMD_TRNG_GET_RANDOM:
@@ -507,26 +550,21 @@ qat_crypto_sym_configure_session(struct rte_cryptodev *dev,
        case ICP_QAT_FW_LA_CMD_DELIMITER:
        PMD_DRV_LOG(ERR, "Unsupported Service %u",
                session->qat_cmd);
-               goto error_out;
+               return -1;
        default:
        PMD_DRV_LOG(ERR, "Unsupported Service %u",
                session->qat_cmd);
-               goto error_out;
+               return -1;
        }
 
-       return session;
-
-error_out:
-       return NULL;
+       return 0;
 }
 
-struct qat_session *
+int
 qat_crypto_sym_configure_session_auth(struct rte_cryptodev *dev,
                                struct rte_crypto_sym_xform *xform,
-                               struct qat_session *session_private)
+                               struct qat_session *session)
 {
-
-       struct qat_session *session = session_private;
        struct rte_crypto_auth_xform *auth_xform = NULL;
        struct qat_pmd_private *internals = dev->data->dev_private;
        auth_xform = qat_get_auth_xform(xform);
@@ -656,17 +694,16 @@ qat_crypto_sym_configure_session_auth(struct rte_cryptodev *dev,
        }
 
        session->digest_length = auth_xform->digest_length;
-       return session;
+       return 0;
 
 error_out:
-       return NULL;
+       return -1;
 }
 
-struct qat_session *
+int
 qat_crypto_sym_configure_session_aead(struct rte_crypto_sym_xform *xform,
-                               struct qat_session *session_private)
+                               struct qat_session *session)
 {
-       struct qat_session *session = session_private;
        struct rte_crypto_aead_xform *aead_xform = &xform->aead;
 
        /*
@@ -710,7 +747,7 @@ qat_crypto_sym_configure_session_aead(struct rte_crypto_sym_xform *xform,
                if (qat_alg_aead_session_create_content_desc_auth(session,
                                        aead_xform->key.data,
                                        aead_xform->key.length,
-                                       aead_xform->add_auth_data_length,
+                                       aead_xform->aad_length,
                                        aead_xform->digest_length,
                                        RTE_CRYPTO_AUTH_OP_GENERATE))
                        goto error_out;
@@ -723,7 +760,7 @@ qat_crypto_sym_configure_session_aead(struct rte_crypto_sym_xform *xform,
                if (qat_alg_aead_session_create_content_desc_auth(session,
                                        aead_xform->key.data,
                                        aead_xform->key.length,
-                                       aead_xform->add_auth_data_length,
+                                       aead_xform->aad_length,
                                        aead_xform->digest_length,
                                        RTE_CRYPTO_AUTH_OP_VERIFY))
                        goto error_out;
@@ -735,10 +772,10 @@ qat_crypto_sym_configure_session_aead(struct rte_crypto_sym_xform *xform,
        }
 
        session->digest_length = aead_xform->digest_length;
-       return session;
+       return 0;
 
 error_out:
-       return NULL;
+       return -1;
 }
 
 unsigned qat_crypto_sym_get_session_private_size(
@@ -753,7 +790,8 @@ qat_bpicipher_preprocess(struct qat_session *ctx,
 {
        uint8_t block_len = qat_cipher_get_block_size(ctx->qat_cipher_alg);
        struct rte_crypto_sym_op *sym_op = op->sym;
-       uint8_t last_block_len = sym_op->cipher.data.length % block_len;
+       uint8_t last_block_len = block_len > 0 ?
+                       sym_op->cipher.data.length % block_len : 0;
 
        if (last_block_len &&
                        ctx->qat_dir == ICP_QAT_HW_CIPHER_DECRYPT) {
@@ -807,7 +845,8 @@ qat_bpicipher_postprocess(struct qat_session *ctx,
 {
        uint8_t block_len = qat_cipher_get_block_size(ctx->qat_cipher_alg);
        struct rte_crypto_sym_op *sym_op = op->sym;
-       uint8_t last_block_len = sym_op->cipher.data.length % block_len;
+       uint8_t last_block_len = block_len > 0 ?
+                       sym_op->cipher.data.length % block_len : 0;
 
        if (last_block_len > 0 &&
                        ctx->qat_dir == ICP_QAT_HW_CIPHER_ENCRYPT) {
@@ -890,7 +929,7 @@ qat_pmd_enqueue_op_burst(void *qp, struct rte_crypto_op **ops,
 
        while (nb_ops_sent != nb_ops_possible) {
                ret = qat_write_hw_desc_entry(*cur_op, base_addr + tail,
-                               tmp_qp->op_cookies[tail / queue->msg_size]);
+                       tmp_qp->op_cookies[tail / queue->msg_size], tmp_qp);
                if (ret != 0) {
                        tmp_qp->stats.enqueue_err_count++;
                        /*
@@ -946,7 +985,10 @@ qat_pmd_dequeue_op_burst(void *qp, struct rte_crypto_op **ops,
                        rx_op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
                } else {
                        struct qat_session *sess = (struct qat_session *)
-                                               (rx_op->sym->session->_private);
+                                       get_session_private_data(
+                                       rx_op->sym->session,
+                                       cryptodev_qat_driver_id);
+
                        if (sess->bpi_ctx)
                                qat_bpicipher_postprocess(sess, rx_op);
                        rx_op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
@@ -1044,7 +1086,7 @@ set_cipher_iv(uint16_t iv_length, uint16_t iv_offset,
 
 static inline int
 qat_write_hw_desc_entry(struct rte_crypto_op *op, uint8_t *out_msg,
-               struct qat_crypto_op_cookie *qat_op_cookie)
+               struct qat_crypto_op_cookie *qat_op_cookie, struct qat_qp *qp)
 {
        int ret = 0;
        struct qat_session *ctx;
@@ -1072,12 +1114,20 @@ qat_write_hw_desc_entry(struct rte_crypto_op *op, uint8_t *out_msg,
                return -EINVAL;
        }
 
-       if (unlikely(op->sym->session->dev_type != RTE_CRYPTODEV_QAT_SYM_PMD)) {
+       ctx = (struct qat_session *)get_session_private_data(
+                       op->sym->session, cryptodev_qat_driver_id);
+
+       if (unlikely(ctx == NULL)) {
                PMD_DRV_LOG(ERR, "Session was not created for this device");
                return -EINVAL;
        }
 
-       ctx = (struct qat_session *)op->sym->session->_private;
+       if (unlikely(ctx->min_qat_dev_gen > qp->qat_dev_gen)) {
+               PMD_DRV_LOG(ERR, "Session alg not supported on this device gen");
+               op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
+               return -EINVAL;
+       }
+
        qat_req = (struct icp_qat_fw_la_bulk_req *)out_msg;
        rte_mov128((uint8_t *)qat_req, (const uint8_t *)&(ctx->fw_req));
        qat_req->comn_mid.opaque_data = (uint64_t)(uintptr_t)op;
@@ -1156,18 +1206,7 @@ qat_write_hw_desc_entry(struct rte_crypto_op *op, uint8_t *out_msg,
                        auth_ofs = op->sym->auth.data.offset >> 3;
                        auth_len = op->sym->auth.data.length >> 3;
 
-                       if (ctx->qat_hash_alg ==
-                                       ICP_QAT_HW_AUTH_ALGO_KASUMI_F9) {
-                               if (do_cipher) {
-                                       auth_len = auth_len + auth_ofs + 1 -
-                                               ICP_QAT_HW_KASUMI_BLK_SZ;
-                                       auth_ofs = ICP_QAT_HW_KASUMI_BLK_SZ;
-                               } else {
-                                       auth_len = auth_len + auth_ofs + 1;
-                                       auth_ofs = 0;
-                               }
-                       } else
-                               auth_param->u1.aad_adr =
+                       auth_param->u1.aad_adr =
                                        rte_crypto_op_ctophys_offset(op,
                                                        ctx->auth_iv.offset);
 
@@ -1179,6 +1218,21 @@ qat_write_hw_desc_entry(struct rte_crypto_op *op, uint8_t *out_msg,
                        set_cipher_iv(ctx->auth_iv.length,
                                ctx->auth_iv.offset,
                                cipher_param, op, qat_req);
+                       auth_ofs = op->sym->auth.data.offset;
+                       auth_len = op->sym->auth.data.length;
+
+                       auth_param->u1.aad_adr = 0;
+                       auth_param->u2.aad_sz = 0;
+
+                       /*
+                        * If len(iv)==12B fw computes J0
+                        */
+                       if (ctx->auth_iv.length == 12) {
+                               ICP_QAT_FW_LA_GCM_IV_LEN_FLAG_SET(
+                                       qat_req->comn_hdr.serv_specif_flags,
+                                       ICP_QAT_FW_LA_GCM_IV_LEN_12_OCTETS);
+
+                       }
                } else {
                        auth_ofs = op->sym->auth.data.offset;
                        auth_len = op->sym->auth.data.length;
@@ -1191,6 +1245,21 @@ qat_write_hw_desc_entry(struct rte_crypto_op *op, uint8_t *out_msg,
        }
 
        if (do_aead) {
+               if (ctx->qat_hash_alg ==
+                               ICP_QAT_HW_AUTH_ALGO_GALOIS_128 ||
+                               ctx->qat_hash_alg ==
+                                       ICP_QAT_HW_AUTH_ALGO_GALOIS_64) {
+                       /*
+                        * If len(iv)==12B fw computes J0
+                        */
+                       if (ctx->cipher_iv.length == 12) {
+                               ICP_QAT_FW_LA_GCM_IV_LEN_FLAG_SET(
+                                       qat_req->comn_hdr.serv_specif_flags,
+                                       ICP_QAT_FW_LA_GCM_IV_LEN_12_OCTETS);
+                       }
+
+               }
+
                cipher_len = op->sym->aead.data.length;
                cipher_ofs = op->sym->aead.data.offset;
                auth_len = op->sym->aead.data.length;
@@ -1308,30 +1377,6 @@ qat_write_hw_desc_entry(struct rte_crypto_op *op, uint8_t *out_msg,
                qat_req->comn_mid.dest_data_addr = dst_buf_start;
        }
 
-       if (ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_GALOIS_128 ||
-                       ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_GALOIS_64) {
-               if (ctx->cipher_iv.length == 12 ||
-                               ctx->auth_iv.length == 12) {
-                       /*
-                        * For GCM a 12 byte IV is allowed,
-                        * but we need to inform the f/w
-                        */
-                       ICP_QAT_FW_LA_GCM_IV_LEN_FLAG_SET(
-                               qat_req->comn_hdr.serv_specif_flags,
-                               ICP_QAT_FW_LA_GCM_IV_LEN_12_OCTETS);
-               }
-               /* GMAC */
-               if (!do_aead) {
-                       qat_req->comn_mid.dst_length =
-                               qat_req->comn_mid.src_length =
-                                       rte_pktmbuf_data_len(op->sym->m_src);
-                       auth_param->u1.aad_adr = 0;
-                       auth_param->auth_len = op->sym->auth.data.length;
-                       auth_param->auth_off = op->sym->auth.data.offset;
-                       auth_param->u2.aad_sz = 0;
-               }
-       }
-
 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_TX
        rte_hexdump(stdout, "qat_req:", qat_req,
                        sizeof(struct icp_qat_fw_la_bulk_req));
@@ -1376,17 +1421,6 @@ static inline uint32_t adf_modulo(uint32_t data, uint32_t shift)
        return data - mult;
 }
 
-void qat_crypto_sym_session_init(struct rte_mempool *mp, void *sym_sess)
-{
-       struct rte_cryptodev_sym_session *sess = sym_sess;
-       struct qat_session *s = (void *)sess->_private;
-
-       PMD_INIT_FUNC_TRACE();
-       s->cd_paddr = rte_mempool_virt2phy(mp, sess) +
-               offsetof(struct qat_session, cd) +
-               offsetof(struct rte_cryptodev_sym_session, _private);
-}
-
 int qat_dev_config(__rte_unused struct rte_cryptodev *dev,
                __rte_unused struct rte_cryptodev_config *config)
 {
@@ -1433,7 +1467,7 @@ void qat_dev_info_get(struct rte_cryptodev *dev,
                info->feature_flags = dev->feature_flags;
                info->capabilities = internals->qat_dev_capabilities;
                info->sym.max_nb_sessions = internals->max_nb_sessions;
-               info->dev_type = RTE_CRYPTODEV_QAT_SYM_PMD;
+               info->driver_id = cryptodev_qat_driver_id;
                info->pci_dev = RTE_DEV_TO_PCI(dev->device);
        }
 }