eventdev: implement PMD registration functions
authorJerin Jacob <jerin.jacob@caviumnetworks.com>
Tue, 6 Dec 2016 02:24:15 +0000 (07:54 +0530)
committerJerin Jacob <jerin.jacob@caviumnetworks.com>
Tue, 4 Apr 2017 17:12:00 +0000 (19:12 +0200)
This patch adds infrastructure for registering the vdev or
the PCI based event device.

Signed-off-by: Jerin Jacob <jerin.jacob@caviumnetworks.com>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
lib/librte_eventdev/rte_eventdev.c
lib/librte_eventdev/rte_eventdev_pmd.h
lib/librte_eventdev/rte_eventdev_version.map

index b13eb00..c8f3e94 100644 (file)
@@ -126,6 +126,8 @@ rte_event_dev_info_get(uint8_t dev_id, struct rte_event_dev_info *dev_info)
        dev_info->dequeue_timeout_ns = dev->data->dev_conf.dequeue_timeout_ns;
 
        dev_info->pci_dev = dev->pci_dev;
+       if (dev->driver)
+               dev_info->driver_name = dev->driver->pci_drv.driver.name;
        return 0;
 }
 
@@ -984,3 +986,237 @@ rte_event_dev_close(uint8_t dev_id)
 
        return (*dev->dev_ops->dev_close)(dev);
 }
