[21580dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup dp8390
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
| 34 | * DP8390 network interface implementation.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <assert.h>
|
---|
| 38 | #include <async.h>
|
---|
| 39 | #include <ddi.h>
|
---|
| 40 | #include <errno.h>
|
---|
[c5b59ce] | 41 | #include <err.h>
|
---|
[21580dd] | 42 | #include <malloc.h>
|
---|
| 43 | #include <ipc/ipc.h>
|
---|
| 44 | #include <ipc/services.h>
|
---|
| 45 |
|
---|
[c7a8442] | 46 | #include <net/modules.h>
|
---|
[0a866eeb] | 47 | #include <packet_client.h>
|
---|
[849ed54] | 48 | #include <adt/measured_strings.h>
|
---|
[e526f08] | 49 | #include <net/device.h>
|
---|
[849ed54] | 50 | #include <nil_interface.h>
|
---|
[14f1db0] | 51 | #include <netif_interface.h>
|
---|
| 52 | #include <netif_local.h>
|
---|
[21580dd] | 53 |
|
---|
| 54 | #include "dp8390.h"
|
---|
| 55 | #include "dp8390_drv.h"
|
---|
| 56 | #include "dp8390_port.h"
|
---|
| 57 |
|
---|
| 58 | /** DP8390 module name.
|
---|
| 59 | */
|
---|
[24ab58b3] | 60 | #define NAME "dp8390"
|
---|
[21580dd] | 61 |
|
---|
| 62 | /** Returns the device from the interrupt call.
|
---|
| 63 | * @param[in] call The interrupt call.
|
---|
| 64 | */
|
---|
[aadf01e] | 65 | #define IRQ_GET_DEVICE(call) (device_id_t) IPC_GET_METHOD(*call)
|
---|
[21580dd] | 66 |
|
---|
| 67 | /** Returns the interrupt status register from the interrupt call.
|
---|
| 68 | * @param[in] call The interrupt call.
|
---|
| 69 | */
|
---|
[aadf01e] | 70 | #define IPC_GET_ISR(call) (int) IPC_GET_ARG2(*call)
|
---|
[21580dd] | 71 |
|
---|
| 72 | /** DP8390 kernel interrupt command sequence.
|
---|
| 73 | */
|
---|
| 74 | static irq_cmd_t dp8390_cmds[] = {
|
---|
[aadf01e] | 75 | { .cmd = CMD_PIO_READ_8,
|
---|
[21580dd] | 76 | .addr = NULL,
|
---|
| 77 | .dstarg = 2
|
---|
| 78 | },
|
---|
| 79 | {
|
---|
| 80 | .cmd = CMD_PREDICATE,
|
---|
| 81 | .value = 1,
|
---|
| 82 | .srcarg = 2
|
---|
| 83 | },
|
---|
| 84 | {
|
---|
| 85 | .cmd = CMD_ACCEPT
|
---|
| 86 | }
|
---|
| 87 | };
|
---|
| 88 |
|
---|
| 89 | /** DP8390 kernel interrupt code.
|
---|
| 90 | */
|
---|
| 91 | static irq_code_t dp8390_code = {
|
---|
[aadf01e] | 92 | sizeof(dp8390_cmds) / sizeof(irq_cmd_t),
|
---|
[21580dd] | 93 | dp8390_cmds
|
---|
| 94 | };
|
---|
| 95 |
|
---|
| 96 | /** Handles the interrupt messages.
|
---|
| 97 | * This is the interrupt handler callback function.
|
---|
| 98 | * @param[in] iid The interrupt message identifier.
|
---|
| 99 | * @param[in] call The interrupt message.
|
---|
| 100 | */
|
---|
[14f1db0] | 101 | static void irq_handler(ipc_callid_t iid, ipc_call_t * call)
|
---|
| 102 | {
|
---|
| 103 | netif_device_t * device;
|
---|
| 104 | dpeth_t * dep;
|
---|
| 105 | packet_t received;
|
---|
| 106 | device_id_t device_id;
|
---|
| 107 | int phone;
|
---|
| 108 |
|
---|
| 109 | device_id = IRQ_GET_DEVICE(call);
|
---|
| 110 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
| 111 | if(find_device(device_id, &device) != EOK){
|
---|
| 112 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
| 113 | return;
|
---|
| 114 | }
|
---|
| 115 | dep = (dpeth_t *) device->specific;
|
---|
| 116 | if (dep->de_mode != DEM_ENABLED){
|
---|
| 117 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
| 118 | return;
|
---|
| 119 | }
|
---|
| 120 | assert(dep->de_flags &DEF_ENABLED);
|
---|
| 121 | dep->de_int_pending = 0;
|
---|
| 122 | dp_check_ints(dep, IPC_GET_ISR(call));
|
---|
| 123 | if(dep->received_queue){
|
---|
| 124 | received = dep->received_queue;
|
---|
| 125 | phone = device->nil_phone;
|
---|
| 126 | dep->received_queue = NULL;
|
---|
| 127 | dep->received_count = 0;
|
---|
| 128 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
| 129 | nil_received_msg(phone, device_id, received, NULL);
|
---|
| 130 | }else{
|
---|
| 131 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
| 132 | }
|
---|
| 133 | ipc_answer_0(iid, EOK);
|
---|
| 134 | }
|
---|
[21580dd] | 135 |
|
---|
| 136 | /** Changes the network interface state.
|
---|
| 137 | * @param[in,out] device The network interface.
|
---|
| 138 | * @param[in] state The new state.
|
---|
| 139 | * @returns The new state.
|
---|
| 140 | */
|
---|
[14f1db0] | 141 | static int change_state(netif_device_t * device, device_state_t state)
|
---|
| 142 | {
|
---|
| 143 | if (device->state != state) {
|
---|
| 144 | device->state = state;
|
---|
| 145 |
|
---|
| 146 | printf("%s: State changed to %s\n", NAME,
|
---|
| 147 | (state == NETIF_ACTIVE) ? "active" : "stopped");
|
---|
| 148 |
|
---|
| 149 | return state;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | return EOK;
|
---|
| 153 | }
|
---|
[21580dd] | 154 |
|
---|
[aadf01e] | 155 | int netif_specific_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
|
---|
[21580dd] | 156 | return ENOTSUP;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[f772bc55] | 159 | int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
|
---|
| 160 | {
|
---|
[14f1db0] | 161 | netif_device_t * device;
|
---|
[aadf01e] | 162 | eth_stat_t * de_stat;
|
---|
[3a5d238f] | 163 | int rc;
|
---|
[21580dd] | 164 |
|
---|
[aadf01e] | 165 | if(! stats){
|
---|
| 166 | return EBADMEM;
|
---|
| 167 | }
|
---|
[3a5d238f] | 168 | rc = find_device(device_id, &device);
|
---|
| 169 | if (rc != EOK)
|
---|
| 170 | return rc;
|
---|
[aadf01e] | 171 | de_stat = &((dpeth_t *) device->specific)->de_stat;
|
---|
| 172 | null_device_stats(stats);
|
---|
[21580dd] | 173 | stats->receive_errors = de_stat->ets_recvErr;
|
---|
| 174 | stats->send_errors = de_stat->ets_sendErr;
|
---|
| 175 | stats->receive_crc_errors = de_stat->ets_CRCerr;
|
---|
| 176 | stats->receive_frame_errors = de_stat->ets_frameAll;
|
---|
| 177 | stats->receive_missed_errors = de_stat->ets_missedP;
|
---|
| 178 | stats->receive_packets = de_stat->ets_packetR;
|
---|
| 179 | stats->send_packets = de_stat->ets_packetT;
|
---|
| 180 | stats->collisions = de_stat->ets_collision;
|
---|
| 181 | stats->send_aborted_errors = de_stat->ets_transAb;
|
---|
| 182 | stats->send_carrier_errors = de_stat->ets_carrSense;
|
---|
| 183 | stats->send_heartbeat_errors = de_stat->ets_CDheartbeat;
|
---|
| 184 | stats->send_window_errors = de_stat->ets_OWC;
|
---|
| 185 | return EOK;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[aadf01e] | 188 | int netif_get_addr_message(device_id_t device_id, measured_string_ref address){
|
---|
[14f1db0] | 189 | netif_device_t * device;
|
---|
[3a5d238f] | 190 | int rc;
|
---|
[21580dd] | 191 |
|
---|
[aadf01e] | 192 | if(! address){
|
---|
| 193 | return EBADMEM;
|
---|
| 194 | }
|
---|
[3a5d238f] | 195 | rc = find_device(device_id, &device);
|
---|
| 196 | if (rc != EOK)
|
---|
| 197 | return rc;
|
---|
[aadf01e] | 198 | address->value = (char *) (&((dpeth_t *) device->specific)->de_address);
|
---|
| 199 | address->length = CONVERT_SIZE(ether_addr_t, char, 1);
|
---|
[21580dd] | 200 | return EOK;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[aadf01e] | 203 |
|
---|
[21580dd] | 204 |
|
---|
[aadf01e] | 205 | int netif_probe_message(device_id_t device_id, int irq, uintptr_t io){
|
---|
[14f1db0] | 206 | netif_device_t * device;
|
---|
[aadf01e] | 207 | dpeth_t * dep;
|
---|
[3a5d238f] | 208 | int rc;
|
---|
[21580dd] | 209 |
|
---|
[14f1db0] | 210 | device = (netif_device_t *) malloc(sizeof(netif_device_t));
|
---|
[aadf01e] | 211 | if(! device){
|
---|
| 212 | return ENOMEM;
|
---|
| 213 | }
|
---|
| 214 | dep = (dpeth_t *) malloc(sizeof(dpeth_t));
|
---|
| 215 | if(! dep){
|
---|
| 216 | free(device);
|
---|
[21580dd] | 217 | return ENOMEM;
|
---|
| 218 | }
|
---|
[14f1db0] | 219 | bzero(device, sizeof(netif_device_t));
|
---|
[aadf01e] | 220 | bzero(dep, sizeof(dpeth_t));
|
---|
[21580dd] | 221 | device->device_id = device_id;
|
---|
| 222 | device->nil_phone = -1;
|
---|
[aadf01e] | 223 | device->specific = (void *) dep;
|
---|
[21580dd] | 224 | device->state = NETIF_STOPPED;
|
---|
| 225 | dep->de_irq = irq;
|
---|
| 226 | dep->de_mode = DEM_DISABLED;
|
---|
| 227 | //TODO address?
|
---|
[3a5d238f] | 228 | rc = pio_enable((void *) io, DP8390_IO_SIZE, (void **) &dep->de_base_port);
|
---|
| 229 | if (rc != EOK) {
|
---|
| 230 | free(dep);
|
---|
| 231 | free(device);
|
---|
| 232 | return rc;
|
---|
| 233 | }
|
---|
| 234 | rc = do_probe(dep);
|
---|
| 235 | if (rc != EOK) {
|
---|
[aadf01e] | 236 | free(dep);
|
---|
| 237 | free(device);
|
---|
[3a5d238f] | 238 | return rc;
|
---|
[21580dd] | 239 | }
|
---|
[3a5d238f] | 240 | rc = netif_device_map_add(&netif_globals.device_map, device->device_id, device);
|
---|
| 241 | if (rc != EOK){
|
---|
[aadf01e] | 242 | free(dep);
|
---|
| 243 | free(device);
|
---|
[3a5d238f] | 244 | return rc;
|
---|
[21580dd] | 245 | }
|
---|
| 246 | return EOK;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[aadf01e] | 249 | int netif_send_message(device_id_t device_id, packet_t packet, services_t sender){
|
---|
[14f1db0] | 250 | netif_device_t * device;
|
---|
[aadf01e] | 251 | dpeth_t * dep;
|
---|
| 252 | packet_t next;
|
---|
[3a5d238f] | 253 | int rc;
|
---|
[21580dd] | 254 |
|
---|
[3a5d238f] | 255 | rc = find_device(device_id, &device);
|
---|
| 256 | if (rc != EOK)
|
---|
| 257 | return rc;
|
---|
[aadf01e] | 258 | if(device->state != NETIF_ACTIVE){
|
---|
| 259 | netif_pq_release(packet_get_id(packet));
|
---|
[21580dd] | 260 | return EFORWARD;
|
---|
| 261 | }
|
---|
[aadf01e] | 262 | dep = (dpeth_t *) device->specific;
|
---|
[21580dd] | 263 | // process packet queue
|
---|
| 264 | do{
|
---|
[aadf01e] | 265 | next = pq_detach(packet);
|
---|
| 266 | if(do_pwrite(dep, packet, FALSE) != EBUSY){
|
---|
| 267 | netif_pq_release(packet_get_id(packet));
|
---|
[21580dd] | 268 | }
|
---|
| 269 | packet = next;
|
---|
[aadf01e] | 270 | }while(packet);
|
---|
[21580dd] | 271 | return EOK;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[14f1db0] | 274 | int netif_start_message(netif_device_t * device){
|
---|
[aadf01e] | 275 | dpeth_t * dep;
|
---|
[3a5d238f] | 276 | int rc;
|
---|
[21580dd] | 277 |
|
---|
[aadf01e] | 278 | if(device->state != NETIF_ACTIVE){
|
---|
| 279 | dep = (dpeth_t *) device->specific;
|
---|
| 280 | dp8390_cmds[0].addr = (void *) (uintptr_t) (dep->de_dp8390_port + DP_ISR);
|
---|
| 281 | dp8390_cmds[2].addr = dp8390_cmds[0].addr;
|
---|
[3a5d238f] | 282 | rc = ipc_register_irq(dep->de_irq, device->device_id, device->device_id, &dp8390_code);
|
---|
| 283 | if (rc != EOK)
|
---|
| 284 | return rc;
|
---|
| 285 | rc = do_init(dep, DL_BROAD_REQ);
|
---|
| 286 | if (rc != EOK) {
|
---|
[aadf01e] | 287 | ipc_unregister_irq(dep->de_irq, device->device_id);
|
---|
[3a5d238f] | 288 | return rc;
|
---|
[21580dd] | 289 | }
|
---|
[aadf01e] | 290 | return change_state(device, NETIF_ACTIVE);
|
---|
[21580dd] | 291 | }
|
---|
| 292 | return EOK;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[14f1db0] | 295 | int netif_stop_message(netif_device_t * device){
|
---|
[aadf01e] | 296 | dpeth_t * dep;
|
---|
[21580dd] | 297 |
|
---|
[aadf01e] | 298 | if(device->state != NETIF_STOPPED){
|
---|
| 299 | dep = (dpeth_t *) device->specific;
|
---|
| 300 | do_stop(dep);
|
---|
| 301 | ipc_unregister_irq(dep->de_irq, device->device_id);
|
---|
| 302 | return change_state(device, NETIF_STOPPED);
|
---|
[21580dd] | 303 | }
|
---|
| 304 | return EOK;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
[aadf01e] | 307 | int netif_initialize(void){
|
---|
| 308 | ipcarg_t phonehash;
|
---|
[21580dd] | 309 |
|
---|
[aadf01e] | 310 | async_set_interrupt_received(irq_handler);
|
---|
[21580dd] | 311 |
|
---|
[aadf01e] | 312 | return REGISTER_ME(SERVICE_DP8390, &phonehash);
|
---|
[21580dd] | 313 | }
|
---|
| 314 |
|
---|
[849ed54] | 315 | /** Default thread for new connections.
|
---|
| 316 | *
|
---|
| 317 | * @param[in] iid The initial message identifier.
|
---|
| 318 | * @param[in] icall The initial message call structure.
|
---|
| 319 | *
|
---|
| 320 | */
|
---|
| 321 | static void netif_client_connection(ipc_callid_t iid, ipc_call_t * icall)
|
---|
| 322 | {
|
---|
| 323 | /*
|
---|
| 324 | * Accept the connection
|
---|
| 325 | * - Answer the first IPC_M_CONNECT_ME_TO call.
|
---|
| 326 | */
|
---|
| 327 | ipc_answer_0(iid, EOK);
|
---|
| 328 |
|
---|
| 329 | while(true) {
|
---|
| 330 | ipc_call_t answer;
|
---|
| 331 | int answer_count;
|
---|
| 332 |
|
---|
| 333 | /* Clear the answer structure */
|
---|
| 334 | refresh_answer(&answer, &answer_count);
|
---|
| 335 |
|
---|
| 336 | /* Fetch the next message */
|
---|
| 337 | ipc_call_t call;
|
---|
| 338 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 339 |
|
---|
| 340 | /* Process the message */
|
---|
[24ab58b3] | 341 | int res = netif_module_message(NAME, callid, &call, &answer,
|
---|
| 342 | &answer_count);
|
---|
[849ed54] | 343 |
|
---|
| 344 | /* End if said to either by the message or the processing result */
|
---|
| 345 | if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
|
---|
| 346 | return;
|
---|
| 347 |
|
---|
| 348 | /* Answer the message */
|
---|
| 349 | answer_call(callid, res, &answer, answer_count);
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | /** Starts the module.
|
---|
| 354 | *
|
---|
| 355 | * @param argc The count of the command line arguments. Ignored parameter.
|
---|
| 356 | * @param argv The command line parameters. Ignored parameter.
|
---|
| 357 | *
|
---|
| 358 | * @returns EOK on success.
|
---|
| 359 | * @returns Other error codes as defined for each specific module start function.
|
---|
| 360 | *
|
---|
| 361 | */
|
---|
| 362 | int main(int argc, char *argv[])
|
---|
| 363 | {
|
---|
[3a5d238f] | 364 | int rc;
|
---|
[849ed54] | 365 |
|
---|
| 366 | /* Start the module */
|
---|
[3a5d238f] | 367 | rc = netif_module_start(netif_client_connection);
|
---|
| 368 | return rc;
|
---|
[849ed54] | 369 | }
|
---|
| 370 |
|
---|
[21580dd] | 371 | /** @}
|
---|
| 372 | */
|
---|