| [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> | 
|---|
|  | 29 | #include <soclib_nic.h> | 
|---|
|  | 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 |  | 
|---|
| [3] | 56 | // set driver specific fields in chdev descriptor and call driver init function | 
|---|
| [1] | 57 | if( impl == IMPL_NIC_SOC ) | 
|---|
|  | 58 | { | 
|---|
| [188] | 59 | nic->cmd = &soclib_nic_cmd; | 
|---|
|  | 60 | nic->isr = &soclib_nic_isr; | 
|---|
|  | 61 | soclib_nic_init( nic ); | 
|---|
| [1] | 62 | } | 
|---|
|  | 63 | else | 
|---|
|  | 64 | { | 
|---|
| [3] | 65 | assert( false , __FUNCTION__ , "undefined NIC device implementation" ); | 
|---|
| [1] | 66 | } | 
|---|
|  | 67 |  | 
|---|
| [188] | 68 | // select a core to execute the NIC server thread | 
|---|
| [3] | 69 | lid_t lid = cluster_select_local_core(); | 
|---|
| [1] | 70 |  | 
|---|
| [188] | 71 | // bind the NIC IRQ to the selected core | 
|---|
|  | 72 | // but does NOT enable it | 
|---|
|  | 73 | dev_pic_bind_irq( lid , nic ); | 
|---|
| [3] | 74 |  | 
|---|
| [1] | 75 | // create server thread | 
|---|
| [3] | 76 | thread_t * new_thread; | 
|---|
| [1] | 77 | error_t    error; | 
|---|
|  | 78 |  | 
|---|
| [3] | 79 | error = thread_kernel_create( &new_thread, | 
|---|
|  | 80 | THREAD_DEV, | 
|---|
|  | 81 | &chdev_sequencial_server, | 
|---|
| [188] | 82 | nic, | 
|---|
| [3] | 83 | lid ); | 
|---|
| [1] | 84 |  | 
|---|
| [3] | 85 | assert( (error == 0) , __FUNCTION__ , "cannot create server thread" ); | 
|---|
| [1] | 86 |  | 
|---|
| [3] | 87 | // set "server" field in chdev descriptor | 
|---|
| [188] | 88 | nic->server = new_thread; | 
|---|
| [1] | 89 |  | 
|---|
|  | 90 | // start server thread | 
|---|
| [3] | 91 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); | 
|---|
| [1] | 92 |  | 
|---|
|  | 93 | }  // end dev_nic_init() | 
|---|
|  | 94 |  | 
|---|
|  | 95 | /////////////////////////////////// | 
|---|
|  | 96 | error_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 |  | 
|---|
| [3] | 110 | // get pointer on NIC-RX chdev descriptor | 
|---|
| [1] | 111 | uint32_t   channel = thread_ptr->dev_channel; | 
|---|
| [3] | 112 | xptr_t     dev_xp  = chdev_dir.nic_rx[channel]; | 
|---|
| [1] | 113 | cxy_t      dev_cxy = GET_CXY( dev_xp ); | 
|---|
| [3] | 114 | chdev_t  * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); | 
|---|
| [1] | 115 |  | 
|---|
| [3] | 116 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined NIC chdev descriptor" ); | 
|---|
| [1] | 117 |  | 
|---|
| [3] | 118 | assert( (dev_cxy == local_cxy) , __FUNCTION__ , " chdev must be local" ); | 
|---|
| [1] | 119 |  | 
|---|
|  | 120 | // initialize command in thread descriptor | 
|---|
| [3] | 121 | thread_ptr->command.nic.dev_xp = dev_xp; | 
|---|
| [1] | 122 |  | 
|---|
|  | 123 | // call driver to test readable | 
|---|
| [3] | 124 | thread_ptr->command.nic.cmd = NIC_CMD_READABLE; | 
|---|
| [1] | 125 | dev_ptr->cmd( thread_xp ); | 
|---|
|  | 126 |  | 
|---|
|  | 127 | // check error | 
|---|
| [3] | 128 | error = thread_ptr->command.nic.error; | 
|---|
| [1] | 129 | if( error ) return error; | 
|---|
|  | 130 |  | 
|---|
|  | 131 | // block and deschedule if queue non readable | 
|---|
| [3] | 132 | if( thread_ptr->command.nic.status == false ) | 
|---|
| [1] | 133 | { | 
|---|
|  | 134 | // enable NIC-RX IRQ | 
|---|
| [188] | 135 | dev_pic_enable_irq( core->lid , dev_ptr ); | 
|---|
| [1] | 136 |  | 
|---|
| [188] | 137 | // block on THREAD_BLOCKED_IO condition and deschedule | 
|---|
| [1] | 138 | thread_block( thread_ptr , THREAD_BLOCKED_IO ); | 
|---|
|  | 139 | sched_yield(); | 
|---|
|  | 140 |  | 
|---|
| [188] | 141 | // disable NIC-RX IRQ | 
|---|
|  | 142 | dev_pic_disable_irq( core->lid , dev_ptr ); | 
|---|
| [1] | 143 | } | 
|---|
|  | 144 |  | 
|---|
|  | 145 | // call driver for actual read | 
|---|
| [3] | 146 | thread_ptr->command.nic.cmd     = NIC_CMD_READ; | 
|---|
|  | 147 | thread_ptr->command.nic.buffer  = pkd->buffer; | 
|---|
| [1] | 148 | dev_ptr->cmd( thread_xp ); | 
|---|
|  | 149 |  | 
|---|
|  | 150 | // check error | 
|---|
| [3] | 151 | error = thread_ptr->command.nic.error; | 
|---|
| [1] | 152 | if( error ) return error; | 
|---|
|  | 153 |  | 
|---|
|  | 154 | // returns packet length | 
|---|
| [3] | 155 | pkd->length = thread_ptr->command.nic.length; | 
|---|
| [1] | 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 | //////////////////////////////////// | 
|---|
|  | 166 | error_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 |  | 
|---|
| [3] | 180 | // get pointer on NIC-TX chdev descriptor | 
|---|
| [1] | 181 | uint32_t   channel = thread_ptr->dev_channel; | 
|---|
| [3] | 182 | xptr_t     dev_xp  = chdev_dir.nic_tx[channel]; | 
|---|
| [1] | 183 | cxy_t      dev_cxy = GET_CXY( dev_xp ); | 
|---|
| [3] | 184 | chdev_t  * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); | 
|---|
| [1] | 185 |  | 
|---|
| [3] | 186 | assert ( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined NIC chdev descriptor" ); | 
|---|
| [1] | 187 |  | 
|---|
| [3] | 188 | assert( (dev_cxy == local_cxy) , __FUNCTION__ , " chdev must be local" ); | 
|---|
| [1] | 189 |  | 
|---|
|  | 190 | // initialize command in thread descriptor | 
|---|
| [3] | 191 | thread_ptr->command.nic.dev_xp = dev_xp; | 
|---|
| [1] | 192 |  | 
|---|
|  | 193 | // call driver to test writable | 
|---|
| [188] | 194 | thread_ptr->command.nic.cmd = NIC_CMD_WRITABLE; | 
|---|
| [1] | 195 | dev_ptr->cmd( thread_xp ); | 
|---|
|  | 196 |  | 
|---|
|  | 197 | // check error | 
|---|
| [3] | 198 | error = thread_ptr->command.nic.error; | 
|---|
| [1] | 199 | if( error ) return error; | 
|---|
|  | 200 |  | 
|---|
|  | 201 | // block and deschedule if queue non writable | 
|---|
| [3] | 202 | if( thread_ptr->command.nic.status == false ) | 
|---|
| [1] | 203 | { | 
|---|
|  | 204 | // enable NIC-TX IRQ | 
|---|
| [188] | 205 | dev_pic_enable_irq( core->lid ,dev_ptr ); | 
|---|
| [1] | 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 | 
|---|
| [188] | 212 | dev_pic_disable_irq( core->lid , dev_ptr ); | 
|---|
| [1] | 213 | } | 
|---|
|  | 214 |  | 
|---|
|  | 215 | // call driver for actual write | 
|---|
| [3] | 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; | 
|---|
| [1] | 219 | dev_ptr->cmd( thread_xp ); | 
|---|
|  | 220 |  | 
|---|
|  | 221 | // check error | 
|---|
| [3] | 222 | error = thread_ptr->command.nic.error; | 
|---|
| [1] | 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 |  | 
|---|