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