drivers: export probe/remove helpers for PCI drivers
[dpdk.git] / lib / librte_cryptodev / rte_cryptodev.c
index f09f67e..caa9db8 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  *   BSD LICENSE
  *
- *   Copyright(c) 2015 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
  *
  *   Redistribution and use in source and binary forms, with or without
  *   modification, are permitted provided that the following conditions
@@ -59,7 +59,6 @@
 #include <rte_atomic.h>
 #include <rte_branch_prediction.h>
 #include <rte_common.h>
-#include <rte_ring.h>
 #include <rte_mempool.h>
 #include <rte_malloc.h>
 #include <rte_mbuf.h>
@@ -102,6 +101,123 @@ struct rte_cryptodev_callback {
        uint32_t active;                        /**< Callback is executing */
 };
 
+#define RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG               ("max_nb_queue_pairs")
+#define RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG             ("max_nb_sessions")
+#define RTE_CRYPTODEV_VDEV_SOCKET_ID                   ("socket_id")
+
+static const char *cryptodev_vdev_valid_params[] = {
+       RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
+       RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
+       RTE_CRYPTODEV_VDEV_SOCKET_ID
+};
+
+static uint8_t
+number_of_sockets(void)
+{
+       int sockets = 0;
+       int i;
+       const struct rte_memseg *ms = rte_eal_get_physmem_layout();
+
+       for (i = 0; ((i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL)); i++) {
+               if (sockets < ms[i].socket_id)
+                       sockets = ms[i].socket_id;
+       }
+
+       /* Number of sockets = maximum socket_id + 1 */
+       return ++sockets;
+}
+
+/** Parse integer from integer argument */
+static int
+parse_integer_arg(const char *key __rte_unused,
+               const char *value, void *extra_args)
+{
+       int *i = (int *) extra_args;
+
+       *i = atoi(value);
+       if (*i < 0) {
+               CDEV_LOG_ERR("Argument has to be positive.");
+               return -1;
+       }
+
+       return 0;
+}
+
+int
+rte_cryptodev_parse_vdev_init_params(struct rte_crypto_vdev_init_params *params,
+               const char *input_args)
+{
+       struct rte_kvargs *kvlist = NULL;
+       int ret = 0;
+
+       if (params == NULL)
+               return -EINVAL;
+
+       if (input_args) {
+               kvlist = rte_kvargs_parse(input_args,
+                               cryptodev_vdev_valid_params);
+               if (kvlist == NULL)
+                       return -1;
+
+               ret = rte_kvargs_process(kvlist,
+                                       RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
+                                       &parse_integer_arg,
+                                       &params->max_nb_queue_pairs);
+               if (ret < 0)
+                       goto free_kvlist;
+
+               ret = rte_kvargs_process(kvlist,
+                                       RTE_CRYPTODEV_VDEV_MAX_NB_SESS_ARG,
+                                       &parse_integer_arg,
+                                       &params->max_nb_sessions);
+               if (ret < 0)
+                       goto free_kvlist;
+
+               ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_SOCKET_ID,
+                                       &parse_integer_arg,
+                                       &params->socket_id);
+               if (ret < 0)
+                       goto free_kvlist;
+
+               if (params->socket_id >= number_of_sockets()) {
+                       CDEV_LOG_ERR("Invalid socket id specified to create "
+                               "the virtual crypto device on");
+                       goto free_kvlist;
+               }
+       }
+
+free_kvlist:
+       rte_kvargs_free(kvlist);
+       return ret;
+}
+
+const char *
+rte_cryptodev_get_feature_name(uint64_t flag)
+{
+       switch (flag) {
+       case RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO:
+               return "SYMMETRIC_CRYPTO";
+       case RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO:
+               return "ASYMMETRIC_CRYPTO";
+       case RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING:
+               return "SYM_OPERATION_CHAINING";
+       case RTE_CRYPTODEV_FF_CPU_SSE:
+               return "CPU_SSE";
+       case RTE_CRYPTODEV_FF_CPU_AVX:
+               return "CPU_AVX";
+       case RTE_CRYPTODEV_FF_CPU_AVX2:
+               return "CPU_AVX2";
+       case RTE_CRYPTODEV_FF_CPU_AESNI:
+               return "CPU_AESNI";
+       case RTE_CRYPTODEV_FF_HW_ACCELERATED:
+               return "HW_ACCELERATED";
+
+       default:
+               return NULL;
+       }
+}
+
+
 int
 rte_cryptodev_create_vdev(const char *name, const char *args)
 {
@@ -202,7 +318,7 @@ rte_cryptodev_find_free_device_index(void)
 }
 
 struct rte_cryptodev *
