source: mainline/uspace/srv/hw/netif/ne2000/ne2000.c@ 6d100fd

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

undo development effort which was neither finished nor properly debugged yet
(this unbreaks networking once again)

  • Property mode set to 100644
File size: 9.1 KB
Line 
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
30/** @addtogroup ne2000
31 * @{
32 */
33
34/** @file
35 * NE2000 network interface implementation.
36 */
37
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 <ns.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>
53#include <nil_remote.h>
54#include "dp8390.h"
55
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))
62
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))
69
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
77static bool irc_service = false;
78static async_sess_t *irc_sess = NULL;
79
80/** NE2000 kernel interrupt command sequence.
81 *
82 */
83static irq_cmd_t ne2k_cmds[] = {
84 {
85 /* Read Interrupt Status Register */
86 .cmd = CMD_PIO_READ_8,
87 .addr = NULL,
88 .dstarg = 2
89 },
90 {
91 /* Mask supported interrupt causes */
92 .cmd = CMD_BTEST,
93 .value = (ISR_PRX | ISR_PTX | ISR_RXE | ISR_TXE | ISR_OVW |
94 ISR_CNT | ISR_RDC),
95 .srcarg = 2,
96 .dstarg = 3,
97 },
98 {
99 /* Predicate for accepting the interrupt */
100 .cmd = CMD_PREDICATE,
101 .value = 4,
102 .srcarg = 3
103 },
104 {
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 */
115 .cmd = CMD_PIO_WRITE_A_8,
116 .addr = NULL,
117 .srcarg = 3
118 },
119 {
120 /* Read Transmit Status Register */
121 .cmd = CMD_PIO_READ_8,
122 .addr = NULL,
123 .dstarg = 3
124 },
125 {
126 .cmd = CMD_ACCEPT
127 }
128};
129
130/** NE2000 kernel interrupt code.
131 *
132 */
133static irq_code_t ne2k_code = {
134 sizeof(ne2k_cmds) / sizeof(irq_cmd_t),
135 ne2k_cmds
136};
137
138/** Handle the interrupt notification.
139 *
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).
145 *
146 * @param[in] iid Interrupt notification identifier.
147 * @param[in] call Interrupt notification.
148 *
149 */
150static void irq_handler(ipc_callid_t iid, ipc_call_t *call)
151{
152 device_id_t device_id = IRQ_GET_DEVICE(*call);
153 netif_device_t *device;
154 async_sess_t *nil_sess;
155 ne2k_t *ne2k;
156
157 fibril_rwlock_read_lock(&netif_globals.lock);
158
159 nil_sess = netif_globals.nil_sess;
160
161 if (find_device(device_id, &device) == EOK)
162 ne2k = (ne2k_t *) device->specific;
163 else
164 ne2k = NULL;
165
166 fibril_rwlock_read_unlock(&netif_globals.lock);
167
168 if (ne2k != NULL) {
169 list_t *frames =
170 ne2k_interrupt(ne2k, IRQ_GET_ISR(*call), IRQ_GET_TSR(*call));
171
172 if (frames != NULL) {
173 while (!list_empty(frames)) {
174 frame_t *frame = list_get_instance(
175 list_first(frames), frame_t, link);
176
177 list_remove(&frame->link);
178 nil_received_msg(nil_sess, device_id, frame->packet,
179 SERVICE_NONE);
180 free(frame);
181 }
182
183 free(frames);
184 }
185 }
186}
187
188/** Change the network interface state.
189 *
190 * @param[in,out] device Network interface.
191 * @param[in] state New state.
192 *
193 */
194static void change_state(netif_device_t *device, device_state_t state)
195{
196 if (device->state != state) {
197 device->state = state;
198
199 const char *desc;
200 switch (state) {
201 case NETIF_ACTIVE:
202 desc = "active";
203 break;
204 case NETIF_STOPPED:
205 desc = "stopped";
206 break;
207 default:
208 desc = "unknown";
209 }
210
211 printf("%s: State changed to %s\n", NAME, desc);
212 }
213}
214
215int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
216 ipc_call_t *answer, size_t *count)
217{
218 return ENOTSUP;
219}
220
221int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
222{
223 if (!stats)
224 return EBADMEM;
225
226 netif_device_t *device;
227 int rc = find_device(device_id, &device);
228 if (rc != EOK)
229 return rc;
230
231 ne2k_t *ne2k = (ne2k_t *) device->specific;
232
233 memcpy(stats, &ne2k->stats, sizeof(device_stats_t));
234 return EOK;
235}
236
237int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
238{
239 if (!address)
240 return EBADMEM;
241
242 netif_device_t *device;
243 int rc = find_device(device_id, &device);
244 if (rc != EOK)
245 return rc;
246
247 ne2k_t *ne2k = (ne2k_t *) device->specific;
248
249 address->value = ne2k->mac;
250 address->length = ETH_ADDR;
251 return EOK;
252}
253
254int netif_probe_message(device_id_t device_id, int irq, void *io)
255{
256 netif_device_t *device =
257 (netif_device_t *) malloc(sizeof(netif_device_t));
258 if (!device)
259 return ENOMEM;
260
261 ne2k_t *ne2k = (ne2k_t *) malloc(sizeof(ne2k_t));
262 if (!ne2k) {
263 free(device);
264 return ENOMEM;
265 }
266
267 void *port;
268 int rc = pio_enable((void *) io, NE2K_IO_SIZE, &port);
269 if (rc != EOK) {
270 free(ne2k);
271 free(device);
272 return rc;
273 }
274
275 bzero(device, sizeof(netif_device_t));
276 bzero(ne2k, sizeof(ne2k_t));
277
278 device->device_id = device_id;
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;
289 }
290
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;
296 }
297
298 printf("%s: Ethernet card at I/O address %p, IRQ %d, MAC ",
299 NAME, port, irq);
300
301 unsigned int i;
302 for (i = 0; i < ETH_ADDR; i++)
303 printf("%02x%c", ne2k->mac[i], i < 5 ? ':' : '\n');
304
305 return EOK;
306}
307
308int netif_start_message(netif_device_t *device)
309{
310 if (device->state != NETIF_ACTIVE) {
311 ne2k_t *ne2k = (ne2k_t *) device->specific;
312
313 ne2k_cmds[0].addr = ne2k->port + DP_ISR;
314 ne2k_cmds[3].addr = ne2k->port + DP_IMR;
315 ne2k_cmds[4].addr = ne2k_cmds[0].addr;
316 ne2k_cmds[5].addr = ne2k->port + DP_TSR;
317
318 int rc = 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 unregister_irq(ne2k->irq, device->device_id);
326 return rc;
327 }
328
329 change_state(device, NETIF_ACTIVE);
330
331 if (irc_service) {
332 async_exch_t *exch = async_exchange_begin(irc_sess);
333 async_msg_1(exch, IRC_ENABLE_INTERRUPT, ne2k->irq);
334 async_exchange_end(exch);
335 }
336 }
337
338 return device->state;
339}
340
341int netif_stop_message(netif_device_t *device)
342{
343 if (device->state != NETIF_STOPPED) {
344 ne2k_t *ne2k = (ne2k_t *) device->specific;
345
346 ne2k_down(ne2k);
347 unregister_irq(ne2k->irq, device->device_id);
348 change_state(device, NETIF_STOPPED);
349 }
350
351 return device->state;
352}
353
354int netif_send_message(device_id_t device_id, packet_t *packet,
355 services_t sender)
356{
357 netif_device_t *device;
358 int rc = find_device(device_id, &device);
359 if (rc != EOK)
360 return rc;
361
362 if (device->state != NETIF_ACTIVE) {
363 netif_pq_release(packet_get_id(packet));
364 return EFORWARD;
365 }
366
367 ne2k_t *ne2k = (ne2k_t *) device->specific;
368
369 /*
370 * Process the packet queue
371 */
372
373 do {
374 packet_t *next = pq_detach(packet);
375 ne2k_send(ne2k, packet);
376 netif_pq_release(packet_get_id(packet));
377 packet = next;
378 } while (packet);
379
380 return EOK;
381}
382
383int netif_initialize(void)
384{
385 sysarg_t apic;
386 sysarg_t i8259;
387
388 if (((sysinfo_get_value("apic", &apic) == EOK) && (apic))
389 || ((sysinfo_get_value("i8259", &i8259) == EOK) && (i8259)))
390 irc_service = true;
391
392 if (irc_service) {
393 while (!irc_sess)
394 irc_sess = service_connect_blocking(EXCHANGE_SERIALIZE,
395 SERVICE_IRC, 0, 0);
396 }
397
398 async_set_interrupt_received(irq_handler);
399
400 return service_register(SERVICE_NE2000);
401}
402
403int main(int argc, char *argv[])
404{
405 /* Start the module */
406 return netif_module_start();
407}
408
409/** @}
410 */
Note: See TracBrowser for help on using the repository browser.