net/sfc: check mbufs allocated using mempool API for Rx
[dpdk.git] / drivers / crypto / virtio / virtio_cryptodev.c
index 596a237..568b5a4 100644 (file)
@@ -15,6 +15,7 @@
 #include "virtio_cryptodev.h"
 #include "virtqueue.h"
 #include "virtio_crypto_algs.h"
+#include "virtio_crypto_capabilities.h"
 
 int virtio_crypto_logtype_init;
 int virtio_crypto_logtype_session;
@@ -29,6 +30,9 @@ static void virtio_crypto_dev_stop(struct rte_cryptodev *dev);
 static int virtio_crypto_dev_close(struct rte_cryptodev *dev);
 static void virtio_crypto_dev_info_get(struct rte_cryptodev *dev,
                struct rte_cryptodev_info *dev_info);
+static void virtio_crypto_dev_stats_get(struct rte_cryptodev *dev,
+               struct rte_cryptodev_stats *stats);
+static void virtio_crypto_dev_stats_reset(struct rte_cryptodev *dev);
 static int virtio_crypto_qp_setup(struct rte_cryptodev *dev,
                uint16_t queue_pair_id,
                const struct rte_cryptodev_qp_conf *qp_conf,
@@ -55,6 +59,11 @@ static const struct rte_pci_id pci_id_virtio_crypto_map[] = {
        { .vendor_id = 0, /* sentinel */ },
 };
 
+static const struct rte_cryptodev_capabilities virtio_capabilities[] = {
+       VIRTIO_SYM_CAPABILITIES,
+       RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
+};
+
 uint8_t cryptodev_virtio_driver_id;
 
 #define NUM_ENTRY_SYM_CREATE_SESSION 4
@@ -501,23 +510,78 @@ static struct rte_cryptodev_ops virtio_crypto_dev_ops = {
        .dev_close                       = virtio_crypto_dev_close,
        .dev_infos_get                   = virtio_crypto_dev_info_get,
 
-       .stats_get                       = NULL,
-       .stats_reset                     = NULL,
+       .stats_get                       = virtio_crypto_dev_stats_get,
+       .stats_reset                     = virtio_crypto_dev_stats_reset,
 
        .queue_pair_setup                = virtio_crypto_qp_setup,
        .queue_pair_release              = virtio_crypto_qp_release,
-       .queue_pair_start                = NULL,
-       .queue_pair_stop                 = NULL,
        .queue_pair_count                = NULL,
 
        /* Crypto related operations */
-       .session_get_size       = virtio_crypto_sym_get_session_private_size,
-       .session_configure      = virtio_crypto_sym_configure_session,
-       .session_clear          = virtio_crypto_sym_clear_session,
-       .qp_attach_session = NULL,
-       .qp_detach_session = NULL
+       .sym_session_get_size           = virtio_crypto_sym_get_session_private_size,
+       .sym_session_configure          = virtio_crypto_sym_configure_session,
+       .sym_session_clear              = virtio_crypto_sym_clear_session
 };
 
+static void
+virtio_crypto_update_stats(struct rte_cryptodev *dev,
+               struct rte_cryptodev_stats *stats)
+{
+       unsigned int i;
+       struct virtio_crypto_hw *hw = dev->data->dev_private;
+
+       PMD_INIT_FUNC_TRACE();
+
+       if (stats == NULL) {
+               VIRTIO_CRYPTO_DRV_LOG_ERR("invalid pointer");
+               return;
+       }
+
+       for (i = 0; i < hw->max_dataqueues; i++) {
+               const struct virtqueue *data_queue
+                       = dev->data->queue_pairs[i];
+               if (data_queue == NULL)
+                       continue;
+
+               stats->enqueued_count += data_queue->packets_sent_total;
+               stats->enqueue_err_count += data_queue->packets_sent_failed;
+
+               stats->dequeued_count += data_queue->packets_received_total;
+               stats->dequeue_err_count
+                       += data_queue->packets_received_failed;
+       }
+}
+
+static void
+virtio_crypto_dev_stats_get(struct rte_cryptodev *dev,
+               struct rte_cryptodev_stats *stats)
+{
+       PMD_INIT_FUNC_TRACE();
+
+       virtio_crypto_update_stats(dev, stats);
+}
+
+static void
+virtio_crypto_dev_stats_reset(struct rte_cryptodev *dev)
+{
+       unsigned int i;
+       struct virtio_crypto_hw *hw = dev->data->dev_private;
+
+       PMD_INIT_FUNC_TRACE();
+
+       for (i = 0; i < hw->max_dataqueues; i++) {
+               struct virtqueue *data_queue = dev->data->queue_pairs[i];
+               if (data_queue == NULL)
+                       continue;
+
+               data_queue->packets_sent_total = 0;
+               data_queue->packets_sent_failed = 0;
+
+               data_queue->packets_received_total = 0;
+               data_queue->packets_received_failed = 0;
+       }
+}
+
 static int
 virtio_crypto_qp_setup(struct rte_cryptodev *dev, uint16_t queue_pair_id,
                const struct rte_cryptodev_qp_conf *qp_conf,
@@ -684,6 +748,7 @@ crypto_virtio_create(const char *name, struct rte_pci_device *pci_dev,
 
        hw = cryptodev->data->dev_private;
        hw->dev_id = cryptodev->data->dev_id;
+       hw->virtio_dev_capabilities = virtio_capabilities;
 
        VIRTIO_CRYPTO_INIT_LOG_DBG("dev %d vendorID=0x%x deviceID=0x%x",
                cryptodev->data->dev_id, pci_dev->id.vendor_id,
@@ -893,7 +958,7 @@ virtio_crypto_sym_clear_session(
 
        hw = dev->data->dev_private;
        vq = hw->cvq;
-       session = (struct virtio_crypto_session *)get_session_private_data(
+       session = (struct virtio_crypto_session *)get_sym_session_private_data(
                sess, cryptodev_virtio_driver_id);
        if (session == NULL) {
                VIRTIO_CRYPTO_SESSION_LOG_ERR("Invalid session parameter");
@@ -1011,7 +1076,10 @@ virtio_crypto_sym_clear_session(
        VIRTIO_CRYPTO_SESSION_LOG_INFO("Close session %"PRIu64" successfully ",
                        session->session_id);
 
-       memset(sess, 0, sizeof(struct virtio_crypto_session));
+       memset(session, 0, sizeof(struct virtio_crypto_session));
+       struct rte_mempool *sess_mp = rte_mempool_from_obj(session);
+       set_sym_session_private_data(sess, cryptodev_virtio_driver_id, NULL);
+       rte_mempool_put(sess_mp, session);
        rte_free(malloc_virt_addr);
 }
 
@@ -1077,6 +1145,9 @@ virtio_crypto_sym_pad_cipher_param(
                struct rte_crypto_cipher_xform *cipher_xform)
 {
        switch (cipher_xform->algo) {
+       case RTE_CRYPTO_CIPHER_AES_CBC:
+               para->algo = VIRTIO_CRYPTO_CIPHER_AES_CBC;
+               break;
        default:
                VIRTIO_CRYPTO_SESSION_LOG_ERR("Crypto: Unsupported "
                                "Cipher alg %u", cipher_xform->algo);
@@ -1124,11 +1195,13 @@ virtio_crypto_sym_pad_auth_param(
        }
 
        switch (auth_xform->algo) {
+       case RTE_CRYPTO_AUTH_SHA1_HMAC:
+               *algo = VIRTIO_CRYPTO_MAC_HMAC_SHA1;
+               break;
        default:
                VIRTIO_CRYPTO_SESSION_LOG_ERR(
                        "Crypto: Undefined Hash algo %u specified",
                        auth_xform->algo);
-               *algo = VIRTIO_CRYPTO_NO_MAC;
                return -1;
        }
 
@@ -1149,6 +1222,12 @@ virtio_crypto_sym_pad_op_ctrl_req(
        /* Get cipher xform from crypto xform chain */
        cipher_xform = virtio_crypto_get_cipher_xform(xform);
        if (cipher_xform) {
+               if (cipher_xform->iv.length > VIRTIO_CRYPTO_MAX_IV_SIZE) {
+                       VIRTIO_CRYPTO_SESSION_LOG_ERR(
+                               "cipher IV size cannot be longer than %u",
+                               VIRTIO_CRYPTO_MAX_IV_SIZE);
+                       return -1;
+               }
                if (is_chainned)
                        ret = virtio_crypto_sym_pad_cipher_param(
                                &ctrl->u.sym_create_session.u.chain.para
@@ -1316,7 +1395,7 @@ virtio_crypto_sym_configure_session(
                goto error_out;
        }
 
-       set_session_private_data(sess, dev->driver_id,
+       set_sym_session_private_data(sess, dev->driver_id,
                session_private);
 
        return 0;
@@ -1335,11 +1414,11 @@ virtio_crypto_dev_info_get(struct rte_cryptodev *dev,
 
        if (info != NULL) {
                info->driver_id = cryptodev_virtio_driver_id;
-               info->pci_dev = RTE_DEV_TO_PCI(dev->device);
                info->feature_flags = dev->feature_flags;
                info->max_nb_queue_pairs = hw->max_dataqueues;
-               info->sym.max_nb_sessions =
-                       RTE_VIRTIO_CRYPTO_PMD_MAX_NB_SESSIONS;
+               /* No limit of number of sessions */
+               info->sym.max_nb_sessions = 0;
+               info->capabilities = hw->virtio_dev_capabilities;
        }
 }
 
@@ -1351,8 +1430,7 @@ crypto_virtio_pci_probe(
        struct rte_cryptodev_pmd_init_params init_params = {
                .name = "",
                .socket_id = rte_socket_id(),
-               .private_data_size = sizeof(struct virtio_crypto_hw),
-               .max_nb_sessions = RTE_VIRTIO_CRYPTO_PMD_MAX_NB_SESSIONS
+               .private_data_size = sizeof(struct virtio_crypto_hw)
        };
        char name[RTE_CRYPTODEV_NAME_MAX_LEN];
 
@@ -1400,9 +1478,7 @@ RTE_PMD_REGISTER_CRYPTO_DRIVER(virtio_crypto_drv,
        rte_virtio_crypto_driver.driver,
        cryptodev_virtio_driver_id);
 
-RTE_INIT(virtio_crypto_init_log);
-static void
-virtio_crypto_init_log(void)
+RTE_INIT(virtio_crypto_init_log)
 {
        virtio_crypto_logtype_init = rte_log_register("pmd.crypto.virtio.init");
        if (virtio_crypto_logtype_init >= 0)