source: mainline/uspace/srv/hw/netif/dp8390/dp8390_module.c@ 61bfc370

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