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

Last change on this file since 204 was 188, checked in by alain, 7 years ago

Redefine the PIC device API.

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