Index: trunk/kernel/devices/dev_nic.c
===================================================================
--- trunk/kernel/devices/dev_nic.c	(revision 1)
+++ trunk/kernel/devices/dev_nic.c	(revision 3)
@@ -25,5 +25,5 @@
 #include <hal_special.h>
 #include <printk.h>
-#include <device.h>
+#include <chdev.h>
 #include <thread.h>
 #include <soclib_nic.h>
@@ -34,108 +34,67 @@
 /////////////////////////////////////////////////////////////////////////////////////////
 
-extern devices_directory_t  devices_dir;         // allocated in kernel_init.c
-
-extern devices_input_irq_t  devices_input_irq;   // allocated in kernel_init.c
-
-
-//////////////////////////////////
-void dev_nic_init( xptr_t dev_xp )
+extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
+
+extern chdev_pic_input_t  chdev_pic_input;   // allocated in kernel_init.c
+
+////////////////////////////////////
+void dev_nic_init( chdev_t * chdev )
 {
-    // get device descriptor cluster and local pointer
-    cxy_t      dev_cxy = GET_CXY( dev_xp );
-    device_t * dev_ptr = (device_t *)GET_PTR( dev_xp );
-
-    // get "impl" , "channel" , "is_rx" fields from device descriptor
-    uint32_t  impl    = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->impl ) );
-    uint32_t  is_rx   = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->is_rx ) );
-    uint32_t  channel = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->channel ) );
-
-    // set driver specific fields in device descriptor
-    // and call driver init function
+    // the local ICU chdev must be initialized before the NIC chdev, because
+    // the NIC chdevs initialisation requires allocation of a WTI from local ICU
+    xptr_t  icu_xp  = chdev_dir.icu[local_cxy];
+    assert( (icu_xp != XPTR_NULL) , __FUNCTION__ , "ICU not initialised before NIC" );
+
+    // get "impl" , "channel" , "is_rx" fields from chdev descriptor
+    uint32_t  impl    = chdev->impl;
+    uint32_t  is_rx   = chdev->is_rx;
+    uint32_t  channel = chdev->channel;
+
+    // set driver specific fields in chdev descriptor and call driver init function
     if( impl == IMPL_NIC_SOC )
     {
-        hal_remote_spt( XPTR( dev_cxy , &dev_ptr->cmd ) , &soclib_nic_cmd );
-        hal_remote_spt( XPTR( dev_cxy , &dev_ptr->isr ) , &soclib_nic_isr );
-        hal_remote_memcpy( XPTR( dev_cxy , &dev_ptr->name ),
-                           XPTR( local_cxy , "NIC_SOC" ) , 16 );
-        soclib_nic_init( dev_xp );
-    }
-//    else if( impl == IMPL_NIC_I86)
-//    {
-//        hal_remote_spt( XPTR( cxy , &ptr->cmd ) , &i86_nic_cmd );
-//        hal_remote_spt( XPTR( cxy , &ptr->isr ) , &i86_nic_isr );
-//        hal_remote_memcpy( XPTR( cxy , &ptr->name ),
-//                           XPTR( local_cxy , "NIC_I86" ) , 16 );
-//        i86_nic_init( dev );
-//    }
+        chdev->cmd = &soclib_nic_cmd;
+        chdev->isr = &soclib_nic_isr;
+        soclib_nic_init( chdev );
+    }
     else
     {
-        printk("\n[PANIC] in %s : undefined NIC device implementation\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
-
-    // get a free WTI mailbox for this NIC device
-    uint32_t wti_id;
-    if( dev_cxy == local_cxy )                       // NIC device cluster is local
-    {
-        wti_id = dev_icu_wti_alloc();
-    }
-    else                                             // NIC device cluster is remote
-    {
-        rpc_icu_wti_alloc_client( dev_cxy , &wti_id ); 
-    }
-
-    if( wti_id == -1 )
-    {
-        printk("\n[PANIC] in %s : cannot allocate WTI mailbox\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
-
-    // enable WTI IRQ in remote ICU and update WTI interrupt vector
-    dev_icu_enable_irq( dev_cxy, 0 , WTI_TYPE , wti_id , dev_xp );
+        assert( false , __FUNCTION__ , "undefined NIC device implementation" );
+    }
+
+    // get a WTI mailbox from local ICU
+    uint32_t wti_id = dev_icu_wti_alloc();
+
+    assert( (wti_id != -1) , __FUNCTION__ , "cannot allocate WTI mailbox" );
+
+    // select a core
+    lid_t lid = cluster_select_local_core();
+
+    // enable WTI IRQ and update WTI interrupt vector
+    dev_icu_enable_irq( lid , WTI_TYPE , wti_id , chdev );
 
     // link NIC IRQ to WTI mailbox in PIC component 
     uint32_t irq_id; 
-    if( is_rx ) irq_id = devices_input_irq.nic_rx[channel];
-    else        irq_id = devices_input_irq.nic_tx[channel];
+    if( is_rx ) irq_id = chdev_pic_input.nic_rx[channel];
+    else        irq_id = chdev_pic_input.nic_tx[channel];
     dev_pic_bind_irq( irq_id , local_cxy , wti_id );
 
     // create server thread
-    thread_t * new_thread_ptr;
-    xptr_t     new_thread_xp;
+    thread_t * new_thread;
     error_t    error;
 
-    if( dev_cxy == local_cxy )                        // device cluster is local
-    {
-        error = thread_kernel_create( &new_thread_ptr,
-                                      THREAD_DEV,
-                                      &dev_ioc_server,
-                                      dev_ptr,
-                                      cluster_select_local_core() ); 
-
-        new_thread_xp = XPTR( local_cxy , new_thread_ptr );
-    }
-    else                                              // device cluster is remote
-    {
-        rpc_thread_kernel_create_client( dev_cxy,
-                                         THREAD_DEV,
-                                         &dev_ioc_server,
-                                         dev_ptr,
-                                         &new_thread_xp,
-                                         &error );
-
-        new_thread_ptr = (thread_t *)GET_PTR( new_thread_xp );
-    }
-    if( error )
-    {
-        printk("\n[PANIC] in %s : cannot create server thread\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
-
-    // set "server" field in device descriptor
-    hal_remote_spt( XPTR( dev_cxy , &dev_ptr->server ) , new_thread_ptr );
+    error = thread_kernel_create( &new_thread,
+                                  THREAD_DEV,
+                                  &chdev_sequencial_server,
+                                  chdev,
+                                  lid ); 
+
+    assert( (error == 0) , __FUNCTION__ , "cannot create server thread" );
+
+    // set "server" field in chdev descriptor
+    chdev->server = new_thread;
     
     // start server thread
-    thread_unblock( new_thread_xp , THREAD_BLOCKED_GLOBAL );
+    thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );
 
 }  // end dev_nic_init()
@@ -156,35 +115,27 @@
                  __FUNCTION__ , core->lid , local_cxy );
 
-    // get pointer on NIC-RX device descriptor
+    // get pointer on NIC-RX chdev descriptor
     uint32_t   channel = thread_ptr->dev_channel; 
-    xptr_t     dev_xp  = devices_dir.nic_rx[channel];
+    xptr_t     dev_xp  = chdev_dir.nic_rx[channel];
     cxy_t      dev_cxy = GET_CXY( dev_xp );
-    device_t * dev_ptr = (device_t *)GET_PTR( dev_xp );
-
-    if ( dev_xp == XPTR_NULL )
-    {
-        printk("\n[PANIC] in %s : undefined NIC device descriptor\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
-
-    if ( dev_cxy != local_cxy )
-    {
-        printk("\n[PANIC] in %s : device descriptor must be local\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
+    chdev_t  * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
+
+    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined NIC chdev descriptor" );
+
+    assert( (dev_cxy == local_cxy) , __FUNCTION__ , " chdev must be local" );
 
     // initialize command in thread descriptor
-    thread_ptr->dev.nic.dev_xp = dev_xp;
+    thread_ptr->command.nic.dev_xp = dev_xp;
 
     // call driver to test readable
-    thread_ptr->dev.nic.cmd = NIC_CMD_READABLE;
-    dev_ptr->cmd( thread_xp );
-
-    // check error
-    error = thread_ptr->dev.nic.error;
+    thread_ptr->command.nic.cmd = NIC_CMD_READABLE;
+    dev_ptr->cmd( thread_xp );
+
+    // check error
+    error = thread_ptr->command.nic.error;
     if( error ) return error;
 
     // block and deschedule if queue non readable
-    if( thread_ptr->dev.nic.status == false )  
+    if( thread_ptr->command.nic.status == false )  
     {
         // get NIC-RX IRQ index and type
@@ -193,5 +144,5 @@
 
         // enable NIC-RX IRQ 
-        dev_icu_enable_irq( local_cxy , core->lid , irq_type , irq_id , dev_xp );
+        dev_icu_enable_irq( core->lid , irq_type , irq_id , dev_ptr );
 
         // block on THREAD_BLOCKED I/O condition and deschedule
@@ -200,18 +151,18 @@
 
         // disable NIC-RX channel IRQ 
-        dev_icu_disable_irq( local_cxy , core->lid , irq_type , irq_id );
+        dev_icu_disable_irq( core->lid , irq_type , irq_id );
     }
 
     // call driver for actual read 
-    thread_ptr->dev.nic.cmd     = NIC_CMD_READ;
-    thread_ptr->dev.nic.buffer  = pkd->buffer;
-    dev_ptr->cmd( thread_xp );
-
-    // check error
-    error = thread_ptr->dev.nic.error;
+    thread_ptr->command.nic.cmd     = NIC_CMD_READ;
+    thread_ptr->command.nic.buffer  = pkd->buffer;
+    dev_ptr->cmd( thread_xp );
+
+    // check error
+    error = thread_ptr->command.nic.error;
     if( error ) return error;
 
     // returns packet length   
-    pkd->length = thread_ptr->dev.nic.length;
+    pkd->length = thread_ptr->command.nic.length;
 
     nic_dmsg("\n[INFO] %s exit for NIC-RX thread on core %d in cluster %x\n", 
@@ -238,35 +189,27 @@
                  __FUNCTION__ , core->lid , local_cxy );
 
-    // get pointer on NIC-TX device descriptor
+    // get pointer on NIC-TX chdev descriptor
     uint32_t   channel = thread_ptr->dev_channel; 
-    xptr_t     dev_xp  = devices_dir.nic_tx[channel];
+    xptr_t     dev_xp  = chdev_dir.nic_tx[channel];
     cxy_t      dev_cxy = GET_CXY( dev_xp );
-    device_t * dev_ptr = (device_t *)GET_PTR( dev_xp );
-
-    if ( dev_xp == XPTR_NULL )
-    {
-        printk("\n[PANIC] in %s : undefined NIC device descriptor\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
-
-    if ( dev_cxy != local_cxy )
-    {
-        printk("\n[PANIC] in %s : device descriptor must be local\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
+    chdev_t  * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
+
+    assert ( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined NIC chdev descriptor" );
+
+    assert( (dev_cxy == local_cxy) , __FUNCTION__ , " chdev must be local" );
 
     // initialize command in thread descriptor
-    thread_ptr->dev.nic.dev_xp = dev_xp;
+    thread_ptr->command.nic.dev_xp = dev_xp;
 
     // call driver to test writable
-    thread_ptr->dev.nic.cmd    = NIC_CMD_WRITABLE;
-    dev_ptr->cmd( thread_xp );
-
-    // check error
-    error = thread_ptr->dev.nic.error;
+    thread_ptr->command.nic.cmd    = NIC_CMD_WRITABLE;
+    dev_ptr->cmd( thread_xp );
+
+    // check error
+    error = thread_ptr->command.nic.error;
     if( error ) return error;
 
     // block and deschedule if queue non writable
-    if( thread_ptr->dev.nic.status == false )  
+    if( thread_ptr->command.nic.status == false )  
     {
         // get NIC-TX IRQ index and type
@@ -275,5 +218,5 @@
 
         // enable NIC-TX IRQ 
-        dev_icu_enable_irq( local_cxy , core->lid , irq_type , irq_id , dev_xp );
+        dev_icu_enable_irq( core->lid , irq_type , irq_id , dev_ptr );
 
         // block on THREAD_BLOCKED I/O condition and deschedule
@@ -282,15 +225,15 @@
 
         // disable NIC-TX IRQ 
-        dev_icu_disable_irq( local_cxy , core->lid , irq_type , irq_id );
+        dev_icu_disable_irq( core->lid , irq_type , irq_id );
     }
 
     // call driver for actual write
-    thread_ptr->dev.nic.cmd    = NIC_CMD_WRITE;
-    thread_ptr->dev.nic.buffer = pkd->buffer;
-    thread_ptr->dev.nic.length = pkd->length;
-    dev_ptr->cmd( thread_xp );
-
-    // check error
-    error = thread_ptr->dev.nic.error;
+    thread_ptr->command.nic.cmd    = NIC_CMD_WRITE;
+    thread_ptr->command.nic.buffer = pkd->buffer;
+    thread_ptr->command.nic.length = pkd->length;
+    dev_ptr->cmd( thread_xp );
+
+    // check error
+    error = thread_ptr->command.nic.error;
     if( error ) return error;
 
