Rev 4743 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4163 | mejdrech | 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 |
||
4756 | mejdrech | 34 | * DP8390 network interface implementation. |
4163 | mejdrech | 35 | */ |
36 | |||
37 | #include <assert.h> |
||
4327 | mejdrech | 38 | #include <async.h> |
4163 | mejdrech | 39 | #include <ddi.h> |
40 | #include <errno.h> |
||
41 | #include <malloc.h> |
||
42 | //#include <stdio.h> |
||
43 | #include <ipc/ipc.h> |
||
44 | #include <ipc/services.h> |
||
45 | |||
46 | #include "../../err.h" |
||
47 | #include "../../messages.h" |
||
48 | #include "../../modules.h" |
||
49 | |||
50 | #include "../../structures/packet/packet_client.h" |
||
51 | #include "../../structures/measured_strings.h" |
||
52 | |||
4243 | mejdrech | 53 | #include "../../include/device.h" |
4307 | mejdrech | 54 | #include "../../include/nil_interface.h" |
4243 | mejdrech | 55 | |
4192 | mejdrech | 56 | #include "../netif.h" |
4307 | mejdrech | 57 | #include "../netif_module.h" |
4192 | mejdrech | 58 | |
4163 | mejdrech | 59 | #include "dp8390.h" |
60 | #include "dp8390_drv.h" |
||
61 | #include "dp8390_port.h" |
||
62 | |||
4756 | mejdrech | 63 | /** DP8390 module name. |
64 | */ |
||
4163 | mejdrech | 65 | #define NAME "dp8390 network interface" |
66 | |||
4756 | mejdrech | 67 | /** Returns the device from the interrupt call. |
68 | * @param[in] call The interrupt call. |
||
69 | */ |
||
4261 | mejdrech | 70 | #define IRQ_GET_DEVICE( call ) ( device_id_t ) IPC_GET_METHOD( * call ) |
4756 | mejdrech | 71 | |
72 | /** Returns the interrupt status register from the interrupt call. |
||
73 | * @param[in] call The interrupt call. |
||
74 | */ |
||
4163 | mejdrech | 75 | #define IPC_GET_ISR( call ) ( int ) IPC_GET_ARG2( * call ) |
76 | |||
4756 | mejdrech | 77 | /** DP8390 kernel interrupt command sequence. |
78 | */ |
||
4163 | mejdrech | 79 | static irq_cmd_t dp8390_cmds[] = { |
80 | { .cmd = CMD_PIO_READ_8, |
||
81 | .addr = NULL, |
||
82 | .dstarg = 2 |
||
83 | }, |
||
84 | { |
||
85 | .cmd = CMD_PREDICATE, |
||
4327 | mejdrech | 86 | .value = 1, |
4163 | mejdrech | 87 | .srcarg = 2 |
88 | }, |
||
4351 | mejdrech | 89 | { |
4163 | mejdrech | 90 | .cmd = CMD_ACCEPT |
91 | } |
||
92 | }; |
||
93 | |||
4756 | mejdrech | 94 | /** DP8390 kernel interrupt code. |
95 | */ |
||
4163 | mejdrech | 96 | static irq_code_t dp8390_code = { |
97 | sizeof( dp8390_cmds ) / sizeof( irq_cmd_t ), |
||
98 | dp8390_cmds |
||
99 | }; |
||
100 | |||
4756 | mejdrech | 101 | /** Network interface module global data. |
102 | */ |
||
4163 | mejdrech | 103 | netif_globals_t netif_globals; |
104 | |||
4756 | mejdrech | 105 | /** Prints the module name. |
106 | * @see NAME |
||
107 | */ |
||
4307 | mejdrech | 108 | void module_print_name( void ); |
4163 | mejdrech | 109 | |
4756 | mejdrech | 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 | */ |
||
4192 | mejdrech | 115 | void irq_handler( ipc_callid_t iid, ipc_call_t * call ); |
4756 | mejdrech | 116 | |
117 | /** Changes the network interface state. |
||
118 | * @param[in,out] device The network interface. |
||
119 | * @param[in] state The new state. |
||
120 | * @returns The new state. |
||
121 | */ |
||
4261 | mejdrech | 122 | int change_state( device_ref device, device_state_t state ); |
4163 | mejdrech | 123 | |
4243 | mejdrech | 124 | int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
4192 | mejdrech | 125 | return ENOTSUP; |
126 | } |
||
127 | |||
4243 | mejdrech | 128 | int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){ |
4192 | mejdrech | 129 | ERROR_DECLARE; |
130 | |||
131 | device_ref device; |
||
132 | eth_stat_t * de_stat; |
||
133 | |||
134 | if( ! stats ) return EBADMEM; |
||
135 | ERROR_PROPAGATE( find_device( device_id, & device )); |
||
136 | de_stat = & (( dpeth_t * ) device->specific )->de_stat; |
||
137 | null_device_stats( stats ); |
||
4728 | mejdrech | 138 | stats->receive_errors = de_stat->ets_recvErr; |
139 | stats->send_errors = de_stat->ets_sendErr; |
||
140 | stats->receive_crc_errors = de_stat->ets_CRCerr; |
||
141 | stats->receive_frame_errors = de_stat->ets_frameAll; |
||
142 | stats->receive_missed_errors = de_stat->ets_missedP; |
||
143 | stats->receive_packets = de_stat->ets_packetR; |
||
144 | stats->send_packets = de_stat->ets_packetT; |
||
4192 | mejdrech | 145 | stats->collisions = de_stat->ets_collision; |
4728 | mejdrech | 146 | stats->send_aborted_errors = de_stat->ets_transAb; |
147 | stats->send_carrier_errors = de_stat->ets_carrSense; |
||
148 | stats->send_heartbeat_errors = de_stat->ets_CDheartbeat; |
||
149 | stats->send_window_errors = de_stat->ets_OWC; |
||
4192 | mejdrech | 150 | return EOK; |
151 | } |
||
152 | |||
4307 | mejdrech | 153 | void module_print_name( void ){ |
4197 | mejdrech | 154 | printf( "%s", NAME ); |
4163 | mejdrech | 155 | } |
156 | |||
4243 | mejdrech | 157 | int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){ |
4163 | mejdrech | 158 | ERROR_DECLARE; |
159 | |||
160 | device_ref device; |
||
161 | |||
4192 | mejdrech | 162 | if( ! address ) return EBADMEM; |
163 | ERROR_PROPAGATE( find_device( device_id, & device )); |
||
164 | address->value = ( char * ) ( & (( dpeth_t * ) device->specific )->de_address ); |
||
165 | address->length = CONVERT_SIZE( ether_addr_t, char, 1 ); |
||
166 | return EOK; |
||
4163 | mejdrech | 167 | } |
168 | |||
4192 | mejdrech | 169 | void irq_handler( ipc_callid_t iid, ipc_call_t * call ) |
4163 | mejdrech | 170 | { |
171 | device_ref device; |
||
172 | dpeth_t * dep; |
||
4261 | mejdrech | 173 | packet_t received; |
174 | device_id_t device_id; |
||
175 | int phone; |
||
4163 | mejdrech | 176 | |
4261 | mejdrech | 177 | device_id = IRQ_GET_DEVICE( call ); |
4582 | mejdrech | 178 | fibril_rwlock_write_lock( & netif_globals.lock ); |
4261 | mejdrech | 179 | if( find_device( device_id, & device ) != EOK ){ |
4582 | mejdrech | 180 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4261 | mejdrech | 181 | return; |
182 | } |
||
4192 | mejdrech | 183 | dep = ( dpeth_t * ) device->specific; |
4261 | mejdrech | 184 | if ( dep->de_mode != DEM_ENABLED){ |
4582 | mejdrech | 185 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4192 | mejdrech | 186 | return; |
4261 | mejdrech | 187 | } |
4192 | mejdrech | 188 | assert( dep->de_flags & DEF_ENABLED); |
4498 | mejdrech | 189 | dep->de_int_pending = 0; |
4743 | mejdrech | 190 | // remove debug print: |
191 | // printf( "I%d: 0x%x\n", device_id, IPC_GET_ISR( call )); |
||
4332 | mejdrech | 192 | dp_check_ints( dep, IPC_GET_ISR( call )); |
4261 | mejdrech | 193 | if( dep->received_queue ){ |
194 | received = dep->received_queue; |
||
195 | phone = device->nil_phone; |
||
196 | dep->received_queue = NULL; |
||
4743 | mejdrech | 197 | dep->received_count = 0; |
4582 | mejdrech | 198 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4743 | mejdrech | 199 | // remove debug dump: |
200 | // uint8_t * data; |
||
201 | // data = packet_get_data( received ); |
||
202 | // 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 ] ); |
||
4261 | mejdrech | 203 | nil_received_msg( phone, device_id, received, NULL ); |
204 | }else{ |
||
4582 | mejdrech | 205 | fibril_rwlock_write_unlock( & netif_globals.lock ); |
4261 | mejdrech | 206 | } |
4243 | mejdrech | 207 | ipc_answer_0( iid, EOK ); |
4163 | mejdrech | 208 | } |
209 | |||
4243 | mejdrech | 210 | int netif_probe_message( device_id_t device_id, int irq, int io ){ |
4163 | mejdrech | 211 | ERROR_DECLARE; |
212 | |||
4192 | mejdrech | 213 | device_ref device; |
214 | dpeth_t * dep; |
||
4163 | mejdrech | 215 | |
216 | device = ( device_ref ) malloc( sizeof( device_t )); |
||
217 | if( ! device ) return ENOMEM; |
||
4192 | mejdrech | 218 | dep = ( dpeth_t * ) malloc( sizeof( dpeth_t )); |
219 | if( ! dep ){ |
||
4163 | mejdrech | 220 | free( device ); |
221 | return ENOMEM; |
||
222 | } |
||
223 | bzero( device, sizeof( device_t )); |
||
4192 | mejdrech | 224 | bzero( dep, sizeof( dpeth_t )); |
4163 | mejdrech | 225 | device->device_id = device_id; |
226 | device->nil_phone = -1; |
||
4192 | mejdrech | 227 | device->specific = ( void * ) dep; |
228 | device->state = NETIF_STOPPED; |
||
229 | dep->de_irq = irq; |
||
230 | dep->de_mode = DEM_DISABLED; |
||
4163 | mejdrech | 231 | //TODO address? |
4192 | mejdrech | 232 | if( ERROR_OCCURRED( pio_enable(( void * ) io, DP8390_IO_SIZE, ( void ** ) & dep->de_base_port )) |
233 | || ERROR_OCCURRED( do_probe( dep ))){ |
||
234 | free( dep ); |
||
4163 | mejdrech | 235 | free( device ); |
236 | return ERROR_CODE; |
||
237 | } |
||
238 | if( ERROR_OCCURRED( device_map_add( & netif_globals.device_map, device->device_id, device ))){ |
||
4192 | mejdrech | 239 | free( dep ); |
4163 | mejdrech | 240 | free( device ); |
241 | return ERROR_CODE; |
||
242 | } |
||
243 | return EOK; |
||
244 | } |
||
245 | |||
4261 | mejdrech | 246 | int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){ |
4192 | mejdrech | 247 | ERROR_DECLARE; |
248 | |||
249 | device_ref device; |
||
250 | dpeth_t * dep; |
||
251 | packet_t next; |
||
252 | |||
4327 | mejdrech | 253 | ERROR_PROPAGATE( find_device( device_id, & device )); |
4731 | mejdrech | 254 | if( device->state != NETIF_ACTIVE ){ |
255 | netif_pq_release( packet_get_id( packet )); |
||
256 | return EFORWARD; |
||
257 | } |
||
4327 | mejdrech | 258 | dep = ( dpeth_t * ) device->specific; |
4192 | mejdrech | 259 | // process packet queue |
260 | do{ |
||
261 | next = pq_detach( packet ); |
||
4743 | mejdrech | 262 | // remove debug dump: |
263 | // uint8_t * data; |
||
264 | // data = packet_get_data( packet ); |
||
265 | // 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 ] ); |
||
4498 | mejdrech | 266 | |
4192 | mejdrech | 267 | if( do_pwrite( dep, packet, FALSE ) != EBUSY ){ |
268 | netif_pq_release( packet_get_id( packet )); |
||
269 | } |
||
270 | packet = next; |
||
271 | }while( packet ); |
||
272 | return EOK; |
||
4163 | mejdrech | 273 | } |
274 | |||
4261 | mejdrech | 275 | int netif_start_message( device_ref device ){ |
4163 | mejdrech | 276 | ERROR_DECLARE; |
277 | |||
278 | dpeth_t * dep; |
||
279 | |||
280 | if( device->state != NETIF_ACTIVE ){ |
||
4192 | mejdrech | 281 | dep = ( dpeth_t * ) device->specific; |
4163 | mejdrech | 282 | dp8390_cmds[ 0 ].addr = ( void * ) ( uint32_t ) ( dep->de_dp8390_port + DP_ISR ); |
4192 | mejdrech | 283 | dp8390_cmds[ 2 ].addr = dp8390_cmds[ 0 ].addr; |
284 | ERROR_PROPAGATE( ipc_register_irq( dep->de_irq, device->device_id, device->device_id, & dp8390_code )); |
||
4163 | mejdrech | 285 | if( ERROR_OCCURRED( do_init( dep, DL_BROAD_REQ ))){ |
286 | ipc_unregister_irq( dep->de_irq, device->device_id ); |
||
287 | return ERROR_CODE; |
||
288 | } |
||
4261 | mejdrech | 289 | return change_state( device, NETIF_ACTIVE ); |
4163 | mejdrech | 290 | } |
291 | return EOK; |
||
292 | } |
||
293 | |||
4261 | mejdrech | 294 | int netif_stop_message( device_ref device ){ |
4163 | mejdrech | 295 | dpeth_t * dep; |
296 | |||
297 | if( device->state != NETIF_STOPPED ){ |
||
4192 | mejdrech | 298 | dep = ( dpeth_t * ) device->specific; |
4163 | mejdrech | 299 | do_stop( dep ); |
300 | ipc_unregister_irq( dep->de_irq, device->device_id ); |
||
4261 | mejdrech | 301 | return change_state( device, NETIF_STOPPED ); |
4163 | mejdrech | 302 | } |
303 | return EOK; |
||
304 | } |
||
305 | |||
4261 | mejdrech | 306 | int change_state( device_ref device, device_state_t state ){ |
4192 | mejdrech | 307 | device->state = state; |
4307 | mejdrech | 308 | printf( "State changed to %s\n", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" ); |
4261 | mejdrech | 309 | return state; |
4192 | mejdrech | 310 | } |
311 | |||
4243 | mejdrech | 312 | int netif_initialize( void ){ |
4163 | mejdrech | 313 | ipcarg_t phonehash; |
314 | |||
315 | async_set_interrupt_received( irq_handler ); |
||
316 | |||
317 | return REGISTER_ME( SERVICE_DP8390, & phonehash ); |
||
318 | } |
||
319 | |||
320 | /** @} |
||
321 | */ |