diff mbox

[API-NEXT,RFC,15/31] linux-generic: driver registration implementation

Message ID 1452285014-60320-16-git-send-email-christophe.milard@linaro.org
State New
Headers show

Commit Message

Christophe Milard Jan. 8, 2016, 8:29 p.m. UTC
The NIC driver table is defined here, as well as the registration function
called by each driver at init time.

Signed-off-by: Christophe Milard <christophe.milard@linaro.org>
---
 platform/linux-generic/include/odp_internal.h |  2 ++
 platform/linux-generic/odp_init.c             |  5 +++
 platform/linux-generic/odp_nic.c              | 49 +++++++++++++++++++++++++++
 3 files changed, 56 insertions(+)
diff mbox

Patch

diff --git a/platform/linux-generic/include/odp_internal.h b/platform/linux-generic/include/odp_internal.h
index 49e23d9..3aa395f 100644
--- a/platform/linux-generic/include/odp_internal.h
+++ b/platform/linux-generic/include/odp_internal.h
@@ -76,6 +76,8 @@  int odp_schedule_term_global(void);
 int odp_schedule_init_local(void);
 int odp_schedule_term_local(void);
 
+int odp_nic_init_local(void);
+
 int odp_timer_init_global(void);
 int odp_timer_disarm_all(void);
 
diff --git a/platform/linux-generic/odp_init.c b/platform/linux-generic/odp_init.c
index d93dab8..a06c99a 100644
--- a/platform/linux-generic/odp_init.c
+++ b/platform/linux-generic/odp_init.c
@@ -157,6 +157,11 @@  int odp_init_local(odp_thread_type_t thr_type)
 		return -1;
 	}
 
+	if (odp_nic_init_local()) {
+		ODP_ERR("ODP nic local init failed.\n");
+		return -1;
+	}
+
 	return 0;
 }
 
diff --git a/platform/linux-generic/odp_nic.c b/platform/linux-generic/odp_nic.c
index 0cd1c19..8a4cd43 100644
--- a/platform/linux-generic/odp_nic.c
+++ b/platform/linux-generic/odp_nic.c
@@ -7,6 +7,27 @@ 
 #include <odp/nic.h>
 #include <odp/pool.h>
 #include <odp_packet_io_internal.h>
+#include <odp/spinlock.h>
+
+#ifdef WITH_DRIVERS
+#include <driver_init.h>
+#endif
+
+odp_spinlock_t		drv_registration_mutex;
+
+/*
+ * registered drivers:
+ * Because at registration time, drivers give function pointers (whose
+ * scope is linux process) the scope of the following table is a process:
+ * This table will be shared between ODP threads when implemented as linux
+ * threads, but will be "duplicated" when ODP threads are processes
+ */
+#define MAX_DRIVERS 10
+struct {
+	uint16_t		nb_drivers; /* number of registered drivers */
+	odp_nic_driver_t	drivers[MAX_DRIVERS]; /* drivers */
+} registered_drv;
+
 
 /* operations on NIC segments pools: */
 odp_nic_sgmt_pool_t odp_nic_sgmt_pool_create(odp_pool_t pool_hdl)
@@ -161,3 +182,31 @@  void odp_nic_sgmt_set_last(odp_nic_sgmt_t segmt)
 	pkt_hdr = odp_packet_hdr((odp_packet_t)segmt);
 	packet_parse_l2(pkt_hdr);
 }
+
+/* initialisation and driver registration functions */
+
+int odp_nic_init_local(void)
+{
+#ifdef WITH_DRIVERS
+	/*
+	 * when ODP threads are linux threads,
+	 * prevent multiple or overlapping driver registration
+	 */
+	odp_spinlock_lock(&drv_registration_mutex);
+
+	if (registered_drv.nb_drivers == 0)
+		_odp_driver_init();
+	odp_spinlock_unlock(&drv_registration_mutex);
+
+	ODP_DBG("%d drivers initialised\n", registered_drv.nb_drivers);
+#endif
+	return 0;
+}
+
+void odp_nic_driver_register(odp_nic_driver_t *drv)
+{
+	if (registered_drv.nb_drivers < MAX_DRIVERS - 1)
+		registered_drv.drivers[registered_drv.nb_drivers++] = *drv;
+	else
+		ODP_ERR("Maximum number of drivers exceeded... ");
+}