-rte_cryptodev_pmd_allocate(const char *name, enum pmd_type type, int socket_id)
+rte_cryptodev_pmd_allocate(const char *name, int socket_id)
 {
        struct rte_cryptodev *cryptodev;
        uint8_t dev_id;
@@ -241,7 +357,6 @@ rte_cryptodev_pmd_allocate(const char *name, enum pmd_type type, int socket_id)
                cryptodev->data->dev_started = 0;
 
                cryptodev->attached = RTE_CRYPTODEV_ATTACHED;
-               cryptodev->pmd_type = type;
 
                cryptodev_globals.nb_devs++;
        }
@@ -290,7 +405,7 @@ rte_cryptodev_pmd_virtual_dev_init(const char *name, size_t dev_private_size,
        struct rte_cryptodev *cryptodev;
 
        /* allocate device structure */
-       cryptodev = rte_cryptodev_pmd_allocate(name, PMD_VDEV, socket_id);
+       cryptodev = rte_cryptodev_pmd_allocate(name, socket_id);
        if (cryptodev == NULL)
                return NULL;
 
@@ -313,9 +428,9 @@ rte_cryptodev_pmd_virtual_dev_init(const char *name, size_t dev_private_size,
        return cryptodev;
 }
 
-static int
-rte_cryptodev_init(struct rte_pci_driver *pci_drv,
-               struct rte_pci_device *pci_dev)
+int
+rte_cryptodev_pci_probe(struct rte_pci_driver *pci_drv,
+                       struct rte_pci_device *pci_dev)
 {
        struct rte_cryptodev_driver *cryptodrv;
        struct rte_cryptodev *cryptodev;
@@ -332,8 +447,7 @@ rte_cryptodev_init(struct rte_pci_driver *pci_drv,
        rte_cryptodev_create_unique_device_name(cryptodev_name,
                        sizeof(cryptodev_name), pci_dev);
 
-       cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, PMD_PDEV,
-                       rte_socket_id());
+       cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
        if (cryptodev == NULL)
                return -ENOMEM;
 
@@ -375,8 +489,8 @@ rte_cryptodev_init(struct rte_pci_driver *pci_drv,
        return -ENXIO;
 }
 
-static int
-rte_cryptodev_uninit(struct rte_pci_device *pci_dev)
+int
+rte_cryptodev_pci_remove(struct rte_pci_device *pci_dev)
 {
        const struct rte_cryptodev_driver *cryptodrv;
        struct rte_cryptodev *cryptodev;
@@ -424,15 +538,16 @@ rte_cryptodev_pmd_driver_register(struct rte_cryptodev_driver *cryptodrv,
 {
        /* Call crypto device initialization directly if device is virtual */
        if (type == PMD_VDEV)
-               return rte_cryptodev_init((struct rte_pci_driver *)cryptodrv,
+               return rte_cryptodev_pci_probe(
+                               (struct rte_pci_driver *)cryptodrv,
                                NULL);
 
        /*
         * Register PCI driver for physical device intialisation during
         * PCI probing
         */
-       cryptodrv->pci_drv.devinit = rte_cryptodev_init;
-       cryptodrv->pci_drv.devuninit = rte_cryptodev_uninit;
+       cryptodrv->pci_drv.probe = rte_cryptodev_pci_probe;
+       cryptodrv->pci_drv.remove = rte_cryptodev_pci_remove;
 
        rte_eal_pci_register(&cryptodrv->pci_drv);
 
@@ -474,7 +589,7 @@ rte_cryptodev_queue_pairs_config(struct rte_cryptodev *dev, uint16_t nb_qpairs,
        if (nb_qpairs > (dev_info.max_nb_queue_pairs)) {
                CDEV_LOG_ERR("Invalid num queue_pairs (%u) for dev %u",
                                nb_qpairs, dev->data->dev_id);
-           return (-EINVAL);
+           return -EINVAL;
        }
 
        if (dev->data->queue_pairs == NULL) { /* first time configuration */
@@ -532,12 +647,6 @@ rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id)
 {
        struct rte_cryptodev *dev;
 
-       /*
-        * This function is only safe when called from the primary process
-        * in a multi-process setup
-        */
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
                CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
                return -EINVAL;
@@ -560,12 +669,6 @@ rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id)
 {
        struct rte_cryptodev *dev;
 
-       /*
-        * This function is only safe when called from the primary process
-        * in a multi-process setup
-        */
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
                CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
                return -EINVAL;
@@ -584,8 +687,8 @@ rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id)
 }
 
 static int
-rte_crypto_session_pool_create(struct rte_cryptodev *dev, unsigned nb_objs,
-               unsigned obj_cache_size, int socket_id);
+rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
+               unsigned nb_objs, unsigned obj_cache_size, int socket_id);
 
 int
 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
@@ -593,15 +696,9 @@ rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
        struct rte_cryptodev *dev;
        int diag;
 
-       /*
-        * This function is only safe when called from the primary process
-        * in a multi-process setup
-        */
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
                CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
-               return (-EINVAL);
+               return -EINVAL;
        }
 
        dev = &rte_crypto_devices[dev_id];
@@ -609,7 +706,7 @@ rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
        if (dev->data->dev_started) {
                CDEV_LOG_ERR(
                    "device %d must be stopped to allow configuration", dev_id);
-               return (-EBUSY);
+               return -EBUSY;
        }
 
        /* Setup new number of queue pairs and reconfigure device. */
@@ -622,8 +719,10 @@ rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config)
        }
 
        /* Setup Session mempool for device */
-       return rte_crypto_session_pool_create(dev, config->session_mp.nb_objs,
-                       config->session_mp.cache_size, config->socket_id);
+       return rte_cryptodev_sym_session_pool_create(dev,
+                       config->session_mp.nb_objs,
+                       config->session_mp.cache_size,
+                       config->socket_id);
 }
 
 
@@ -635,15 +734,9 @@ rte_cryptodev_start(uint8_t dev_id)
 
        CDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
 
-       /*
-        * This function is only safe when called from the primary process
-        * in a multi-process setup
-        */
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
                CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
-               return (-EINVAL);
+               return -EINVAL;
        }
 
        dev = &rte_crypto_devices[dev_id];
