source: mainline/uspace/srv/hw/netif/dp8390/dp8390_module.c@ 7922dea

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7922dea was 66b628a, checked in by martin@…>, 15 years ago

further code simplification

  • Property mode set to 100644
File size: 9.1 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_A_8,
95 .addr = NULL,
96 .srcarg = 3
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 device_id_t device_id = IRQ_GET_DEVICE(*call);
118 netif_device_t *device;
119 int nil_phone;
120 dpeth_t *dep;
121
122 fibril_rwlock_write_lock(&netif_globals.lock);
123
124 if (find_device(device_id, &device) == EOK) {
125 nil_phone = device->nil_phone;
126 dep = (dpeth_t *) device->specific;
127 } else
128 dep = NULL;
129
130 fibril_rwlock_write_unlock(&netif_globals.lock);
131
132 if ((dep != NULL) && (dep->up)) {
133 assert(dep->enabled);
134 dp_check_ints(nil_phone, device_id, dep, IRQ_GET_ISR(*call));
135 }
136}
137
138/** Changes the network interface state.
139 * @param[in,out] device The network interface.
140 * @param[in] state The new state.
141 * @returns The new state.
142 */
143static int change_state(netif_device_t *device, device_state_t state)
144{
145 if (device->state != state) {
146 device->state = state;
147
148 printf("%s: State changed to %s\n", NAME,
149 (state == NETIF_ACTIVE) ? "active" : "stopped");
150
151 return state;
152 }
153
154 return EOK;
155}
156
157int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
158 ipc_call_t *answer, int *answer_count)
159{
160 return ENOTSUP;
161}
162
163int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
164{
165 netif_device_t * device;
166 eth_stat_t * de_stat;
167 int rc;
168
169 if (!stats)
170 return EBADMEM;
171
172 rc = find_device(device_id, &device);
173 if (rc != EOK)
174 return rc;
175
176 de_stat = &((dpeth_t *) device->specific)->de_stat;
177
178 null_device_stats(stats);
179 stats->receive_errors = de_stat->ets_recvErr;
180 stats->send_errors = de_stat->ets_sendErr;
181 stats->receive_crc_errors = de_stat->ets_CRCerr;
182 stats->receive_frame_errors = de_stat->ets_frameAll;
183 stats->receive_missed_errors = de_stat->ets_missedP;
184 stats->receive_packets = de_stat->ets_packetR;
185 stats->send_packets = de_stat->ets_packetT;
186 stats->collisions = de_stat->ets_collision;
187 stats->send_aborted_errors = de_stat->ets_transAb;
188 stats->send_carrier_errors = de_stat->ets_carrSense;
189 stats->send_heartbeat_errors = de_stat->ets_CDheartbeat;
190 stats->send_window_errors = de_stat->ets_OWC;
191
192 return EOK;
193}
194
195int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
196{
197 netif_device_t *device;
198 int rc;
199
200 if (!address)
201 return EBADMEM;
202
203 rc = find_device(device_id, &device);
204 if (rc != EOK)
205 return rc;
206
207 address->value = (char *) (&((dpeth_t *) device->specific)->de_address);
208 address->length = sizeof(ether_addr_t);
209 return EOK;
210}
211
212int netif_probe_message(device_id_t device_id, int irq, uintptr_t io)
213{
214 netif_device_t *device;
215 dpeth_t *dep;
216 int rc;
217
218 device = (netif_device_t *) malloc(sizeof(netif_device_t));
219 if (!device)
220 return ENOMEM;
221
222 dep = (dpeth_t *) malloc(sizeof(dpeth_t));
223 if (!dep) {
224 free(device);
225 return ENOMEM;
226 }
227
228 bzero(device, sizeof(netif_device_t));
229 bzero(dep, sizeof(dpeth_t));
230 device->device_id = device_id;
231 device->nil_phone = -1;
232 device->specific = (void *) dep;
233 device->state = NETIF_STOPPED;
234 dep->de_irq = irq;
235 dep->up = false;
236
237 //TODO address?
238 rc = pio_enable((void *) io, DP8390_IO_SIZE, (void **) &dep->de_base_port);
239 if (rc != EOK) {
240 free(dep);
241 free(device);
242 return rc;
243 }
244
245 rc = do_probe(dep);
246 if (rc != EOK) {
247 free(dep);
248 free(device);
249 return rc;
250 }
251
252 rc = netif_device_map_add(&netif_globals.device_map, device->device_id, device);
253 if (rc != EOK) {
254 free(dep);
255 free(device);
256 return rc;
257 }
258
259 return EOK;
260}
261
262int netif_send_message(device_id_t device_id, packet_t *packet,
263 services_t sender)
264{
265 netif_device_t *device;
266 dpeth_t *dep;
267 packet_t *next;
268 int rc;
269
270 rc = find_device(device_id, &device);
271 if (rc != EOK)
272 return rc;
273
274 if (device->state != NETIF_ACTIVE){
275 netif_pq_release(packet_get_id(packet));
276 return EFORWARD;
277 }
278
279 dep = (dpeth_t *) device->specific;
280
281 /* Process packet queue */
282 do {
283 next = pq_detach(packet);
284
285 if (do_pwrite(dep, packet, false) != EBUSY)
286 netif_pq_release(packet_get_id(packet));
287
288 packet = next;
289 } while (packet);
290
291 return EOK;
292}
293
294int netif_start_message(netif_device_t * device)
295{
296 dpeth_t *dep;
297 int rc;
298
299 if (device->state != NETIF_ACTIVE) {
300 dep = (dpeth_t *) device->specific;
301 dp8390_cmds[0].addr = (void *) (uintptr_t) (dep->de_dp8390_port + DP_ISR);
302 dp8390_cmds[3].addr = dp8390_cmds[0].addr;
303
304 rc = ipc_register_irq(dep->de_irq, device->device_id, device->device_id, &dp8390_code);
305 if (rc != EOK)
306 return rc;
307
308 rc = do_init(dep);
309 if (rc != EOK) {
310 ipc_unregister_irq(dep->de_irq, device->device_id);
311 return rc;
312 }
313
314 return change_state(device, NETIF_ACTIVE);
315 }
316
317 return EOK;
318}
319
320int netif_stop_message(netif_device_t * device)
321{
322 dpeth_t *dep;
323
324 if (device->state != NETIF_STOPPED) {
325 dep = (dpeth_t *) device->specific;
326 do_stop(dep);
327 ipc_unregister_irq(dep->de_irq, device->device_id);
328 return change_state(device, NETIF_STOPPED);
329 }
330
331 return EOK;
332}
333
334int netif_initialize(void)
335{
336 sysarg_t phonehash;
337 async_set_interrupt_received(irq_handler);
338 return ipc_connect_to_me(PHONE_NS, SERVICE_DP8390, 0, 0, &phonehash);
339}
340
341/** Default thread for new connections.
342 *
343 * @param[in] iid The initial message identifier.
344 * @param[in] icall The initial message call structure.
345 *
346 */
347static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
348{
349 /*
350 * Accept the connection
351 * - Answer the first IPC_M_CONNECT_ME_TO call.
352 */
353 ipc_answer_0(iid, EOK);
354
355 while (true) {
356 ipc_call_t answer;
357 int answer_count;
358
359 /* Clear the answer structure */
360 refresh_answer(&answer, &answer_count);
361
362 /* Fetch the next message */
363 ipc_call_t call;
364 ipc_callid_t callid = async_get_call(&call);
365
366 /* Process the message */
367 int res = netif_module_message(NAME, callid, &call, &answer,
368 &answer_count);
369
370 /* End if said to either by the message or the processing result */
371 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
372 return;
373
374 /* Answer the message */
375 answer_call(callid, res, &answer, answer_count);
376 }
377}
378
379/** Start the module.
380 *
381 * @param argc The count of the command line arguments. Ignored parameter.
382 * @param argv The command line parameters. Ignored parameter.
383 *
384 * @returns EOK on success.
385 * @returns Other error codes as defined for each specific module start function.
386 *
387 */
388int main(int argc, char *argv[])
389{
390 /* Start the module */
391 return netif_module_start(netif_client_connection);
392}
393
394/** @}
395 */
Note: See TracBrowser for help on using the repository browser.