source: mainline/uspace/srv/hw/netif/dp8390/dp8390_module.c@ 6fc0edd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6fc0edd was 6fc0edd, checked in by Martin Decky <martin@…>, 15 years ago

more robust interrupt processing

  • Property mode set to 100644
File size: 9.5 KB
RevLine 
[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>
[c7a8442]45#include <net/modules.h>
[0a866eeb]46#include <packet_client.h>
[849ed54]47#include <adt/measured_strings.h>
[e526f08]48#include <net/device.h>
[849ed54]49#include <nil_interface.h>
[14f1db0]50#include <netif_interface.h>
51#include <netif_local.h>
[21580dd]52#include "dp8390.h"
53#include "dp8390_drv.h"
54#include "dp8390_port.h"
55
56/** DP8390 module name.
57 */
[24ab58b3]58#define NAME "dp8390"
[21580dd]59
[6fc0edd]60/** Return the device from the interrupt call.
61 *
62 * @param[in] call The interrupt call.
63 *
64 */
65#define IRQ_GET_DEVICE(call) ((device_id_t) IPC_GET_IMETHOD(call))
66
67/** Return the ISR from the interrupt call.
68 *
[21580dd]69 * @param[in] call The interrupt call.
[6fc0edd]70 *
[21580dd]71 */
[6fc0edd]72#define IRQ_GET_ISR(call) ((int) IPC_GET_ARG2(call))
[21580dd]73
74/** DP8390 kernel interrupt command sequence.
75 */
[0777f4c5]76static irq_cmd_t dp8390_cmds[] = {
77 {
78 .cmd = CMD_PIO_READ_8,
[21580dd]79 .addr = NULL,
80 .dstarg = 2
81 },
[6fc0edd]82 {
83 .cmd = CMD_BTEST,
84 .value = 0x7f,
85 .srcarg = 2,
86 .dstarg = 3,
87 },
[21580dd]88 {
89 .cmd = CMD_PREDICATE,
[6fc0edd]90 .value = 2,
91 .srcarg = 3
92 },
93 {
94 .cmd = CMD_PIO_WRITE_8,
95 .addr = NULL,
96 .value = 0xff
[21580dd]97 },
98 {
99 .cmd = CMD_ACCEPT
100 }
101};
102
103/** DP8390 kernel interrupt code.
104 */
[0777f4c5]105static irq_code_t dp8390_code = {
[aadf01e]106 sizeof(dp8390_cmds) / sizeof(irq_cmd_t),
[21580dd]107 dp8390_cmds
108};
109
110/** Handles the interrupt messages.
111 * This is the interrupt handler callback function.
112 * @param[in] iid The interrupt message identifier.
113 * @param[in] call The interrupt message.
114 */
[0777f4c5]115static void irq_handler(ipc_callid_t iid, ipc_call_t *call)
[14f1db0]116{
[0777f4c5]117 netif_device_t *device;
118 dpeth_t *dep;
[46d4d9f]119 packet_t *received;
[14f1db0]120 device_id_t device_id;
121 int phone;
[0777f4c5]122
[6fc0edd]123 device_id = IRQ_GET_DEVICE(*call);
[14f1db0]124 fibril_rwlock_write_lock(&netif_globals.lock);
[0777f4c5]125
126 if (find_device(device_id, &device) != EOK) {
[14f1db0]127 fibril_rwlock_write_unlock(&netif_globals.lock);
128 return;
129 }
[0777f4c5]130
[14f1db0]131 dep = (dpeth_t *) device->specific;
[0777f4c5]132 if (dep->de_mode != DEM_ENABLED) {
[14f1db0]133 fibril_rwlock_write_unlock(&netif_globals.lock);
134 return;
135 }
[0777f4c5]136
137 assert(dep->de_flags & DEF_ENABLED);
138
[6fc0edd]139 dp_check_ints(dep, IRQ_GET_ISR(*call));
[0777f4c5]140
141 if (dep->received_queue) {
[14f1db0]142 received = dep->received_queue;
143 phone = device->nil_phone;
144 dep->received_queue = NULL;
145 dep->received_count = 0;
146 fibril_rwlock_write_unlock(&netif_globals.lock);
[0b4a67a]147 nil_received_msg(phone, device_id, received, SERVICE_NONE);
[0777f4c5]148 } else
[14f1db0]149 fibril_rwlock_write_unlock(&netif_globals.lock);
150}
[21580dd]151
152/** Changes the network interface state.
153 * @param[in,out] device The network interface.
154 * @param[in] state The new state.
155 * @returns The new state.
156 */
[0777f4c5]157static int change_state(netif_device_t *device, device_state_t state)
[14f1db0]158{
159 if (device->state != state) {
160 device->state = state;
161
162 printf("%s: State changed to %s\n", NAME,
163 (state == NETIF_ACTIVE) ? "active" : "stopped");
164
165 return state;
166 }
167
168 return EOK;
169}
[21580dd]170
[0777f4c5]171int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
172 ipc_call_t *answer, int *answer_count)
173{
[21580dd]174 return ENOTSUP;
175}
176
[f772bc55]177int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
178{
[14f1db0]179 netif_device_t * device;
[aadf01e]180 eth_stat_t * de_stat;
[3a5d238f]181 int rc;
[0777f4c5]182
183 if (!stats)
[aadf01e]184 return EBADMEM;
[0777f4c5]185
[3a5d238f]186 rc = find_device(device_id, &device);
187 if (rc != EOK)
188 return rc;
[0777f4c5]189
[aadf01e]190 de_stat = &((dpeth_t *) device->specific)->de_stat;
[0777f4c5]191
[aadf01e]192 null_device_stats(stats);
[21580dd]193 stats->receive_errors = de_stat->ets_recvErr;
194 stats->send_errors = de_stat->ets_sendErr;
195 stats->receive_crc_errors = de_stat->ets_CRCerr;
196 stats->receive_frame_errors = de_stat->ets_frameAll;
197 stats->receive_missed_errors = de_stat->ets_missedP;
198 stats->receive_packets = de_stat->ets_packetR;
199 stats->send_packets = de_stat->ets_packetT;
200 stats->collisions = de_stat->ets_collision;
201 stats->send_aborted_errors = de_stat->ets_transAb;
202 stats->send_carrier_errors = de_stat->ets_carrSense;
203 stats->send_heartbeat_errors = de_stat->ets_CDheartbeat;
204 stats->send_window_errors = de_stat->ets_OWC;
[0777f4c5]205
[21580dd]206 return EOK;
207}
208
[0777f4c5]209int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
210{
211 netif_device_t *device;
[3a5d238f]212 int rc;
[0777f4c5]213
214 if (!address)
[aadf01e]215 return EBADMEM;
[0777f4c5]216
[3a5d238f]217 rc = find_device(device_id, &device);
218 if (rc != EOK)
219 return rc;
[0777f4c5]220
[aadf01e]221 address->value = (char *) (&((dpeth_t *) device->specific)->de_address);
[1b59023]222 address->length = sizeof(ether_addr_t);
[21580dd]223 return EOK;
224}
225
[0777f4c5]226int netif_probe_message(device_id_t device_id, int irq, uintptr_t io)
227{
228 netif_device_t *device;
229 dpeth_t *dep;
[3a5d238f]230 int rc;
[0777f4c5]231
[14f1db0]232 device = (netif_device_t *) malloc(sizeof(netif_device_t));
[0777f4c5]233 if (!device)
[aadf01e]234 return ENOMEM;
[0777f4c5]235
[aadf01e]236 dep = (dpeth_t *) malloc(sizeof(dpeth_t));
[0777f4c5]237 if (!dep) {
[aadf01e]238 free(device);
[21580dd]239 return ENOMEM;
240 }
[0777f4c5]241
[14f1db0]242 bzero(device, sizeof(netif_device_t));
[aadf01e]243 bzero(dep, sizeof(dpeth_t));
[21580dd]244 device->device_id = device_id;
245 device->nil_phone = -1;
[aadf01e]246 device->specific = (void *) dep;
[21580dd]247 device->state = NETIF_STOPPED;
248 dep->de_irq = irq;
249 dep->de_mode = DEM_DISABLED;
[0777f4c5]250
[21580dd]251 //TODO address?
[3a5d238f]252 rc = pio_enable((void *) io, DP8390_IO_SIZE, (void **) &dep->de_base_port);
253 if (rc != EOK) {
254 free(dep);
255 free(device);
256 return rc;
[0777f4c5]257 }
258
[3a5d238f]259 rc = do_probe(dep);
260 if (rc != EOK) {
[aadf01e]261 free(dep);
262 free(device);
[3a5d238f]263 return rc;
[21580dd]264 }
[0777f4c5]265
[3a5d238f]266 rc = netif_device_map_add(&netif_globals.device_map, device->device_id, device);
[0777f4c5]267 if (rc != EOK) {
[aadf01e]268 free(dep);
269 free(device);
[3a5d238f]270 return rc;
[21580dd]271 }
[0777f4c5]272
[21580dd]273 return EOK;
274}
275
[0777f4c5]276int netif_send_message(device_id_t device_id, packet_t *packet,
277 services_t sender)
278{
279 netif_device_t *device;
280 dpeth_t *dep;
[46d4d9f]281 packet_t *next;
[3a5d238f]282 int rc;
[0777f4c5]283
[3a5d238f]284 rc = find_device(device_id, &device);
285 if (rc != EOK)
286 return rc;
[0777f4c5]287
288 if (device->state != NETIF_ACTIVE){
[aadf01e]289 netif_pq_release(packet_get_id(packet));
[21580dd]290 return EFORWARD;
291 }
[0777f4c5]292
[aadf01e]293 dep = (dpeth_t *) device->specific;
[0777f4c5]294
295 /* Process packet queue */
296 do {
[aadf01e]297 next = pq_detach(packet);
[0777f4c5]298
299 if (do_pwrite(dep, packet, false) != EBUSY)
[6fc0edd]300 netif_pq_release(packet_get_id(packet));
[0777f4c5]301
[21580dd]302 packet = next;
[0777f4c5]303 } while(packet);
304
[21580dd]305 return EOK;
306}
307
[0777f4c5]308int netif_start_message(netif_device_t * device)
309{
310 dpeth_t *dep;
[3a5d238f]311 int rc;
[0777f4c5]312
313 if (device->state != NETIF_ACTIVE) {
[aadf01e]314 dep = (dpeth_t *) device->specific;
315 dp8390_cmds[0].addr = (void *) (uintptr_t) (dep->de_dp8390_port + DP_ISR);
[6fc0edd]316 dp8390_cmds[3].addr = dp8390_cmds[0].addr;
[0777f4c5]317
[3a5d238f]318 rc = ipc_register_irq(dep->de_irq, device->device_id, device->device_id, &dp8390_code);
319 if (rc != EOK)
320 return rc;
[0777f4c5]321
[3a5d238f]322 rc = do_init(dep, DL_BROAD_REQ);
323 if (rc != EOK) {
[aadf01e]324 ipc_unregister_irq(dep->de_irq, device->device_id);
[3a5d238f]325 return rc;
[21580dd]326 }
[0777f4c5]327
[aadf01e]328 return change_state(device, NETIF_ACTIVE);
[21580dd]329 }
[0777f4c5]330
[21580dd]331 return EOK;
332}
333
[0777f4c5]334int netif_stop_message(netif_device_t * device)
335{
336 dpeth_t *dep;
337
338 if (device->state != NETIF_STOPPED) {
[aadf01e]339 dep = (dpeth_t *) device->specific;
340 do_stop(dep);
341 ipc_unregister_irq(dep->de_irq, device->device_id);
342 return change_state(device, NETIF_STOPPED);
[21580dd]343 }
[0777f4c5]344
[21580dd]345 return EOK;
346}
347
[0777f4c5]348int netif_initialize(void)
349{
[96b02eb9]350 sysarg_t phonehash;
[aadf01e]351 async_set_interrupt_received(irq_handler);
[f87c900]352 return ipc_connect_to_me(PHONE_NS, SERVICE_DP8390, 0, 0, &phonehash);
[21580dd]353}
354
[849ed54]355/** Default thread for new connections.
356 *
357 * @param[in] iid The initial message identifier.
358 * @param[in] icall The initial message call structure.
359 *
360 */
[0777f4c5]361static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
[849ed54]362{
363 /*
364 * Accept the connection
365 * - Answer the first IPC_M_CONNECT_ME_TO call.
366 */
367 ipc_answer_0(iid, EOK);
368
[0777f4c5]369 while (true) {
[849ed54]370 ipc_call_t answer;
371 int answer_count;
372
373 /* Clear the answer structure */
374 refresh_answer(&answer, &answer_count);
375
376 /* Fetch the next message */
377 ipc_call_t call;
378 ipc_callid_t callid = async_get_call(&call);
379
380 /* Process the message */
[24ab58b3]381 int res = netif_module_message(NAME, callid, &call, &answer,
382 &answer_count);
[849ed54]383
384 /* End if said to either by the message or the processing result */
[228e490]385 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
[849ed54]386 return;
387
388 /* Answer the message */
389 answer_call(callid, res, &answer, answer_count);
390 }
391}
392
[0777f4c5]393/** Start the module.
[849ed54]394 *
395 * @param argc The count of the command line arguments. Ignored parameter.
396 * @param argv The command line parameters. Ignored parameter.
397 *
398 * @returns EOK on success.
399 * @returns Other error codes as defined for each specific module start function.
400 *
401 */
402int main(int argc, char *argv[])
403{
404 /* Start the module */
[0777f4c5]405 return netif_module_start(netif_client_connection);
[849ed54]406}
407
[21580dd]408/** @}
409 */
Note: See TracBrowser for help on using the repository browser.