@@ -670,12 +763,6 @@ rte_cryptodev_stop(uint8_t dev_id)
 {
        struct rte_cryptodev *dev;
 
-       /*
-        * This function is only safe when called from the primary process
-        * in a multi-process setup
-        */
-       RTE_PROC_PRIMARY_OR_RET();
-
        if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
                CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
                return;
@@ -701,12 +788,6 @@ rte_cryptodev_close(uint8_t dev_id)
        struct rte_cryptodev *dev;
        int retval;
 
-       /*
-        * This function is only safe when called from the primary process
-        * in a multi-process setup
-        */
-       RTE_PROC_PRIMARY_OR_ERR_RET(-EINVAL);
-
        if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
                CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
                return -1;
@@ -747,21 +828,15 @@ rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
 {
        struct rte_cryptodev *dev;
 
-       /*
-        * This function is only safe when called from the primary process
-        * in a multi-process setup
-        */
-       RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
-
        if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
                CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
-               return (-EINVAL);
+               return -EINVAL;
        }
 
        dev = &rte_crypto_devices[dev_id];
        if (queue_pair_id >= dev->data->nb_queue_pairs) {
                CDEV_LOG_ERR("Invalid queue_pair_id=%d", queue_pair_id);
-               return (-EINVAL);
+               return -EINVAL;
        }
 
        if (dev->data->dev_started) {
@@ -784,7 +859,7 @@ rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats)
 
        if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
                CDEV_LOG_ERR("Invalid dev_id=%d", dev_id);
-               return (-ENODEV);
+               return -ENODEV;
        }
 
        if (stats == NULL) {
@@ -849,11 +924,11 @@ rte_cryptodev_callback_register(uint8_t dev_id,
        struct rte_cryptodev_callback *user_cb;
 
        if (!cb_fn)
-               return (-EINVAL);
+               return -EINVAL;
 
        if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
                CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
-               return (-EINVAL);
+               return -EINVAL;
        }
 
        dev = &rte_crypto_devices[dev_id];
@@ -880,7 +955,7 @@ rte_cryptodev_callback_register(uint8_t dev_id,
        }
 
        rte_spinlock_unlock(&rte_cryptodev_cb_lock);
-       return ((user_cb == NULL) ? -ENOMEM : 0);
+       return (user_cb == NULL) ? -ENOMEM : 0;
 }
 
 int
