source: trunk/kernel/devices/dev_nic.c @ 32

Last change on this file since 32 was 23, checked in by alain, 8 years ago

Introduce syscalls.

File size: 8.0 KB
Line 
1/*
2 * dev_nic.c - NIC (Network Controler) generic device API implementation.
3 *
4 * Author  Alain Greiner    (2016)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <hal_types.h>
25#include <hal_special.h>
26#include <printk.h>
27#include <chdev.h>
28#include <thread.h>
29#include <soclib_nic.h>
30#include <dev_nic.h>
31
32/////////////////////////////////////////////////////////////////////////////////////////
33// Extern global variables
34/////////////////////////////////////////////////////////////////////////////////////////
35
36extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
37
38extern chdev_pic_input_t  chdev_pic_input;   // allocated in kernel_init.c
39
40////////////////////////////////////
41void dev_nic_init( chdev_t * chdev )
42{
43    // the local ICU chdev must be initialized before the NIC chdev, because
44    // the NIC chdevs initialisation requires allocation of a WTI from local ICU
45    xptr_t  icu_xp  = chdev_dir.icu[local_cxy];
46    assert( (icu_xp != XPTR_NULL) , __FUNCTION__ , "ICU not initialised before NIC" );
47
48    // get "impl" , "channel" , "is_rx" fields from chdev descriptor
49    uint32_t  impl    = chdev->impl;
50    uint32_t  is_rx   = chdev->is_rx;
51    uint32_t  channel = chdev->channel;
52
53    // set chdev name
54    snprintf( chdev->name , 16 , "nic_%d" , chdev->channel );
55
56    // set driver specific fields in chdev descriptor and call driver init function
57    if( impl == IMPL_NIC_SOC )
58    {
59        chdev->cmd = &soclib_nic_cmd;
60        chdev->isr = &soclib_nic_isr;
61        soclib_nic_init( chdev );
62    }
63    else
64    {
65        assert( false , __FUNCTION__ , "undefined NIC device implementation" );
66    }
67
68    // get a WTI mailbox from local ICU
69    uint32_t wti_id = dev_icu_wti_alloc();
70
71    assert( (wti_id != -1) , __FUNCTION__ , "cannot allocate WTI mailbox" );
72
73    // select a core
74    lid_t lid = cluster_select_local_core();
75
76    // enable WTI IRQ and update WTI interrupt vector
77    dev_icu_enable_irq( lid , WTI_TYPE , wti_id , chdev );
78
79    // link NIC IRQ to WTI mailbox in PIC component
80    uint32_t irq_id; 
81    if( is_rx ) irq_id = chdev_pic_input.nic_rx[channel];
82    else        irq_id = chdev_pic_input.nic_tx[channel];
83    dev_pic_bind_irq( irq_id , local_cxy , wti_id );
84
85    // create server thread
86    thread_t * new_thread;
87    error_t    error;
88
89    error = thread_kernel_create( &new_thread,
90                                  THREAD_DEV,
91                                  &chdev_sequencial_server,
92                                  chdev,
93                                  lid ); 
94
95    assert( (error == 0) , __FUNCTION__ , "cannot create server thread" );
96
97    // set "server" field in chdev descriptor
98    chdev->server = new_thread;
99   
100    // start server thread
101    thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );
102
103}  // end dev_nic_init()
104
105///////////////////////////////////
106error_t dev_nic_read( pkd_t * pkd )
107{
108    error_t  error;
109
110    // get pointers on this NIC-RX kernel thread
111    thread_t * thread_ptr = CURRENT_THREAD;
112    xptr_t     thread_xp  = XPTR( local_cxy , thread_ptr );
113
114    // get local pointer on core running this kernel thead
115    core_t * core = thread_ptr->core;
116
117    nic_dmsg("\n[INFO] %s enters for NIC-RX thread on core %d in cluster %x\n", 
118                 __FUNCTION__ , core->lid , local_cxy );
119
120    // get pointer on NIC-RX chdev descriptor
121    uint32_t   channel = thread_ptr->dev_channel; 
122    xptr_t     dev_xp  = chdev_dir.nic_rx[channel];
123    cxy_t      dev_cxy = GET_CXY( dev_xp );
124    chdev_t  * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
125
126    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined NIC chdev descriptor" );
127
128    assert( (dev_cxy == local_cxy) , __FUNCTION__ , " chdev must be local" );
129
130    // initialize command in thread descriptor
131    thread_ptr->command.nic.dev_xp = dev_xp;
132
133    // call driver to test readable
134    thread_ptr->command.nic.cmd = NIC_CMD_READABLE;
135    dev_ptr->cmd( thread_xp );
136
137    // check error
138    error = thread_ptr->command.nic.error;
139    if( error ) return error;
140
141    // block and deschedule if queue non readable
142    if( thread_ptr->command.nic.status == false ) 
143    {
144        // get NIC-RX IRQ index and type
145        uint32_t   irq_type = dev_ptr->irq_type;
146        uint32_t   irq_id   = dev_ptr->irq_id;
147
148        // enable NIC-RX IRQ
149        dev_icu_enable_irq( core->lid , irq_type , irq_id , dev_ptr );
150
151        // block on THREAD_BLOCKED I/O condition and deschedule
152        thread_block( thread_ptr , THREAD_BLOCKED_IO );
153        sched_yield();
154
155        // disable NIC-RX channel IRQ
156        dev_icu_disable_irq( core->lid , irq_type , irq_id );
157    }
158
159    // call driver for actual read
160    thread_ptr->command.nic.cmd     = NIC_CMD_READ;
161    thread_ptr->command.nic.buffer  = pkd->buffer;
162    dev_ptr->cmd( thread_xp );
163
164    // check error
165    error = thread_ptr->command.nic.error;
166    if( error ) return error;
167
168    // returns packet length   
169    pkd->length = thread_ptr->command.nic.length;
170
171    nic_dmsg("\n[INFO] %s exit for NIC-RX thread on core %d in cluster %x\n", 
172             __FUNCTION__ , core->lid , local_cxy );
173
174    return 0;
175
176}   // end dev_nic_read()
177
178
179////////////////////////////////////
180error_t dev_nic_write( pkd_t * pkd )
181{
182    error_t error;
183
184    // get pointers on the NIC-TX kernel tread
185    thread_t * thread_ptr = CURRENT_THREAD;
186    xptr_t     thread_xp  = XPTR( local_cxy , thread_ptr );
187
188    // get local pointer on core running this kernel thead
189    core_t * core = thread_ptr->core;
190
191    nic_dmsg("\n[INFO] %s enters for NIC-RX thread on core %d in cluster %x\n", 
192                 __FUNCTION__ , core->lid , local_cxy );
193
194    // get pointer on NIC-TX chdev descriptor
195    uint32_t   channel = thread_ptr->dev_channel; 
196    xptr_t     dev_xp  = chdev_dir.nic_tx[channel];
197    cxy_t      dev_cxy = GET_CXY( dev_xp );
198    chdev_t  * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
199
200    assert ( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined NIC chdev descriptor" );
201
202    assert( (dev_cxy == local_cxy) , __FUNCTION__ , " chdev must be local" );
203
204    // initialize command in thread descriptor
205    thread_ptr->command.nic.dev_xp = dev_xp;
206
207    // call driver to test writable
208    thread_ptr->command.nic.cmd    = NIC_CMD_WRITABLE;
209    dev_ptr->cmd( thread_xp );
210
211    // check error
212    error = thread_ptr->command.nic.error;
213    if( error ) return error;
214
215    // block and deschedule if queue non writable
216    if( thread_ptr->command.nic.status == false ) 
217    {
218        // get NIC-TX IRQ index and type
219        uint32_t   irq_type = dev_ptr->irq_type;
220        uint32_t   irq_id   = dev_ptr->irq_id;
221
222        // enable NIC-TX IRQ
223        dev_icu_enable_irq( core->lid , irq_type , irq_id , dev_ptr );
224
225        // block on THREAD_BLOCKED I/O condition and deschedule
226        thread_block( thread_ptr , THREAD_BLOCKED_IO );
227        sched_yield();
228
229        // disable NIC-TX IRQ
230        dev_icu_disable_irq( core->lid , irq_type , irq_id );
231    }
232
233    // call driver for actual write
234    thread_ptr->command.nic.cmd    = NIC_CMD_WRITE;
235    thread_ptr->command.nic.buffer = pkd->buffer;
236    thread_ptr->command.nic.length = pkd->length;
237    dev_ptr->cmd( thread_xp );
238
239    // check error
240    error = thread_ptr->command.nic.error;
241    if( error ) return error;
242
243    nic_dmsg("\n[INFO] %s exit for NIC-TX thread on core %d in cluster %x\n", 
244             __FUNCTION__ , core->lid , local_cxy );
245
246    return 0;
247}  // end dev_nic_write()
248
249
250
Note: See TracBrowser for help on using the repository browser.