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 <hal_drivers.h> |
---|
30 | #include <dev_nic.h> |
---|
31 | |
---|
32 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
33 | // Extern global variables |
---|
34 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
35 | |
---|
36 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
---|
37 | |
---|
38 | ////////////////////////////////// |
---|
39 | void 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 | // 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) , __FUNCTION__ , "cannot create server thread" ); |
---|
77 | |
---|
78 | // set "server" field in chdev descriptor |
---|
79 | nic->server = new_thread; |
---|
80 | |
---|
81 | // start server thread |
---|
82 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
---|
83 | |
---|
84 | } // end dev_nic_init() |
---|
85 | |
---|
86 | /////////////////////////////////// |
---|
87 | error_t dev_nic_read( pkd_t * pkd ) |
---|
88 | { |
---|
89 | error_t error; |
---|
90 | |
---|
91 | // get pointers on this NIC-RX kernel thread |
---|
92 | thread_t * thread_ptr = CURRENT_THREAD; |
---|
93 | xptr_t thread_xp = XPTR( local_cxy , thread_ptr ); |
---|
94 | |
---|
95 | // get local pointer on core running this kernel thead |
---|
96 | core_t * core = thread_ptr->core; |
---|
97 | |
---|
98 | nic_dmsg("\n[INFO] %s enters for NIC-RX thread on core %d in cluster %x\n", |
---|
99 | __FUNCTION__ , core->lid , local_cxy ); |
---|
100 | |
---|
101 | // get pointer on NIC-RX chdev descriptor |
---|
102 | uint32_t channel = thread_ptr->dev_channel; |
---|
103 | xptr_t dev_xp = chdev_dir.nic_rx[channel]; |
---|
104 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
105 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
---|
106 | |
---|
107 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined NIC chdev descriptor" ); |
---|
108 | |
---|
109 | assert( (dev_cxy == local_cxy) , __FUNCTION__ , " chdev must be local" ); |
---|
110 | |
---|
111 | // initialize command in thread descriptor |
---|
112 | thread_ptr->nic_cmd.dev_xp = dev_xp; |
---|
113 | |
---|
114 | // call driver to test readable |
---|
115 | thread_ptr->nic_cmd.cmd = NIC_CMD_READABLE; |
---|
116 | dev_ptr->cmd( thread_xp ); |
---|
117 | |
---|
118 | // check error |
---|
119 | error = thread_ptr->nic_cmd.error; |
---|
120 | if( error ) return error; |
---|
121 | |
---|
122 | // block and deschedule if queue non readable |
---|
123 | if( thread_ptr->nic_cmd.status == false ) |
---|
124 | { |
---|
125 | // enable NIC-RX IRQ |
---|
126 | dev_pic_enable_irq( core->lid , dev_xp ); |
---|
127 | |
---|
128 | // block on THREAD_BLOCKED_IO condition and deschedule |
---|
129 | thread_block( thread_ptr , THREAD_BLOCKED_IO ); |
---|
130 | sched_yield(); |
---|
131 | |
---|
132 | // disable NIC-RX IRQ |
---|
133 | dev_pic_disable_irq( core->lid , dev_xp ); |
---|
134 | } |
---|
135 | |
---|
136 | // call driver for actual read |
---|
137 | thread_ptr->nic_cmd.cmd = NIC_CMD_READ; |
---|
138 | thread_ptr->nic_cmd.buffer = pkd->buffer; |
---|
139 | dev_ptr->cmd( thread_xp ); |
---|
140 | |
---|
141 | // check error |
---|
142 | error = thread_ptr->nic_cmd.error; |
---|
143 | if( error ) return error; |
---|
144 | |
---|
145 | // returns packet length |
---|
146 | pkd->length = thread_ptr->nic_cmd.length; |
---|
147 | |
---|
148 | nic_dmsg("\n[INFO] %s exit for NIC-RX thread on core %d in cluster %x\n", |
---|
149 | __FUNCTION__ , core->lid , local_cxy ); |
---|
150 | |
---|
151 | return 0; |
---|
152 | |
---|
153 | } // end dev_nic_read() |
---|
154 | |
---|
155 | |
---|
156 | //////////////////////////////////// |
---|
157 | error_t dev_nic_write( pkd_t * pkd ) |
---|
158 | { |
---|
159 | error_t error; |
---|
160 | |
---|
161 | // get pointers on the NIC-TX kernel tread |
---|
162 | thread_t * thread_ptr = CURRENT_THREAD; |
---|
163 | xptr_t thread_xp = XPTR( local_cxy , thread_ptr ); |
---|
164 | |
---|
165 | // get local pointer on core running this kernel thead |
---|
166 | core_t * core = thread_ptr->core; |
---|
167 | |
---|
168 | nic_dmsg("\n[INFO] %s enters for NIC-RX thread on core %d in cluster %x\n", |
---|
169 | __FUNCTION__ , core->lid , local_cxy ); |
---|
170 | |
---|
171 | // get pointer on NIC-TX chdev descriptor |
---|
172 | uint32_t channel = thread_ptr->dev_channel; |
---|
173 | xptr_t dev_xp = chdev_dir.nic_tx[channel]; |
---|
174 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
175 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
---|
176 | |
---|
177 | assert ( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined NIC chdev descriptor" ); |
---|
178 | |
---|
179 | assert( (dev_cxy == local_cxy) , __FUNCTION__ , " chdev must be local" ); |
---|
180 | |
---|
181 | // initialize command in thread descriptor |
---|
182 | thread_ptr->nic_cmd.dev_xp = dev_xp; |
---|
183 | |
---|
184 | // call driver to test writable |
---|
185 | thread_ptr->nic_cmd.cmd = NIC_CMD_WRITABLE; |
---|
186 | dev_ptr->cmd( thread_xp ); |
---|
187 | |
---|
188 | // check error |
---|
189 | error = thread_ptr->nic_cmd.error; |
---|
190 | if( error ) return error; |
---|
191 | |
---|
192 | // block and deschedule if queue non writable |
---|
193 | if( thread_ptr->nic_cmd.status == false ) |
---|
194 | { |
---|
195 | // enable NIC-TX IRQ |
---|
196 | dev_pic_enable_irq( core->lid ,dev_xp ); |
---|
197 | |
---|
198 | // block on THREAD_BLOCKED I/O condition and deschedule |
---|
199 | thread_block( thread_ptr , THREAD_BLOCKED_IO ); |
---|
200 | sched_yield(); |
---|
201 | |
---|
202 | // disable NIC-TX IRQ |
---|
203 | dev_pic_disable_irq( core->lid , dev_xp ); |
---|
204 | } |
---|
205 | |
---|
206 | // call driver for actual write |
---|
207 | thread_ptr->nic_cmd.cmd = NIC_CMD_WRITE; |
---|
208 | thread_ptr->nic_cmd.buffer = pkd->buffer; |
---|
209 | thread_ptr->nic_cmd.length = pkd->length; |
---|
210 | dev_ptr->cmd( thread_xp ); |
---|
211 | |
---|
212 | // check error |
---|
213 | error = thread_ptr->nic_cmd.error; |
---|
214 | if( error ) return error; |
---|
215 | |
---|
216 | nic_dmsg("\n[INFO] %s exit for NIC-TX thread on core %d in cluster %x\n", |
---|
217 | __FUNCTION__ , core->lid , local_cxy ); |
---|
218 | |
---|
219 | return 0; |
---|
220 | } // end dev_nic_write() |
---|
221 | |
---|
222 | |
---|
223 | |
---|