@@ -893,11 +968,11 @@ rte_cryptodev_callback_unregister(uint8_t dev_id,
        struct rte_cryptodev_callback *cb, *next;
 
        if (!cb_fn)
-               return (-EINVAL);
+               return -EINVAL;
 
        if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
                CDEV_LOG_ERR("Invalid dev_id=%" PRIu8, dev_id);
-               return (-EINVAL);
+               return -EINVAL;
        }
 
        dev = &rte_crypto_devices[dev_id];
@@ -953,27 +1028,27 @@ rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
 
 
 static void
-rte_crypto_session_init(struct rte_mempool *mp,
+rte_cryptodev_sym_session_init(struct rte_mempool *mp,
                void *opaque_arg,
                void *_sess,
                __rte_unused unsigned i)
 {
-       struct rte_cryptodev_session *sess = _sess;
+       struct rte_cryptodev_sym_session *sess = _sess;
        struct rte_cryptodev *dev = opaque_arg;
 
        memset(sess, 0, mp->elt_size);
 
        sess->dev_id = dev->data->dev_id;
-       sess->type = dev->dev_type;
+       sess->dev_type = dev->dev_type;
        sess->mp = mp;
 
        if (dev->dev_ops->session_initialize)
-               (*dev->dev_ops->session_initialize)(mp, sess->_private);
+               (*dev->dev_ops->session_initialize)(mp, sess);
 }
 
 static int
-rte_crypto_session_pool_create(struct rte_cryptodev *dev, unsigned nb_objs,
-               unsigned obj_cache_size, int socket_id)
+rte_cryptodev_sym_session_pool_create(struct rte_cryptodev *dev,
+               unsigned nb_objs, unsigned obj_cache_size, int socket_id)
 {
        char mp_name[RTE_CRYPTODEV_NAME_MAX_LEN];
        unsigned priv_sess_size;
@@ -993,7 +1068,7 @@ rte_crypto_session_pool_create(struct rte_cryptodev *dev, unsigned nb_objs,
                return -ENOMEM;
        }
 
-       unsigned elt_size = sizeof(struct rte_cryptodev_session) +
+       unsigned elt_size = sizeof(struct rte_cryptodev_sym_session) +
                        priv_sess_size;
 
        dev->data->session_pool = rte_mempool_lookup(mp_name);
@@ -1017,7 +1092,8 @@ rte_crypto_session_pool_create(struct rte_cryptodev *dev, unsigned nb_objs,
                                0, /* private data size */
                                NULL, /* obj initialization constructor */
                                NULL, /* obj initialization constructor arg */
-                               rte_crypto_session_init, /* obj constructor */
+                               rte_cryptodev_sym_session_init,
+                               /**< obj constructor*/
                                dev, /* obj constructor arg */
                                socket_id, /* socket id */
                                0); /* flags */
@@ -1032,11 +1108,12 @@ rte_crypto_session_pool_create(struct rte_cryptodev *dev, unsigned nb_objs,
        return 0;
 }
 
-struct rte_cryptodev_session *
-rte_cryptodev_session_create(uint8_t dev_id, struct rte_crypto_xform *xform)
+struct rte_cryptodev_sym_session *
+rte_cryptodev_sym_session_create(uint8_t dev_id,
+               struct rte_crypto_sym_xform *xform)
 {
        struct rte_cryptodev *dev;
-       struct rte_cryptodev_session *sess;
+       struct rte_cryptodev_sym_session *sess;
        void *_sess;
 
        if (!rte_cryptodev_pmd_is_valid_dev(dev_id)) {
@@ -1052,7 +1129,7 @@ rte_cryptodev_session_create(uint8_t dev_id, struct rte_crypto_xform *xform)
                return NULL;
        }
 
-       sess = (struct rte_cryptodev_session *)_sess;
+       sess = (struct rte_cryptodev_sym_session *)_sess;
 
        RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->session_configure, NULL);
        if (dev->dev_ops->session_configure(dev, xform, sess->_private) ==
@@ -1068,8 +1145,9 @@ rte_cryptodev_session_create(uint8_t dev_id, struct rte_crypto_xform *xform)
        return sess;
 }
 
-struct rte_cryptodev_session *
-rte_cryptodev_session_free(uint8_t dev_id, struct rte_cryptodev_session *sess)
+struct rte_cryptodev_sym_session *
+rte_cryptodev_sym_session_free(uint8_t dev_id,
+               struct rte_cryptodev_sym_session *sess)
 {
        struct rte_cryptodev *dev;
 
@@ -1081,7 +1159,7 @@ rte_cryptodev_session_free(uint8_t dev_id, struct rte_cryptodev_session *sess)
        dev = &rte_crypto_devices[dev_id];
 
        /* Check the session belongs to this device type */
-       if (sess->type != dev->dev_type)
+       if (sess->dev_type != dev->dev_type)
                return sess;
 
        /* Let device implementation clear session material */
@@ -1093,3 +1171,79 @@ rte_cryptodev_session_free(uint8_t dev_id, struct rte_cryptodev_session *sess)
 
        return NULL;
 }
+
+/** Initialise rte_crypto_op mempool element */
+static void
+rte_crypto_op_init(struct rte_mempool *mempool,
+               void *opaque_arg,
+               void *_op_data,
+               __rte_unused unsigned i)
+{
+       struct rte_crypto_op *op = _op_data;
+       enum rte_crypto_op_type type = *(enum rte_crypto_op_type *)opaque_arg;
+
+       memset(_op_data, 0, mempool->elt_size);
+
+       __rte_crypto_op_reset(op, type);
+
+       op->phys_addr = rte_mem_virt2phy(_op_data);
+       op->mempool = mempool;
+}
+
+
+struct rte_mempool *
+rte_crypto_op_pool_create(const char *name, enum rte_crypto_op_type type,
+               unsigned nb_elts, unsigned cache_size, uint16_t priv_size,
+               int socket_id)
+{
+       struct rte_crypto_op_pool_private *priv;
+
+       unsigned elt_size = sizeof(struct rte_crypto_op) +
+                       sizeof(struct rte_crypto_sym_op) +
+                       priv_size;
+
+       /* lookup mempool in case already allocated */
+       struct rte_mempool *mp = rte_mempool_lookup(name);
+
+       if (mp != NULL) {
+               priv = (struct rte_crypto_op_pool_private *)
+                               rte_mempool_get_priv(mp);
+
+               if (mp->elt_size != elt_size ||
+                               mp->cache_size < cache_size ||
+                               mp->size < nb_elts ||
+                               priv->priv_size <  priv_size) {
+                       mp = NULL;
+                       CDEV_LOG_ERR("Mempool %s already exists but with "
+                                       "incompatible parameters", name);
+                       return NULL;
+               }
+               return mp;
+       }
+
+       mp = rte_mempool_create(
+                       name,
+                       nb_elts,
+                       elt_size,
+                       cache_size,
+                       sizeof(struct rte_crypto_op_pool_private),
+                       NULL,
+                       NULL,
+                       rte_crypto_op_init,
+                       &type,
+                       socket_id,
+                       0);
+
+       if (mp == NULL) {
+               CDEV_LOG_ERR("Failed to create mempool %s", name);
+               return NULL;
+       }
+
+       priv = (struct rte_crypto_op_pool_private *)
+                       rte_mempool_get_priv(mp);
+
+       priv->priv_size = priv_size;
+       priv->type = type;
+
+       return mp;
+}