source: mainline/uspace/srv/hw/netif/dp8390/dp8390_module.c@ 849ed54

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

Networking work:
Split the networking stack into end-user library (libsocket) and two helper libraries (libnet and libnetif).
Don't use over-the-hand compiling and linking, but rather separation of conserns.
There might be still some issues and the non-modular networking architecture is currently broken, but this will be fixed soon.

  • Property mode set to 100644
File size: 12.2 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 <malloc.h>
42#include <ipc/ipc.h>
43#include <ipc/services.h>
44
45#include <net_err.h>
46#include <net_messages.h>
47#include <net_modules.h>
48#include <packet/packet_client.h>
49#include <adt/measured_strings.h>
50#include <net_device.h>
51#include <nil_interface.h>
52#include <netif.h>
53#include <netif_module.h>
54
55#include "dp8390.h"
56#include "dp8390_drv.h"
57#include "dp8390_port.h"
58
59/** DP8390 module name.
60 */
61#define NAME "dp8390 network interface"
62
63/** Returns the device from the interrupt call.
64 * @param[in] call The interrupt call.
65 */
66#define IRQ_GET_DEVICE(call) (device_id_t) IPC_GET_METHOD(*call)
67
68/** Returns the interrupt status register from the interrupt call.
69 * @param[in] call The interrupt call.
70 */
71#define IPC_GET_ISR(call) (int) IPC_GET_ARG2(*call)
72
73/** DP8390 kernel interrupt command sequence.
74 */
75static irq_cmd_t dp8390_cmds[] = {
76 { .cmd = CMD_PIO_READ_8,
77 .addr = NULL,
78 .dstarg = 2
79 },
80 {
81 .cmd = CMD_PREDICATE,
82 .value = 1,
83 .srcarg = 2
84 },
85 {
86 .cmd = CMD_ACCEPT
87 }
88};
89
90/** DP8390 kernel interrupt code.
91 */
92static irq_code_t dp8390_code = {
93 sizeof(dp8390_cmds) / sizeof(irq_cmd_t),
94 dp8390_cmds
95};
96
97/** Network interface module global data.
98 */
99netif_globals_t netif_globals;
100
101/** Handles the interrupt messages.
102 * This is the interrupt handler callback function.
103 * @param[in] iid The interrupt message identifier.
104 * @param[in] call The interrupt message.
105 */
106void irq_handler(ipc_callid_t iid, ipc_call_t * call);
107
108/** Changes the network interface state.
109 * @param[in,out] device The network interface.
110 * @param[in] state The new state.
111 * @returns The new state.
112 */
113int change_state(device_ref device, device_state_t state);
114
115int netif_specific_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
116 return ENOTSUP;
117}
118
119int netif_get_device_stats(device_id_t device_id, device_stats_ref stats){
120 ERROR_DECLARE;
121
122 device_ref device;
123 eth_stat_t * de_stat;
124
125 if(! stats){
126 return EBADMEM;
127 }
128 ERROR_PROPAGATE(find_device(device_id, &device));
129 de_stat = &((dpeth_t *) device->specific)->de_stat;
130 null_device_stats(stats);
131 stats->receive_errors = de_stat->ets_recvErr;
132 stats->send_errors = de_stat->ets_sendErr;
133 stats->receive_crc_errors = de_stat->ets_CRCerr;
134 stats->receive_frame_errors = de_stat->ets_frameAll;
135 stats->receive_missed_errors = de_stat->ets_missedP;
136 stats->receive_packets = de_stat->ets_packetR;
137 stats->send_packets = de_stat->ets_packetT;
138 stats->collisions = de_stat->ets_collision;
139 stats->send_aborted_errors = de_stat->ets_transAb;
140 stats->send_carrier_errors = de_stat->ets_carrSense;
141 stats->send_heartbeat_errors = de_stat->ets_CDheartbeat;
142 stats->send_window_errors = de_stat->ets_OWC;
143 return EOK;
144}
145
146int netif_get_addr_message(device_id_t device_id, measured_string_ref address){
147 ERROR_DECLARE;
148
149 device_ref device;
150
151 if(! address){
152 return EBADMEM;
153 }
154 ERROR_PROPAGATE(find_device(device_id, &device));
155 address->value = (char *) (&((dpeth_t *) device->specific)->de_address);
156 address->length = CONVERT_SIZE(ether_addr_t, char, 1);
157 return EOK;
158}
159
160void irq_handler(ipc_callid_t iid, ipc_call_t * call)
161{
162 device_ref device;
163 dpeth_t * dep;
164 packet_t received;
165 device_id_t device_id;
166 int phone;
167
168 device_id = IRQ_GET_DEVICE(call);
169 fibril_rwlock_write_lock(&netif_globals.lock);
170 if(find_device(device_id, &device) != EOK){
171 fibril_rwlock_write_unlock(&netif_globals.lock);
172 return;
173 }
174 dep = (dpeth_t *) device->specific;
175 if (dep->de_mode != DEM_ENABLED){
176 fibril_rwlock_write_unlock(&netif_globals.lock);
177 return;
178 }
179 assert(dep->de_flags &DEF_ENABLED);
180 dep->de_int_pending = 0;
181// remove debug print:
182#ifdef CONFIG_DEBUG
183 printf("I%d: 0x%x\n", device_id, IPC_GET_ISR(call));
184#endif
185 dp_check_ints(dep, IPC_GET_ISR(call));
186 if(dep->received_queue){
187 received = dep->received_queue;
188 phone = device->nil_phone;
189 dep->received_queue = NULL;
190 dep->received_count = 0;
191 fibril_rwlock_write_unlock(&netif_globals.lock);
192// remove debug dump:
193#ifdef CONFIG_DEBUG
194 uint8_t * data;
195 data = packet_get_data(received);
196 printf("Receiving packet:\n\tid\t= %d\n\tlength\t= %d\n\tdata\t= %.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX\n\t\t%.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX\n", packet_get_id(received), packet_get_data_length(received), data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15], data[16], data[17], data[18], data[19], data[20], data[21], data[22], data[23], data[24], data[25], data[26], data[27], data[28], data[29], data[30], data[31], data[32], data[33], data[34], data[35], data[36], data[37], data[38], data[39], data[40], data[41], data[42], data[43], data[44], data[45], data[46], data[47], data[48], data[49], data[50], data[51], data[52], data[53], data[54], data[55], data[56], data[57], data[58], data[59]);
197#endif
198 nil_received_msg(phone, device_id, received, NULL);
199 }else{
200 fibril_rwlock_write_unlock(&netif_globals.lock);
201 }
202 ipc_answer_0(iid, EOK);
203}
204
205int netif_probe_message(device_id_t device_id, int irq, uintptr_t io){
206 ERROR_DECLARE;
207
208 device_ref device;
209 dpeth_t * dep;
210
211 device = (device_ref) malloc(sizeof(device_t));
212 if(! device){
213 return ENOMEM;
214 }
215 dep = (dpeth_t *) malloc(sizeof(dpeth_t));
216 if(! dep){
217 free(device);
218 return ENOMEM;
219 }
220 bzero(device, sizeof(device_t));
221 bzero(dep, sizeof(dpeth_t));
222 device->device_id = device_id;
223 device->nil_phone = -1;
224 device->specific = (void *) dep;
225 device->state = NETIF_STOPPED;
226 dep->de_irq = irq;
227 dep->de_mode = DEM_DISABLED;
228 //TODO address?
229 if(ERROR_OCCURRED(pio_enable((void *) io, DP8390_IO_SIZE, (void **) &dep->de_base_port))
230 || ERROR_OCCURRED(do_probe(dep))){
231 free(dep);
232 free(device);
233 return ERROR_CODE;
234 }
235 if(ERROR_OCCURRED(device_map_add(&netif_globals.device_map, device->device_id, device))){
236 free(dep);
237 free(device);
238 return ERROR_CODE;
239 }
240 return EOK;
241}
242
243int netif_send_message(device_id_t device_id, packet_t packet, services_t sender){
244 ERROR_DECLARE;
245
246 device_ref device;
247 dpeth_t * dep;
248 packet_t next;
249
250 ERROR_PROPAGATE(find_device(device_id, &device));
251 if(device->state != NETIF_ACTIVE){
252 netif_pq_release(packet_get_id(packet));
253 return EFORWARD;
254 }
255 dep = (dpeth_t *) device->specific;
256 // process packet queue
257 do{
258 next = pq_detach(packet);
259// remove debug dump:
260#ifdef CONFIG_DEBUG
261 uint8_t * data;
262 data = packet_get_data(packet);
263 printf("Sending packet:\n\tid\t= %d\n\tlength\t= %d\n\tdata\t= %.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX\n\t\t%.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX\n", packet_get_id(packet), packet_get_data_length(packet), data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15], data[16], data[17], data[18], data[19], data[20], data[21], data[22], data[23], data[24], data[25], data[26], data[27], data[28], data[29], data[30], data[31], data[32], data[33], data[34], data[35], data[36], data[37], data[38], data[39], data[40], data[41], data[42], data[43], data[44], data[45], data[46], data[47], data[48], data[49], data[50], data[51], data[52], data[53], data[54], data[55], data[56], data[57], data[58], data[59]);
264#endif
265 if(do_pwrite(dep, packet, FALSE) != EBUSY){
266 netif_pq_release(packet_get_id(packet));
267 }
268 packet = next;
269 }while(packet);
270 return EOK;
271}
272
273int netif_start_message(device_ref device){
274 ERROR_DECLARE;
275
276 dpeth_t * dep;
277
278 if(device->state != NETIF_ACTIVE){
279 dep = (dpeth_t *) device->specific;
280 dp8390_cmds[0].addr = (void *) (uintptr_t) (dep->de_dp8390_port + DP_ISR);
281 dp8390_cmds[2].addr = dp8390_cmds[0].addr;
282 ERROR_PROPAGATE(ipc_register_irq(dep->de_irq, device->device_id, device->device_id, &dp8390_code));
283 if(ERROR_OCCURRED(do_init(dep, DL_BROAD_REQ))){
284 ipc_unregister_irq(dep->de_irq, device->device_id);
285 return ERROR_CODE;
286 }
287 return change_state(device, NETIF_ACTIVE);
288 }
289 return EOK;
290}
291
292int netif_stop_message(device_ref device){
293 dpeth_t * dep;
294
295 if(device->state != NETIF_STOPPED){
296 dep = (dpeth_t *) device->specific;
297 do_stop(dep);
298 ipc_unregister_irq(dep->de_irq, device->device_id);
299 return change_state(device, NETIF_STOPPED);
300 }
301 return EOK;
302}
303
304int change_state(device_ref device, device_state_t state){
305 device->state = state;
306 printf("State changed to %s\n", (state == NETIF_ACTIVE) ? "ACTIVE" : "STOPPED");
307 return state;
308}
309
310int netif_initialize(void){
311 ipcarg_t phonehash;
312
313 async_set_interrupt_received(irq_handler);
314
315 return REGISTER_ME(SERVICE_DP8390, &phonehash);
316}
317
318#ifdef CONFIG_NETWORKING_modular
319
320#include <netif_standalone.h>
321
322/** Default thread for new connections.
323 *
324 * @param[in] iid The initial message identifier.
325 * @param[in] icall The initial message call structure.
326 *
327 */
328static void netif_client_connection(ipc_callid_t iid, ipc_call_t * icall)
329{
330 /*
331 * Accept the connection
332 * - Answer the first IPC_M_CONNECT_ME_TO call.
333 */
334 ipc_answer_0(iid, EOK);
335
336 while(true) {
337 ipc_call_t answer;
338 int answer_count;
339
340 /* Clear the answer structure */
341 refresh_answer(&answer, &answer_count);
342
343 /* Fetch the next message */
344 ipc_call_t call;
345 ipc_callid_t callid = async_get_call(&call);
346
347 /* Process the message */
348 int res = netif_module_message(callid, &call, &answer, &answer_count);
349
350 /* End if said to either by the message or the processing result */
351 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
352 return;
353
354 /* Answer the message */
355 answer_call(callid, res, &answer, answer_count);
356 }
357}
358
359/** Starts the module.
360 *
361 * @param argc The count of the command line arguments. Ignored parameter.
362 * @param argv The command line parameters. Ignored parameter.
363 *
364 * @returns EOK on success.
365 * @returns Other error codes as defined for each specific module start function.
366 *
367 */
368int main(int argc, char *argv[])
369{
370 ERROR_DECLARE;
371
372 /* Print the module label */
373 printf("Task %d - %s\n", task_get_id(), NAME);
374
375 /* Start the module */
376 if (ERROR_OCCURRED(netif_module_start(netif_client_connection))) {
377 printf(" - ERROR %i\n", ERROR_CODE);
378 return ERROR_CODE;
379 }
380
381 return EOK;
382}
383
384#endif /* CONFIG_NETWORKING_modular */
385
386
387/** @}
388 */
Note: See TracBrowser for help on using the repository browser.