[e0854e3] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
| 3 | * Copyright (c) 2011 Martin Decky
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[3c106e88] | 30 | /** @addtogroup ne2000
|
---|
[21580dd] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | /** @file
|
---|
[3c106e88] | 35 | * NE2000 network interface implementation.
|
---|
[21580dd] | 36 | */
|
---|
| 37 |
|
---|
[3c106e88] | 38 | #include <assert.h>
|
---|
| 39 | #include <async.h>
|
---|
| 40 | #include <ddi.h>
|
---|
| 41 | #include <errno.h>
|
---|
| 42 | #include <err.h>
|
---|
| 43 | #include <malloc.h>
|
---|
| 44 | #include <sysinfo.h>
|
---|
| 45 | #include <ipc/ipc.h>
|
---|
| 46 | #include <ipc/services.h>
|
---|
| 47 | #include <ipc/irc.h>
|
---|
| 48 | #include <net/modules.h>
|
---|
| 49 | #include <packet_client.h>
|
---|
| 50 | #include <adt/measured_strings.h>
|
---|
| 51 | #include <net/device.h>
|
---|
| 52 | #include <netif_skel.h>
|
---|
[fe8dfa6] | 53 | #include <nil_remote.h>
|
---|
[21580dd] | 54 | #include "dp8390.h"
|
---|
| 55 |
|
---|
[3c106e88] | 56 | /** Return the device from the interrupt call.
|
---|
| 57 | *
|
---|
| 58 | * @param[in] call The interrupt call.
|
---|
| 59 | *
|
---|
| 60 | */
|
---|
| 61 | #define IRQ_GET_DEVICE(call) ((device_id_t) IPC_GET_IMETHOD(call))
|
---|
[21580dd] | 62 |
|
---|
[3c106e88] | 63 | /** Return the ISR from the interrupt call.
|
---|
| 64 | *
|
---|
| 65 | * @param[in] call The interrupt call.
|
---|
| 66 | *
|
---|
| 67 | */
|
---|
| 68 | #define IRQ_GET_ISR(call) ((int) IPC_GET_ARG2(call))
|
---|
[21580dd] | 69 |
|
---|
[7300c37] | 70 | /** Return the TSR from the interrupt call.
|
---|
| 71 | *
|
---|
| 72 | * @param[in] call The interrupt call.
|
---|
| 73 | *
|
---|
| 74 | */
|
---|
| 75 | #define IRQ_GET_TSR(call) ((int) IPC_GET_ARG3(call))
|
---|
| 76 |
|
---|
[3c106e88] | 77 | static int irc_service = 0;
|
---|
| 78 | static int irc_phone = -1;
|
---|
[21580dd] | 79 |
|
---|
[f902d36] | 80 | /** NE2000 kernel interrupt command sequence.
|
---|
[3c106e88] | 81 | *
|
---|
[21580dd] | 82 | */
|
---|
[3c106e88] | 83 | static irq_cmd_t ne2k_cmds[] = {
|
---|
| 84 | {
|
---|
[abe95c9] | 85 | /* Read Interrupt Status Register */
|
---|
[3c106e88] | 86 | .cmd = CMD_PIO_READ_8,
|
---|
| 87 | .addr = NULL,
|
---|
| 88 | .dstarg = 2
|
---|
| 89 | },
|
---|
| 90 | {
|
---|
[abe95c9] | 91 | /* Mask supported interrupt causes */
|
---|
[3c106e88] | 92 | .cmd = CMD_BTEST,
|
---|
[abe95c9] | 93 | .value = (ISR_PRX | ISR_PTX | ISR_RXE | ISR_TXE | ISR_OVW |
|
---|
| 94 | ISR_CNT | ISR_RDC),
|
---|
[3c106e88] | 95 | .srcarg = 2,
|
---|
| 96 | .dstarg = 3,
|
---|
| 97 | },
|
---|
| 98 | {
|
---|
[abe95c9] | 99 | /* Predicate for accepting the interrupt */
|
---|
[3c106e88] | 100 | .cmd = CMD_PREDICATE,
|
---|
[7300c37] | 101 | .value = 4,
|
---|
[3c106e88] | 102 | .srcarg = 3
|
---|
| 103 | },
|
---|
| 104 | {
|
---|
[abe95c9] | 105 | /*
|
---|
| 106 | * Mask future interrupts via
|
---|
| 107 | * Interrupt Mask Register
|
---|
| 108 | */
|
---|
| 109 | .cmd = CMD_PIO_WRITE_8,
|
---|
| 110 | .addr = NULL,
|
---|
| 111 | .value = 0
|
---|
| 112 | },
|
---|
| 113 | {
|
---|
| 114 | /* Acknowledge the current interrupt */
|
---|
[3c106e88] | 115 | .cmd = CMD_PIO_WRITE_A_8,
|
---|
| 116 | .addr = NULL,
|
---|
| 117 | .srcarg = 3
|
---|
| 118 | },
|
---|
[7300c37] | 119 | {
|
---|
| 120 | /* Read Transmit Status Register */
|
---|
| 121 | .cmd = CMD_PIO_READ_8,
|
---|
| 122 | .addr = NULL,
|
---|
| 123 | .dstarg = 3
|
---|
| 124 | },
|
---|
[3c106e88] | 125 | {
|
---|
| 126 | .cmd = CMD_ACCEPT
|
---|
| 127 | }
|
---|
| 128 | };
|
---|
[21580dd] | 129 |
|
---|
[f902d36] | 130 | /** NE2000 kernel interrupt code.
|
---|
[3c106e88] | 131 | *
|
---|
[21580dd] | 132 | */
|
---|
[3c106e88] | 133 | static irq_code_t ne2k_code = {
|
---|
| 134 | sizeof(ne2k_cmds) / sizeof(irq_cmd_t),
|
---|
| 135 | ne2k_cmds
|
---|
| 136 | };
|
---|
[21580dd] | 137 |
|
---|
[3c106e88] | 138 | /** Handle the interrupt notification.
|
---|
| 139 | *
|
---|
[7300c37] | 140 | * This is the interrupt notification function. It is quarantied
|
---|
| 141 | * that there is only a single instance of this notification
|
---|
| 142 | * function running at one time until the return from the
|
---|
| 143 | * ne2k_interrupt() function (where the interrupts are unmasked
|
---|
| 144 | * again).
|
---|
[3c106e88] | 145 | *
|
---|
| 146 | * @param[in] iid Interrupt notification identifier.
|
---|
| 147 | * @param[in] call Interrupt notification.
|
---|
| 148 | *
|
---|
| 149 | */
|
---|
| 150 | static void irq_handler(ipc_callid_t iid, ipc_call_t *call)
|
---|
[21580dd] | 151 | {
|
---|
[3c106e88] | 152 | device_id_t device_id = IRQ_GET_DEVICE(*call);
|
---|
| 153 | netif_device_t *device;
|
---|
| 154 | int nil_phone;
|
---|
| 155 | ne2k_t *ne2k;
|
---|
[d3c9b60] | 156 |
|
---|
[3c106e88] | 157 | fibril_rwlock_read_lock(&netif_globals.lock);
|
---|
[d3c9b60] | 158 |
|
---|
[3c106e88] | 159 | if (find_device(device_id, &device) == EOK) {
|
---|
| 160 | nil_phone = device->nil_phone;
|
---|
| 161 | ne2k = (ne2k_t *) device->specific;
|
---|
| 162 | } else
|
---|
| 163 | ne2k = NULL;
|
---|
[d3c9b60] | 164 |
|
---|
[3c106e88] | 165 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
[d3c9b60] | 166 |
|
---|
[7300c37] | 167 | if (ne2k != NULL) {
|
---|
| 168 | link_t *frames =
|
---|
| 169 | ne2k_interrupt(ne2k, IRQ_GET_ISR(*call), IRQ_GET_TSR(*call));
|
---|
| 170 |
|
---|
| 171 | if (frames != NULL) {
|
---|
| 172 | while (!list_empty(frames)) {
|
---|
| 173 | frame_t *frame =
|
---|
| 174 | list_get_instance(frames->next, frame_t, link);
|
---|
| 175 |
|
---|
| 176 | list_remove(&frame->link);
|
---|
| 177 | nil_received_msg(nil_phone, device_id, frame->packet,
|
---|
| 178 | SERVICE_NONE);
|
---|
| 179 | free(frame);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | free(frames);
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
[21580dd] | 185 | }
|
---|
| 186 |
|
---|
[3c106e88] | 187 | /** Change the network interface state.
|
---|
[7922dea] | 188 | *
|
---|
[3c106e88] | 189 | * @param[in,out] device Network interface.
|
---|
| 190 | * @param[in] state New state.
|
---|
[7922dea] | 191 | *
|
---|
| 192 | */
|
---|
[3c106e88] | 193 | static void change_state(netif_device_t *device, device_state_t state)
|
---|
[21580dd] | 194 | {
|
---|
[3c106e88] | 195 | if (device->state != state) {
|
---|
| 196 | device->state = state;
|
---|
| 197 |
|
---|
| 198 | const char *desc;
|
---|
| 199 | switch (state) {
|
---|
| 200 | case NETIF_ACTIVE:
|
---|
| 201 | desc = "active";
|
---|
| 202 | break;
|
---|
| 203 | case NETIF_STOPPED:
|
---|
| 204 | desc = "stopped";
|
---|
| 205 | break;
|
---|
| 206 | default:
|
---|
| 207 | desc = "unknown";
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | printf("%s: State changed to %s\n", NAME, desc);
|
---|
[21580dd] | 211 | }
|
---|
[3c106e88] | 212 | }
|
---|
| 213 |
|
---|
| 214 | int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
|
---|
| 215 | ipc_call_t *answer, size_t *count)
|
---|
| 216 | {
|
---|
| 217 | return ENOTSUP;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
|
---|
| 221 | {
|
---|
| 222 | if (!stats)
|
---|
| 223 | return EBADMEM;
|
---|
[d3c9b60] | 224 |
|
---|
[3c106e88] | 225 | netif_device_t *device;
|
---|
| 226 | int rc = find_device(device_id, &device);
|
---|
| 227 | if (rc != EOK)
|
---|
| 228 | return rc;
|
---|
[d3c9b60] | 229 |
|
---|
[3c106e88] | 230 | ne2k_t *ne2k = (ne2k_t *) device->specific;
|
---|
[d3c9b60] | 231 |
|
---|
[3c106e88] | 232 | memcpy(stats, &ne2k->stats, sizeof(device_stats_t));
|
---|
| 233 | return EOK;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
|
---|
| 237 | {
|
---|
| 238 | if (!address)
|
---|
| 239 | return EBADMEM;
|
---|
[d3c9b60] | 240 |
|
---|
[3c106e88] | 241 | netif_device_t *device;
|
---|
| 242 | int rc = find_device(device_id, &device);
|
---|
| 243 | if (rc != EOK)
|
---|
| 244 | return rc;
|
---|
[d3c9b60] | 245 |
|
---|
[3c106e88] | 246 | ne2k_t *ne2k = (ne2k_t *) device->specific;
|
---|
[d3c9b60] | 247 |
|
---|
[3c106e88] | 248 | address->value = ne2k->mac;
|
---|
| 249 | address->length = ETH_ADDR;
|
---|
| 250 | return EOK;
|
---|
[21580dd] | 251 | }
|
---|
| 252 |
|
---|
[3c106e88] | 253 | int netif_probe_message(device_id_t device_id, int irq, void *io)
|
---|
[21580dd] | 254 | {
|
---|
[3c106e88] | 255 | netif_device_t *device =
|
---|
| 256 | (netif_device_t *) malloc(sizeof(netif_device_t));
|
---|
| 257 | if (!device)
|
---|
| 258 | return ENOMEM;
|
---|
[506a805] | 259 |
|
---|
[3c106e88] | 260 | ne2k_t *ne2k = (ne2k_t *) malloc(sizeof(ne2k_t));
|
---|
| 261 | if (!ne2k) {
|
---|
| 262 | free(device);
|
---|
| 263 | return ENOMEM;
|
---|
| 264 | }
|
---|
[506a805] | 265 |
|
---|
[3c106e88] | 266 | void *port;
|
---|
| 267 | int rc = pio_enable((void *) io, NE2K_IO_SIZE, &port);
|
---|
| 268 | if (rc != EOK) {
|
---|
| 269 | free(ne2k);
|
---|
| 270 | free(device);
|
---|
| 271 | return rc;
|
---|
| 272 | }
|
---|
[506a805] | 273 |
|
---|
[3c106e88] | 274 | bzero(device, sizeof(netif_device_t));
|
---|
| 275 | bzero(ne2k, sizeof(ne2k_t));
|
---|
[506a805] | 276 |
|
---|
[3c106e88] | 277 | device->device_id = device_id;
|
---|
| 278 | device->nil_phone = -1;
|
---|
| 279 | device->specific = (void *) ne2k;
|
---|
| 280 | device->state = NETIF_STOPPED;
|
---|
| 281 |
|
---|
| 282 | rc = ne2k_probe(ne2k, port, irq);
|
---|
| 283 | if (rc != EOK) {
|
---|
| 284 | printf("%s: No ethernet card found at I/O address %p\n",
|
---|
| 285 | NAME, port);
|
---|
| 286 | free(ne2k);
|
---|
| 287 | free(device);
|
---|
| 288 | return rc;
|
---|
[21580dd] | 289 | }
|
---|
[506a805] | 290 |
|
---|
[3c106e88] | 291 | rc = netif_device_map_add(&netif_globals.device_map, device->device_id, device);
|
---|
| 292 | if (rc != EOK) {
|
---|
| 293 | free(ne2k);
|
---|
| 294 | free(device);
|
---|
| 295 | return rc;
|
---|
[21580dd] | 296 | }
|
---|
[506a805] | 297 |
|
---|
[3c106e88] | 298 | printf("%s: Ethernet card at I/O address %p, IRQ %d, MAC ",
|
---|
| 299 | NAME, port, irq);
|
---|
[506a805] | 300 |
|
---|
[3c106e88] | 301 | unsigned int i;
|
---|
| 302 | for (i = 0; i < ETH_ADDR; i++)
|
---|
| 303 | printf("%02x%c", ne2k->mac[i], i < 5 ? ':' : '\n');
|
---|
[506a805] | 304 |
|
---|
[3c106e88] | 305 | return EOK;
|
---|
[21580dd] | 306 | }
|
---|
| 307 |
|
---|
[3c106e88] | 308 | int netif_start_message(netif_device_t *device)
|
---|
[21580dd] | 309 | {
|
---|
[3c106e88] | 310 | if (device->state != NETIF_ACTIVE) {
|
---|
| 311 | ne2k_t *ne2k = (ne2k_t *) device->specific;
|
---|
| 312 |
|
---|
| 313 | ne2k_cmds[0].addr = ne2k->port + DP_ISR;
|
---|
[abe95c9] | 314 | ne2k_cmds[3].addr = ne2k->port + DP_IMR;
|
---|
| 315 | ne2k_cmds[4].addr = ne2k_cmds[0].addr;
|
---|
[6b22c97] | 316 | ne2k_cmds[5].addr = ne2k->port + DP_TSR;
|
---|
[3c106e88] | 317 |
|
---|
| 318 | int rc = ipc_register_irq(ne2k->irq, device->device_id,
|
---|
| 319 | device->device_id, &ne2k_code);
|
---|
| 320 | if (rc != EOK)
|
---|
| 321 | return rc;
|
---|
| 322 |
|
---|
| 323 | rc = ne2k_up(ne2k);
|
---|
| 324 | if (rc != EOK) {
|
---|
| 325 | ipc_unregister_irq(ne2k->irq, device->device_id);
|
---|
| 326 | return rc;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[43b4314] | 329 | change_state(device, NETIF_ACTIVE);
|
---|
| 330 |
|
---|
[3c106e88] | 331 | if (irc_service)
|
---|
| 332 | async_msg_1(irc_phone, IRC_ENABLE_INTERRUPT, ne2k->irq);
|
---|
| 333 | }
|
---|
[506a805] | 334 |
|
---|
[3c106e88] | 335 | return device->state;
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | int netif_stop_message(netif_device_t *device)
|
---|
| 339 | {
|
---|
| 340 | if (device->state != NETIF_STOPPED) {
|
---|
| 341 | ne2k_t *ne2k = (ne2k_t *) device->specific;
|
---|
| 342 |
|
---|
| 343 | ne2k_down(ne2k);
|
---|
| 344 | ipc_unregister_irq(ne2k->irq, device->device_id);
|
---|
| 345 | change_state(device, NETIF_STOPPED);
|
---|
| 346 | }
|
---|
[506a805] | 347 |
|
---|
[fe8dfa6] | 348 | return device->state;
|
---|
[3c106e88] | 349 | }
|
---|
| 350 |
|
---|
| 351 | int netif_send_message(device_id_t device_id, packet_t *packet,
|
---|
| 352 | services_t sender)
|
---|
| 353 | {
|
---|
| 354 | netif_device_t *device;
|
---|
| 355 | int rc = find_device(device_id, &device);
|
---|
| 356 | if (rc != EOK)
|
---|
| 357 | return rc;
|
---|
[506a805] | 358 |
|
---|
[3c106e88] | 359 | if (device->state != NETIF_ACTIVE) {
|
---|
| 360 | netif_pq_release(packet_get_id(packet));
|
---|
| 361 | return EFORWARD;
|
---|
[21580dd] | 362 | }
|
---|
[506a805] | 363 |
|
---|
[3c106e88] | 364 | ne2k_t *ne2k = (ne2k_t *) device->specific;
|
---|
[506a805] | 365 |
|
---|
[3c106e88] | 366 | /*
|
---|
| 367 | * Process the packet queue
|
---|
| 368 | */
|
---|
[506a805] | 369 |
|
---|
[3c106e88] | 370 | do {
|
---|
| 371 | packet_t *next = pq_detach(packet);
|
---|
| 372 | ne2k_send(ne2k, packet);
|
---|
| 373 | netif_pq_release(packet_get_id(packet));
|
---|
| 374 | packet = next;
|
---|
| 375 | } while (packet);
|
---|
[506a805] | 376 |
|
---|
[3c106e88] | 377 | return EOK;
|
---|
[21580dd] | 378 | }
|
---|
| 379 |
|
---|
[3c106e88] | 380 | int netif_initialize(void)
|
---|
| 381 | {
|
---|
| 382 | sysarg_t apic;
|
---|
| 383 | sysarg_t i8259;
|
---|
| 384 |
|
---|
| 385 | if ((sysinfo_get_value("apic", &apic) == EOK) && (apic))
|
---|
| 386 | irc_service = SERVICE_APIC;
|
---|
| 387 | else if ((sysinfo_get_value("i8259", &i8259) == EOK) && (i8259))
|
---|
| 388 | irc_service = SERVICE_I8259;
|
---|
| 389 |
|
---|
| 390 | if (irc_service) {
|
---|
| 391 | while (irc_phone < 0) {
|
---|
| 392 | irc_phone = ipc_connect_me_to_blocking(PHONE_NS, irc_service,
|
---|
| 393 | 0, 0);
|
---|
| 394 | }
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | async_set_interrupt_received(irq_handler);
|
---|
| 398 |
|
---|
[124c061] | 399 | return ipc_connect_to_me(PHONE_NS, SERVICE_NE2000, 0, 0, NULL, NULL);
|
---|
[3c106e88] | 400 | }
|
---|
| 401 |
|
---|
| 402 | int main(int argc, char *argv[])
|
---|
[21580dd] | 403 | {
|
---|
[3c106e88] | 404 | /* Start the module */
|
---|
| 405 | return netif_module_start();
|
---|
[21580dd] | 406 | }
|
---|
| 407 |
|
---|
| 408 | /** @}
|
---|
| 409 | */
|
---|