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