+
+static inline int
+rte_eventdev_data_alloc(uint8_t dev_id, struct rte_eventdev_data **data,
+               int socket_id)
+{
+       char mz_name[RTE_EVENTDEV_NAME_MAX_LEN];
+       const struct rte_memzone *mz;
+       int n;
+
+       /* Generate memzone name */
+       n = snprintf(mz_name, sizeof(mz_name), "rte_eventdev_data_%u", dev_id);
+       if (n >= (int)sizeof(mz_name))
+               return -EINVAL;
+
+       if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
+               mz = rte_memzone_reserve(mz_name,
+                               sizeof(struct rte_eventdev_data),
+                               socket_id, 0);
+       } else
+               mz = rte_memzone_lookup(mz_name);
+
+       if (mz == NULL)
+               return -ENOMEM;
+
+       *data = mz->addr;
+       if (rte_eal_process_type() == RTE_PROC_PRIMARY)
+               memset(*data, 0, sizeof(struct rte_eventdev_data));
+
+       return 0;
+}
+
+static inline uint8_t
+rte_eventdev_find_free_device_index(void)
+{
+       uint8_t dev_id;
+
+       for (dev_id = 0; dev_id < RTE_EVENT_MAX_DEVS; dev_id++) {
+               if (rte_eventdevs[dev_id].attached ==
+                               RTE_EVENTDEV_DETACHED)
+                       return dev_id;
+       }
+       return RTE_EVENT_MAX_DEVS;
+}
+
+struct rte_eventdev *
+rte_event_pmd_allocate(const char *name, int socket_id)
+{
+       struct rte_eventdev *eventdev;
+       uint8_t dev_id;
+
+       if (rte_event_pmd_get_named_dev(name) != NULL) {
+               RTE_EDEV_LOG_ERR("Event device with name %s already "
+                               "allocated!", name);
+               return NULL;
+       }
+
+       dev_id = rte_eventdev_find_free_device_index();
+       if (dev_id == RTE_EVENT_MAX_DEVS) {
+               RTE_EDEV_LOG_ERR("Reached maximum number of event devices");
+               return NULL;
+       }
+
+       eventdev = &rte_eventdevs[dev_id];
+
+       if (eventdev->data == NULL) {
+               struct rte_eventdev_data *eventdev_data = NULL;
+
+               int retval = rte_eventdev_data_alloc(dev_id, &eventdev_data,
+                               socket_id);
+
+               if (retval < 0 || eventdev_data == NULL)
+                       return NULL;
+
+               eventdev->data = eventdev_data;
+
+               snprintf(eventdev->data->name, RTE_EVENTDEV_NAME_MAX_LEN,
+                               "%s", name);
+
+               eventdev->data->dev_id = dev_id;
+               eventdev->data->socket_id = socket_id;
+               eventdev->data->dev_started = 0;
+
+               eventdev->attached = RTE_EVENTDEV_ATTACHED;
+
+               eventdev_globals.nb_devs++;
+       }
+
+       return eventdev;
+}
+
+int
+rte_event_pmd_release(struct rte_eventdev *eventdev)
+{
+       int ret;
+
+       if (eventdev == NULL)
+               return -EINVAL;
+
+       ret = rte_event_dev_close(eventdev->data->dev_id);
+       if (ret < 0)
+               return ret;
+
+       eventdev->attached = RTE_EVENTDEV_DETACHED;
+       eventdev_globals.nb_devs--;
+       eventdev->data = NULL;
+
+       return 0;
+}
+
+struct rte_eventdev *
+rte_event_pmd_vdev_init(const char *name, size_t dev_private_size,
+               int socket_id)
+{
+       struct rte_eventdev *eventdev;
+
+       /* Allocate device structure */
+       eventdev = rte_event_pmd_allocate(name, socket_id);
+       if (eventdev == NULL)
+               return NULL;
+
+       /* Allocate private device structure */
+       if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
+               eventdev->data->dev_private =
+                               rte_zmalloc_socket("eventdev device private",
+                                               dev_private_size,
+                                               RTE_CACHE_LINE_SIZE,
+                                               socket_id);
+
+               if (eventdev->data->dev_private == NULL)
+                       rte_panic("Cannot allocate memzone for private device"
+                                       " data");
+       }
+
+       return eventdev;
+}
+
+int
+rte_event_pmd_pci_probe(struct rte_pci_driver *pci_drv,
+                       struct rte_pci_device *pci_dev)
+{
+       struct rte_eventdev_driver *eventdrv;
+       struct rte_eventdev *eventdev;
+
+       char eventdev_name[RTE_EVENTDEV_NAME_MAX_LEN];
+
+       int retval;
+
+       eventdrv = (struct rte_eventdev_driver *)pci_drv;
+       if (eventdrv == NULL)
+               return -ENODEV;
+
+       rte_eal_pci_device_name(&pci_dev->addr, eventdev_name,
+                       sizeof(eventdev_name));
+
+       eventdev = rte_event_pmd_allocate(eventdev_name,
+                        pci_dev->device.numa_node);
+       if (eventdev == NULL)
+               return -ENOMEM;
+
+       if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
+               eventdev->data->dev_private =
+                               rte_zmalloc_socket(
+                                               "eventdev private structure",
+                                               eventdrv->dev_private_size,
+                                               RTE_CACHE_LINE_SIZE,
+                                               rte_socket_id());
+
+               if (eventdev->data->dev_private == NULL)
+                       rte_panic("Cannot allocate memzone for private "
+                                       "device data");
+       }
+
+       eventdev->pci_dev = pci_dev;
+       eventdev->driver = eventdrv;
+
+       /* Invoke PMD device initialization function */
+       retval = (*eventdrv->eventdev_init)(eventdev);
+       if (retval == 0)
+               return 0;
+
+       RTE_EDEV_LOG_ERR("driver %s: (vendor_id=0x%x device_id=0x%x)"
+                       " failed", pci_drv->driver.name,
+                       (unsigned int) pci_dev->id.vendor_id,
+                       (unsigned int) pci_dev->id.device_id);
+
+       if (rte_eal_process_type() == RTE_PROC_PRIMARY)
+               rte_free(eventdev->data->dev_private);
+
+       eventdev->attached = RTE_EVENTDEV_DETACHED;
+       eventdev_globals.nb_devs--;
+
+       return -ENXIO;
+}
+
+int
+rte_event_pmd_pci_remove(struct rte_pci_device *pci_dev)
+{
+       const struct rte_eventdev_driver *eventdrv;
+       struct rte_eventdev *eventdev;
+       char eventdev_name[RTE_EVENTDEV_NAME_MAX_LEN];
+       int ret;
+
+       if (pci_dev == NULL)
+               return -EINVAL;
+
+       rte_eal_pci_device_name(&pci_dev->addr, eventdev_name,
+                       sizeof(eventdev_name));
+
+       eventdev = rte_event_pmd_get_named_dev(eventdev_name);
+       if (eventdev == NULL)
+               return -ENODEV;
+
+       eventdrv = (const struct rte_eventdev_driver *)pci_dev->driver;
+       if (eventdrv == NULL)
+               return -ENODEV;
+
+       /* Invoke PMD device un-init function */
+       if (*eventdrv->eventdev_uninit) {
+               ret = (*eventdrv->eventdev_uninit)(eventdev);
+               if (ret)
+                       return ret;
+       }
+
+       /* Free event device */
+       rte_event_pmd_release(eventdev);
+
+       if (rte_eal_process_type() == RTE_PROC_PRIMARY)
+               rte_free(eventdev->data->dev_private);
+
+       eventdev->pci_dev = NULL;
+       eventdev->driver = NULL;
+
+       return 0;
+}
index e60eca9..c84c9a2 100644 (file)
@@ -92,6 +92,60 @@ extern "C" {
 #define RTE_EVENTDEV_DETACHED  (0)
 #define RTE_EVENTDEV_ATTACHED  (1)
 
+/**
+ * Initialisation function of a event driver invoked for each matching
+ * event PCI device detected during the PCI probing phase.
+ *
+ * @param dev
+ *   The dev pointer is the address of the *rte_eventdev* structure associated
+ *   with the matching device and which has been [automatically] allocated in
+ *   the *rte_event_devices* array.
+ *
+ * @return
+ *   - 0: Success, the device is properly initialised by the driver.
+ *        In particular, the driver MUST have set up the *dev_ops* pointer
+ *        of the *dev* structure.
+ *   - <0: Error code of the device initialisation failure.
+ */
+typedef int (*eventdev_init_t)(struct rte_eventdev *dev);
+
+/**
+ * Finalisation function of a driver invoked for each matching
+ * PCI device detected during the PCI closing phase.
+ *
+ * @param dev
+ *   The dev pointer is the address of the *rte_eventdev* structure associated
+ *   with the matching device and which        has been [automatically] allocated in
+ *   the *rte_event_devices* array.
+ *
+ * @return
+ *   - 0: Success, the device is properly finalised by the driver.
+ *        In particular, the driver MUST free the *dev_ops* pointer
+ *        of the *dev* structure.
+ *   - <0: Error code of the device initialisation failure.
+ */
+typedef int (*eventdev_uninit_t)(struct rte_eventdev *dev);
+
+/**
+ * The structure associated with a PMD driver.
+ *
+ * Each driver acts as a PCI driver and is represented by a generic
+ * *event_driver* structure that holds:
+ *
+ * - An *rte_pci_driver* structure (which must be the first field).
+ *
+ * - The *eventdev_init* function invoked for each matching PCI device.
+ *
+ * - The size of the private data to allocate for each matching device.
+ */
+struct rte_eventdev_driver {
+       struct rte_pci_driver pci_drv;  /**< The PMD is also a PCI driver. */
+       unsigned int dev_private_size;  /**< Size of device private data. */
+
+       eventdev_init_t eventdev_init;  /**< Device init function. */
+       eventdev_uninit_t eventdev_uninit; /**< Device uninit function. */
+};
+
 /** Global structure used for maintaining state of allocated event devices */
 struct rte_eventdev_global {
        uint8_t nb_devs;        /**< Number of devices found */
@@ -396,6 +450,63 @@ struct rte_eventdev_ops {
        /* Dump internal information */
 };
 
+/**
+ * Allocates a new eventdev slot for an event device and returns the pointer
+ * to that slot for the driver to use.
+ *
+ * @param name
+ *   Unique identifier name for each device
+ * @param socket_id
+ *   Socket to allocate resources on.
+ * @return
+ *   - Slot in the rte_dev_devices array for a new device;
+ */
+struct rte_eventdev *
+rte_event_pmd_allocate(const char *name, int socket_id);
+
+/**
+ * Release the specified eventdev device.
+ *
+ * @param eventdev
+ * The *eventdev* pointer is the address of the *rte_eventdev* structure.
+ * @return
+ *   - 0 on success, negative on error
+ */
+int
+rte_event_pmd_release(struct rte_eventdev *eventdev);
+
+/**
+ * Creates a new virtual event device and returns the pointer to that device.
+ *
+ * @param name
+ *   PMD type name
+ * @param dev_private_size
+ *   Size of event PMDs private data
+ * @param socket_id
+ *   Socket to allocate resources on.
+ *
+ * @return
+ *   - Eventdev pointer if device is successfully created.
+ *   - NULL if device cannot be created.
+ */
+struct rte_eventdev *
+rte_event_pmd_vdev_init(const char *name, size_t dev_private_size,
+               int socket_id);
+
+
+/**
+ * Wrapper for use by pci drivers as a .probe function to attach to a event
+ * interface.
+ */
+int rte_event_pmd_pci_probe(struct rte_pci_driver *pci_drv,
+                           struct rte_pci_device *pci_dev);
+
+/**
+ * Wrapper for use by pci drivers as a .remove function to detach a event
+ * interface.
+ */
+int rte_event_pmd_pci_remove(struct rte_pci_device *pci_dev);
+
 #ifdef __cplusplus
 }
 #endif
index 45072db..f4a39c4 100644 (file)
@@ -29,5 +29,11 @@ DPDK_17.05 {
 
        rte_event_dequeue_timeout_ticks;
 
+       rte_event_pmd_allocate;
+       rte_event_pmd_release;
+       rte_event_pmd_vdev_init;
+       rte_event_pmd_pci_probe;
+       rte_event_pmd_pci_remove;
+
        local: *;
 };