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

Last change on this file since 613 was 565, checked in by alain, 6 years ago

Complete restructuration of kernel locks.

File size: 7.6 KB
Line 
1/*
2 * dev_nic.c - NIC (Network Controler) generic device API implementation.
3 *
4 * Author  Alain Greiner    (2016,2017,2018)
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_kernel_types.h>
25#include <hal_special.h>
26#include <printk.h>
27#include <chdev.h>
28#include <thread.h>
29#include <hal_drivers.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
38//////////////////////////////////
39void dev_nic_init( chdev_t * nic )
40{
41    // the PIC chdev must be initialized before the NIC chdev, because
42    // the NIC chdev initialisation requires the routing of an external IRQ.
43    xptr_t  pic_xp  = chdev_dir.pic;
44
45    assert( (pic_xp != XPTR_NULL) , "ICU not initialised before NIC" );
46
47    // get "impl" , "channel" , "is_rx" fields from chdev descriptor
48    uint32_t  impl    = nic->impl;
49    uint32_t  channel = nic->channel;
50    bool_t    is_rx   = nic->is_rx;
51
52    // set chdev name
53    if( is_rx ) snprintf( nic->name , 16 , "nic%d_rx" , channel );
54    else        snprintf( nic->name , 16 , "nic%d_tx" , channel );
55
56    // call driver init function
57    hal_drivers_nic_init( nic , impl );
58
59    // select a core to execute the NIC server thread
60    lid_t lid = cluster_select_local_core();
61
62    // bind the NIC IRQ to the selected core
63    // but does NOT enable it
64    dev_pic_bind_irq( lid , nic );
65
66    // create server thread
67    thread_t * new_thread;
68    error_t    error;
69
70    error = thread_kernel_create( &new_thread,
71                                  THREAD_DEV,
72                                  &chdev_sequencial_server,
73                                  nic,
74                                  lid ); 
75
76    assert( (error == 0) , "cannot create server thread" );
77
78    // set "server" field in chdev descriptor
79    nic->server = new_thread;
80   
81    // set "chdev" field in thread descriptor
82    new_thread->chdev = nic;
83
84    // unblock server thread
85    thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );
86
87}  // end dev_nic_init()
88
89///////////////////////////////////
90error_t dev_nic_read( pkd_t * pkd )
91{
92    error_t  error;
93
94    // get pointers on this NIC-RX kernel thread
95    thread_t * thread_ptr = CURRENT_THREAD;
96    xptr_t     thread_xp  = XPTR( local_cxy , thread_ptr );
97
98    // get local pointer on core running this kernel thead
99    core_t * core = thread_ptr->core;
100
101// check thread can yield
102assert( (thread_ptr->busylocks == 0),
103"cannot yield : busylocks = %d\n", thread_ptr->busylocks );
104
105#if DEBUG_DEV_NIC_RX
106uint32_t cycle = (uint32_t)hal_get_cycles();
107if( DEBUG_DEV_NIC_RX < cycle )
108printk("\n[DBG] %s : thread %x enters for packet %x in cluster %x\n", 
109__FUNCTION__ , thread_ptr , pkd , local_cxy );
110#endif
111
112    // get pointer on NIC-RX chdev descriptor
113    uint32_t   channel = thread_ptr->chdev->channel; 
114    xptr_t     dev_xp  = chdev_dir.nic_rx[channel];
115    cxy_t      dev_cxy = GET_CXY( dev_xp );
116    chdev_t  * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
117
118    assert( (dev_xp != XPTR_NULL) , "undefined NIC chdev descriptor" );
119
120    assert( (dev_cxy == local_cxy) , " chdev must be local" );
121
122    // initialize command in thread descriptor
123    thread_ptr->nic_cmd.dev_xp = dev_xp;
124
125    // call driver to test readable
126    thread_ptr->nic_cmd.cmd = NIC_CMD_READABLE;
127    dev_ptr->cmd( thread_xp );
128
129    // check error
130    error = thread_ptr->nic_cmd.error;
131    if( error ) return error;
132
133    // block and deschedule if queue non readable
134    if( thread_ptr->nic_cmd.status == false ) 
135    {
136        // enable NIC-RX IRQ
137        dev_pic_enable_irq( core->lid , dev_xp );
138
139        // block client thread on THREAD_BLOCKED_IO
140        thread_block( XPTR( local_cxy , thread_ptr ) , THREAD_BLOCKED_IO );
141
142        // deschedule client thread
143        sched_yield("client blocked on I/O");
144
145        // disable NIC-RX IRQ
146        dev_pic_disable_irq( core->lid , dev_xp );
147    }
148
149    // call driver for actual read
150    thread_ptr->nic_cmd.cmd     = NIC_CMD_READ;
151    thread_ptr->nic_cmd.buffer  = pkd->buffer;
152    dev_ptr->cmd( thread_xp );
153
154    // check error
155    error = thread_ptr->nic_cmd.error;
156    if( error ) return error;
157
158    // returns packet length   
159    pkd->length = thread_ptr->nic_cmd.length;
160
161#if DEBUG_DEV_NIC_RX
162cycle = (uint32_t)hal_get_cycles();
163if( DEBUG_DEV_NIC_RX < cycle )
164printk("\n[DBG] %s : thread %x exit for packet %x in cluster %x\n", 
165__FUNCTION__ , thread_ptr , pkd , local_cxy );
166#endif
167
168    return 0;
169
170}   // end dev_nic_read()
171
172
173////////////////////////////////////
174error_t dev_nic_write( pkd_t * pkd )
175{
176    error_t error;
177
178    // get pointers on the NIC-TX kernel tread
179    thread_t * thread_ptr = CURRENT_THREAD;
180    xptr_t     thread_xp  = XPTR( local_cxy , thread_ptr );
181
182    // get local pointer on core running this kernel thead
183    core_t * core = thread_ptr->core;
184
185// check thread can yield
186assert( (thread_ptr->busylocks == 0),
187"cannot yield : busylocks = %d\n", thread_ptr->busylocks );
188
189#if DEBUG_DEV_NIC_RX
190uint32_t cycle = (uint32_t)hal_get_cycles();
191if( DEBUG_DEV_NIC_RX < cycle )
192printk("\n[DBG] %s : thread %x enters for packet %x in cluster %x\n", 
193__FUNCTION__ , thread_ptr , pkd , local_cxy );
194#endif
195
196    // get pointer on NIC-TX chdev descriptor
197    uint32_t   channel = thread_ptr->chdev->channel; 
198    xptr_t     dev_xp  = chdev_dir.nic_tx[channel];
199    cxy_t      dev_cxy = GET_CXY( dev_xp );
200    chdev_t  * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
201
202    assert( (dev_xp != XPTR_NULL) , "undefined NIC chdev descriptor" );
203
204    assert( (dev_cxy == local_cxy) , " chdev must be local" );
205
206    // initialize command in thread descriptor
207    thread_ptr->nic_cmd.dev_xp = dev_xp;
208
209    // call driver to test writable
210    thread_ptr->nic_cmd.cmd = NIC_CMD_WRITABLE;
211    dev_ptr->cmd( thread_xp );
212
213    // check error
214    error = thread_ptr->nic_cmd.error;
215    if( error ) return error;
216
217    // block and deschedule if queue non writable
218    if( thread_ptr->nic_cmd.status == false ) 
219    {
220        // enable NIC-TX IRQ
221        dev_pic_enable_irq( core->lid ,dev_xp );
222
223        // block client thread on THREAD_BLOCKED I/O condition
224        thread_block( XPTR( local_cxy , thread_ptr ) , THREAD_BLOCKED_IO );
225
226        // deschedule client thread
227        sched_yield("client blocked on I/O");
228
229        // disable NIC-TX IRQ
230        dev_pic_disable_irq( core->lid , dev_xp );
231    }
232
233    // call driver for actual write
234    thread_ptr->nic_cmd.cmd    = NIC_CMD_WRITE;
235    thread_ptr->nic_cmd.buffer = pkd->buffer;
236    thread_ptr->nic_cmd.length = pkd->length;
237    dev_ptr->cmd( thread_xp );
238
239    // check error
240    error = thread_ptr->nic_cmd.error;
241    if( error ) return error;
242
243#if DEBUG_DEV_NIC_RX
244cycle = (uint32_t)hal_get_cycles();
245if( DEBUG_DEV_NIC_RX < cycle )
246printk("\n[DBG] %s : thread %x exit for packet %x in cluster %x\n", 
247__FUNCTION__ , thread_ptr , pkd , local_cxy );
248#endif
249
250    return 0;
251}  // end dev_nic_write()
252
253
254
Note: See TracBrowser for help on using the repository browser.