[1] | 1 | /* |
---|
| 2 | * dev_nic.c - NIC (Network Controler) generic device API implementation. |
---|
| 3 | * |
---|
| 4 | * Author Alain Greiner (2016) |
---|
| 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 | |
---|
[3] | 38 | extern chdev_pic_input_t chdev_pic_input; // allocated in kernel_init.c |
---|
[1] | 39 | |
---|
[3] | 40 | //////////////////////////////////// |
---|
| 41 | void dev_nic_init( chdev_t * chdev ) |
---|
[1] | 42 | { |
---|
[3] | 43 | // the local ICU chdev must be initialized before the NIC chdev, because |
---|
| 44 | // the NIC chdevs initialisation requires allocation of a WTI from local ICU |
---|
| 45 | xptr_t icu_xp = chdev_dir.icu[local_cxy]; |
---|
| 46 | assert( (icu_xp != XPTR_NULL) , __FUNCTION__ , "ICU not initialised before NIC" ); |
---|
[1] | 47 | |
---|
[3] | 48 | // get "impl" , "channel" , "is_rx" fields from chdev descriptor |
---|
| 49 | uint32_t impl = chdev->impl; |
---|
| 50 | uint32_t is_rx = chdev->is_rx; |
---|
| 51 | uint32_t channel = chdev->channel; |
---|
[1] | 52 | |
---|
[23] | 53 | // set chdev name |
---|
| 54 | snprintf( chdev->name , 16 , "nic_%d" , chdev->channel ); |
---|
| 55 | |
---|
[3] | 56 | // set driver specific fields in chdev descriptor and call driver init function |
---|
[1] | 57 | if( impl == IMPL_NIC_SOC ) |
---|
| 58 | { |
---|
[3] | 59 | chdev->cmd = &soclib_nic_cmd; |
---|
| 60 | chdev->isr = &soclib_nic_isr; |
---|
| 61 | soclib_nic_init( chdev ); |
---|
[1] | 62 | } |
---|
| 63 | else |
---|
| 64 | { |
---|
[3] | 65 | assert( false , __FUNCTION__ , "undefined NIC device implementation" ); |
---|
[1] | 66 | } |
---|
| 67 | |
---|
[3] | 68 | // get a WTI mailbox from local ICU |
---|
| 69 | uint32_t wti_id = dev_icu_wti_alloc(); |
---|
[1] | 70 | |
---|
[3] | 71 | assert( (wti_id != -1) , __FUNCTION__ , "cannot allocate WTI mailbox" ); |
---|
[1] | 72 | |
---|
[3] | 73 | // select a core |
---|
| 74 | lid_t lid = cluster_select_local_core(); |
---|
[1] | 75 | |
---|
[3] | 76 | // enable WTI IRQ and update WTI interrupt vector |
---|
| 77 | dev_icu_enable_irq( lid , WTI_TYPE , wti_id , chdev ); |
---|
| 78 | |
---|
[1] | 79 | // link NIC IRQ to WTI mailbox in PIC component |
---|
| 80 | uint32_t irq_id; |
---|
[3] | 81 | if( is_rx ) irq_id = chdev_pic_input.nic_rx[channel]; |
---|
| 82 | else irq_id = chdev_pic_input.nic_tx[channel]; |
---|
[1] | 83 | dev_pic_bind_irq( irq_id , local_cxy , wti_id ); |
---|
| 84 | |
---|
| 85 | // create server thread |
---|
[3] | 86 | thread_t * new_thread; |
---|
[1] | 87 | error_t error; |
---|
| 88 | |
---|
[3] | 89 | error = thread_kernel_create( &new_thread, |
---|
| 90 | THREAD_DEV, |
---|
| 91 | &chdev_sequencial_server, |
---|
| 92 | chdev, |
---|
| 93 | lid ); |
---|
[1] | 94 | |
---|
[3] | 95 | assert( (error == 0) , __FUNCTION__ , "cannot create server thread" ); |
---|
[1] | 96 | |
---|
[3] | 97 | // set "server" field in chdev descriptor |
---|
| 98 | chdev->server = new_thread; |
---|
[1] | 99 | |
---|
| 100 | // start server thread |
---|
[3] | 101 | thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); |
---|
[1] | 102 | |
---|
| 103 | } // end dev_nic_init() |
---|
| 104 | |
---|
| 105 | /////////////////////////////////// |
---|
| 106 | error_t dev_nic_read( pkd_t * pkd ) |
---|
| 107 | { |
---|
| 108 | error_t error; |
---|
| 109 | |
---|
| 110 | // get pointers on this NIC-RX kernel thread |
---|
| 111 | thread_t * thread_ptr = CURRENT_THREAD; |
---|
| 112 | xptr_t thread_xp = XPTR( local_cxy , thread_ptr ); |
---|
| 113 | |
---|
| 114 | // get local pointer on core running this kernel thead |
---|
| 115 | core_t * core = thread_ptr->core; |
---|
| 116 | |
---|
| 117 | nic_dmsg("\n[INFO] %s enters for NIC-RX thread on core %d in cluster %x\n", |
---|
| 118 | __FUNCTION__ , core->lid , local_cxy ); |
---|
| 119 | |
---|
[3] | 120 | // get pointer on NIC-RX chdev descriptor |
---|
[1] | 121 | uint32_t channel = thread_ptr->dev_channel; |
---|
[3] | 122 | xptr_t dev_xp = chdev_dir.nic_rx[channel]; |
---|
[1] | 123 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
[3] | 124 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
---|
[1] | 125 | |
---|
[3] | 126 | assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined NIC chdev descriptor" ); |
---|
[1] | 127 | |
---|
[3] | 128 | assert( (dev_cxy == local_cxy) , __FUNCTION__ , " chdev must be local" ); |
---|
[1] | 129 | |
---|
| 130 | // initialize command in thread descriptor |
---|
[3] | 131 | thread_ptr->command.nic.dev_xp = dev_xp; |
---|
[1] | 132 | |
---|
| 133 | // call driver to test readable |
---|
[3] | 134 | thread_ptr->command.nic.cmd = NIC_CMD_READABLE; |
---|
[1] | 135 | dev_ptr->cmd( thread_xp ); |
---|
| 136 | |
---|
| 137 | // check error |
---|
[3] | 138 | error = thread_ptr->command.nic.error; |
---|
[1] | 139 | if( error ) return error; |
---|
| 140 | |
---|
| 141 | // block and deschedule if queue non readable |
---|
[3] | 142 | if( thread_ptr->command.nic.status == false ) |
---|
[1] | 143 | { |
---|
| 144 | // get NIC-RX IRQ index and type |
---|
| 145 | uint32_t irq_type = dev_ptr->irq_type; |
---|
| 146 | uint32_t irq_id = dev_ptr->irq_id; |
---|
| 147 | |
---|
| 148 | // enable NIC-RX IRQ |
---|
[3] | 149 | dev_icu_enable_irq( core->lid , irq_type , irq_id , dev_ptr ); |
---|
[1] | 150 | |
---|
| 151 | // block on THREAD_BLOCKED I/O condition and deschedule |
---|
| 152 | thread_block( thread_ptr , THREAD_BLOCKED_IO ); |
---|
| 153 | sched_yield(); |
---|
| 154 | |
---|
| 155 | // disable NIC-RX channel IRQ |
---|
[3] | 156 | dev_icu_disable_irq( core->lid , irq_type , irq_id ); |
---|
[1] | 157 | } |
---|
| 158 | |
---|
| 159 | // call driver for actual read |
---|
[3] | 160 | thread_ptr->command.nic.cmd = NIC_CMD_READ; |
---|
| 161 | thread_ptr->command.nic.buffer = pkd->buffer; |
---|
[1] | 162 | dev_ptr->cmd( thread_xp ); |
---|
| 163 | |
---|
| 164 | // check error |
---|
[3] | 165 | error = thread_ptr->command.nic.error; |
---|
[1] | 166 | if( error ) return error; |
---|
| 167 | |
---|
| 168 | // returns packet length |
---|
[3] | 169 | pkd->length = thread_ptr->command.nic.length; |
---|
[1] | 170 | |
---|
| 171 | nic_dmsg("\n[INFO] %s exit for NIC-RX thread on core %d in cluster %x\n", |
---|
| 172 | __FUNCTION__ , core->lid , local_cxy ); |
---|
| 173 | |
---|
| 174 | return 0; |
---|
| 175 | |
---|
| 176 | } // end dev_nic_read() |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | //////////////////////////////////// |
---|
| 180 | error_t dev_nic_write( pkd_t * pkd ) |
---|
| 181 | { |
---|
| 182 | error_t error; |
---|
| 183 | |
---|
| 184 | // get pointers on the NIC-TX kernel tread |
---|
| 185 | thread_t * thread_ptr = CURRENT_THREAD; |
---|
| 186 | xptr_t thread_xp = XPTR( local_cxy , thread_ptr ); |
---|
| 187 | |
---|
| 188 | // get local pointer on core running this kernel thead |
---|
| 189 | core_t * core = thread_ptr->core; |
---|
| 190 | |
---|
| 191 | nic_dmsg("\n[INFO] %s enters for NIC-RX thread on core %d in cluster %x\n", |
---|
| 192 | __FUNCTION__ , core->lid , local_cxy ); |
---|
| 193 | |
---|
[3] | 194 | // get pointer on NIC-TX chdev descriptor |
---|
[1] | 195 | uint32_t channel = thread_ptr->dev_channel; |
---|
[3] | 196 | xptr_t dev_xp = chdev_dir.nic_tx[channel]; |
---|
[1] | 197 | cxy_t dev_cxy = GET_CXY( dev_xp ); |
---|
[3] | 198 | chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); |
---|
[1] | 199 | |
---|
[3] | 200 | assert ( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined NIC chdev descriptor" ); |
---|
[1] | 201 | |
---|
[3] | 202 | assert( (dev_cxy == local_cxy) , __FUNCTION__ , " chdev must be local" ); |
---|
[1] | 203 | |
---|
| 204 | // initialize command in thread descriptor |
---|
[3] | 205 | thread_ptr->command.nic.dev_xp = dev_xp; |
---|
[1] | 206 | |
---|
| 207 | // call driver to test writable |
---|
[3] | 208 | thread_ptr->command.nic.cmd = NIC_CMD_WRITABLE; |
---|
[1] | 209 | dev_ptr->cmd( thread_xp ); |
---|
| 210 | |
---|
| 211 | // check error |
---|
[3] | 212 | error = thread_ptr->command.nic.error; |
---|
[1] | 213 | if( error ) return error; |
---|
| 214 | |
---|
| 215 | // block and deschedule if queue non writable |
---|
[3] | 216 | if( thread_ptr->command.nic.status == false ) |
---|
[1] | 217 | { |
---|
| 218 | // get NIC-TX IRQ index and type |
---|
| 219 | uint32_t irq_type = dev_ptr->irq_type; |
---|
| 220 | uint32_t irq_id = dev_ptr->irq_id; |
---|
| 221 | |
---|
| 222 | // enable NIC-TX IRQ |
---|
[3] | 223 | dev_icu_enable_irq( core->lid , irq_type , irq_id , dev_ptr ); |
---|
[1] | 224 | |
---|
| 225 | // block on THREAD_BLOCKED I/O condition and deschedule |
---|
| 226 | thread_block( thread_ptr , THREAD_BLOCKED_IO ); |
---|
| 227 | sched_yield(); |
---|
| 228 | |
---|
| 229 | // disable NIC-TX IRQ |
---|
[3] | 230 | dev_icu_disable_irq( core->lid , irq_type , irq_id ); |
---|
[1] | 231 | } |
---|
| 232 | |
---|
| 233 | // call driver for actual write |
---|
[3] | 234 | thread_ptr->command.nic.cmd = NIC_CMD_WRITE; |
---|
| 235 | thread_ptr->command.nic.buffer = pkd->buffer; |
---|
| 236 | thread_ptr->command.nic.length = pkd->length; |
---|
[1] | 237 | dev_ptr->cmd( thread_xp ); |
---|
| 238 | |
---|
| 239 | // check error |
---|
[3] | 240 | error = thread_ptr->command.nic.error; |
---|
[1] | 241 | if( error ) return error; |
---|
| 242 | |
---|
| 243 | nic_dmsg("\n[INFO] %s exit for NIC-TX thread on core %d in cluster %x\n", |
---|
| 244 | __FUNCTION__ , core->lid , local_cxy ); |
---|
| 245 | |
---|
| 246 | return 0; |
---|
| 247 | } // end dev_nic_write() |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | |
---|