source: mainline/uspace/srv/hw/netif/dp8390/dp8390_module.c@ 096234b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 096234b 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
Line 
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>
41#include <err.h>
42#include <malloc.h>
43#include <ipc/ipc.h>
44#include <ipc/services.h>
45#include <net/modules.h>
46#include <packet_client.h>
47#include <adt/measured_strings.h>
48#include <net/device.h>
49#include <nil_interface.h>
50#include <netif_interface.h>
51#include <netif_local.h>
52#include "dp8390.h"
53#include "dp8390_drv.h"
54#include "dp8390_port.h"
55
56/** DP8390 module name.
57 */
58#define NAME "dp8390"
59
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 *
69 * @param[in] call The interrupt call.
70 *
71 */
72#define IRQ_GET_ISR(call) ((int) IPC_GET_ARG2(call))
73
74/** DP8390 kernel interrupt command sequence.
75 */
76static irq_cmd_t dp8390_cmds[] = {
77 {
78 .cmd = CMD_PIO_READ_8,
79 .addr = NULL,
80 .dstarg = 2
81 },
82 {
83 .cmd = CMD_BTEST,
84 .value = 0x7f,
85 .srcarg = 2,
86 .dstarg = 3,
87 },
88 {
89 .cmd = CMD_PREDICATE,
90 .value = 2,
91 .srcarg = 3
92 },
93 {
94 .cmd = CMD_PIO_WRITE_8,
95 .addr = NULL,
96 .value = 0xff
97 },
98 {
99 .cmd = CMD_ACCEPT
100 }
101};
102
103/** DP8390 kernel interrupt code.
104 */
105static irq_code_t dp8390_code = {
106 sizeof(dp8390_cmds) / sizeof(irq_cmd_t),
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 */
115static void irq_handler(ipc_callid_t iid, ipc_call_t *call)
116{
117 netif_device_t *device;
118 dpeth_t *dep;
119 packet_t *received;
120 device_id_t device_id;
121 int phone;
122
123 device_id = IRQ_GET_DEVICE(*call);
124 fibril_rwlock_write_lock(&netif_globals.lock);
125
126 if (find_device(device_id, &device) != EOK) {
127 fibril_rwlock_write_unlock(&netif_globals.lock);
128 return;
129 }
130
131 dep = (dpeth_t *) device->specific;
132 if (dep->de_mode != DEM_ENABLED) {
133 fibril_rwlock_write_unlock(&netif_globals.lock);
134 return;
135 }
136
137 assert(dep->de_flags & DEF_ENABLED);
138
139 dp_check_ints(dep, IRQ_GET_ISR(*call));
140
141 if (dep->received_queue) {
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);
147 nil_received_msg(phone, device_id, received, SERVICE_NONE);
148 } else
149 fibril_rwlock_write_unlock(&netif_globals.lock);
150}
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 */
157static int change_state(netif_device_t *device, device_state_t state)
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}
170
171int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
172 ipc_call_t *answer, int *answer_count)
173{
174 return ENOTSUP;
175}
176
177int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
178{
179 netif_device_t * device;
180 eth_stat_t * de_stat;
181 int rc;
182
183 if (!stats)
184 return EBADMEM;
185
186 rc = find_device(device_id, &device);
187 if (rc != EOK)
188 return rc;
189
190 de_stat = &((dpeth_t *) device->specific)->de_stat;
191
192 null_device_stats(stats);
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;
205
206 return EOK;
207}
208
209int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
210{
211 netif_device_t *device;
212 int rc;
213
214 if (!address)
215 return EBADMEM;
216
217 rc = find_device(device_id, &device);
218 if (rc != EOK)
219 return rc;
220
221 address->value = (char *) (&((dpeth_t *) device->specific)->de_address);
222 address->length = sizeof(ether_addr_t);
223 return EOK;
224}
225
226int netif_probe_message(device_id_t device_id, int irq, uintptr_t io)
227{
228 netif_device_t *device;
229 dpeth_t *dep;
230 int rc;
231
232 device = (netif_device_t *) malloc(sizeof(netif_device_t));
233 if (!device)
234 return ENOMEM;
235
236 dep = (dpeth_t *) malloc(sizeof(dpeth_t));
237 if (!dep) {
238 free(device);
239 return ENOMEM;
240 }
241
242 bzero(device, sizeof(netif_device_t));
243 bzero(dep, sizeof(dpeth_t));
244 device->device_id = device_id;
245 device->nil_phone = -1;
246 device->specific = (void *) dep;
247 device->state = NETIF_STOPPED;
248 dep->de_irq = irq;
249 dep->de_mode = DEM_DISABLED;
250
251 //TODO address?
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;
257 }
258
259 rc = do_probe(dep);
260 if (rc != EOK) {
261 free(dep);
262 free(device);
263 return rc;
264 }
265
266 rc = netif_device_map_add(&netif_globals.device_map, device->device_id, device);
267 if (rc != EOK) {
268 free(dep);
269 free(device);
270 return rc;
271 }
272
273 return EOK;
274}
275
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;
281 packet_t *next;
282 int rc;
283
284 rc = find_device(device_id, &device);
285 if (rc != EOK)
286 return rc;
287
288 if (device->state != NETIF_ACTIVE){
289 netif_pq_release(packet_get_id(packet));
290 return EFORWARD;
291 }
292
293 dep = (dpeth_t *) device->specific;
294
295 /* Process packet queue */
296 do {
297 next = pq_detach(packet);
298
299 if (do_pwrite(dep, packet, false) != EBUSY)
300 netif_pq_release(packet_get_id(packet));
301
302 packet = next;
303 } while(packet);
304
305 return EOK;
306}
307
308int netif_start_message(netif_device_t * device)
309{
310 dpeth_t *dep;
311 int rc;
312
313 if (device->state != NETIF_ACTIVE) {
314 dep = (dpeth_t *) device->specific;
315 dp8390_cmds[0].addr = (void *) (uintptr_t) (dep->de_dp8390_port + DP_ISR);
316 dp8390_cmds[3].addr = dp8390_cmds[0].addr;
317
318 rc = ipc_register_irq(dep->de_irq, device->device_id, device->device_id, &dp8390_code);
319 if (rc != EOK)
320 return rc;
321
322 rc = do_init(dep, DL_BROAD_REQ);
323 if (rc != EOK) {
324 ipc_unregister_irq(dep->de_irq, device->device_id);
325 return rc;
326 }
327
328 return change_state(device, NETIF_ACTIVE);
329 }
330
331 return EOK;
332}
333
334int netif_stop_message(netif_device_t * device)
335{
336 dpeth_t *dep;
337
338 if (device->state != NETIF_STOPPED) {
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);
343 }
344
345 return EOK;
346}
347
348int netif_initialize(void)
349{
350 sysarg_t phonehash;
351 async_set_interrupt_received(irq_handler);
352 return ipc_connect_to_me(PHONE_NS, SERVICE_DP8390, 0, 0, &phonehash);
353}
354
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 */
361static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
362{
363 /*
364 * Accept the connection
365 * - Answer the first IPC_M_CONNECT_ME_TO call.
366 */
367 ipc_answer_0(iid, EOK);
368
369 while (true) {
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 */
381 int res = netif_module_message(NAME, callid, &call, &answer,
382 &answer_count);
383
384 /* End if said to either by the message or the processing result */
385 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
386 return;
387
388 /* Answer the message */
389 answer_call(callid, res, &answer, answer_count);
390 }
391}
392
393/** Start the module.
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 */
405 return netif_module_start(netif_client_connection);
406}
407
408/** @}
409 */
Note: See TracBrowser for help on using the repository browser.