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

Last change on this file since 649 was 647, checked in by alain, 5 years ago

...miscelaneous...

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