Subversion Repositories HelenOS

Rev

Rev 4192 | Rev 4243 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4192 Rev 4197
1
/*
1
/*
2
 * Copyright (c) 2009 Lukas Mejdrech
2
 * Copyright (c) 2009 Lukas Mejdrech
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup dp8390
29
/** @addtogroup dp8390
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <assert.h>
36
#include <assert.h>
37
#include <ddi.h>
37
#include <ddi.h>
38
#include <errno.h>
38
#include <errno.h>
39
#include <malloc.h>
39
#include <malloc.h>
40
//#include <stdio.h>
40
//#include <stdio.h>
41
#include <ipc/ipc.h>
41
#include <ipc/ipc.h>
42
#include <ipc/services.h>
42
#include <ipc/services.h>
43
 
43
 
44
#include "../../err.h"
44
#include "../../err.h"
45
#include "../../messages.h"
45
#include "../../messages.h"
46
#include "../../modules.h"
46
#include "../../modules.h"
47
 
47
 
48
#include "../../structures/packet/packet_client.h"
48
#include "../../structures/packet/packet_client.h"
49
#include "../../structures/measured_strings.h"
49
#include "../../structures/measured_strings.h"
50
 
50
 
51
#include "../device.h"
51
#include "../device.h"
52
#include "../netif.h"
52
#include "../netif.h"
53
#include "../netif_interface.h"
53
#include "../netif_interface.h"
54
 
54
 
55
#include "dp8390.h"
55
#include "dp8390.h"
56
#include "dp8390_drv.h"
56
#include "dp8390_drv.h"
57
#include "dp8390_module.h"
57
#include "dp8390_module.h"
58
#include "dp8390_port.h"
58
#include "dp8390_port.h"
59
 
59
 
60
#define NAME    "dp8390 network interface"
60
#define NAME    "dp8390 network interface"
61
 
61
 
62
#define IPC_GET_DEVICE( call )          ( device_id_t ) IPC_GET_METHOD( * call )
62
#define IPC_GET_DEVICE( call )          ( device_id_t ) IPC_GET_METHOD( * call )
63
#define IPC_GET_ISR( call )             ( int ) IPC_GET_ARG2( * call )
63
#define IPC_GET_ISR( call )             ( int ) IPC_GET_ARG2( * call )
64
 
64
 
65
static irq_cmd_t    dp8390_cmds[] = {
65
static irq_cmd_t    dp8390_cmds[] = {
66
    {   .cmd = CMD_PIO_READ_8,
66
    {   .cmd = CMD_PIO_READ_8,
67
        .addr = NULL,
67
        .addr = NULL,
68
        .dstarg = 2
68
        .dstarg = 2
69
    },
69
    },
70
    {
70
    {
71
        .cmd = CMD_PREDICATE,
71
        .cmd = CMD_PREDICATE,
72
        .value = 2,
72
        .value = 2,
73
        .srcarg = 2
73
        .srcarg = 2
74
    },
74
    },
75
    {   .cmd = CMD_PIO_WRITE_8,
75
    {   .cmd = CMD_PIO_WRITE_8,
76
        .addr = NULL,
76
        .addr = NULL,
77
        .srcarg = 2
77
        .srcarg = 2
78
    },
78
    },
79
    {
79
    {
80
        .cmd = CMD_ACCEPT
80
        .cmd = CMD_ACCEPT
81
    }
81
    }
82
};
82
};
83
 
83
 
84
static irq_code_t   dp8390_code = {
84
static irq_code_t   dp8390_code = {
85
    sizeof( dp8390_cmds ) / sizeof( irq_cmd_t ),
85
    sizeof( dp8390_cmds ) / sizeof( irq_cmd_t ),
86
    dp8390_cmds
86
    dp8390_cmds
87
};
87
};
88
 
88
 
89
netif_globals_t netif_globals;
89
netif_globals_t netif_globals;
90
 
90
 
91
void    netif_print_name( void );
91
void    netif_print_name( void );
92
 
92
 
93
void    irq_handler( ipc_callid_t iid, ipc_call_t * call );
93
void    irq_handler( ipc_callid_t iid, ipc_call_t * call );
94
void    change_state( device_ref device, device_state_t state );
94
void    change_state( device_ref device, device_state_t state );
95
 
95
 
96
int specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
96
int specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
97
    return ENOTSUP;
97
    return ENOTSUP;
98
}
98
}
99
 
99
 
100
int get_device_stats( device_id_t device_id, device_stats_ref stats ){
100
int get_device_stats( device_id_t device_id, device_stats_ref stats ){
101
    ERROR_DECLARE;
101
    ERROR_DECLARE;
102
 
102
 
103
    device_ref      device;
103
    device_ref      device;
104
    eth_stat_t *    de_stat;
104
    eth_stat_t *    de_stat;
105
 
105
 
106
    if( ! stats ) return EBADMEM;
106
    if( ! stats ) return EBADMEM;
107
    ERROR_PROPAGATE( find_device( device_id, & device ));
107
    ERROR_PROPAGATE( find_device( device_id, & device ));
108
    de_stat = & (( dpeth_t * ) device->specific )->de_stat;
108
    de_stat = & (( dpeth_t * ) device->specific )->de_stat;
109
    null_device_stats( stats );
109
    null_device_stats( stats );
110
    stats->rx_errors = de_stat->ets_recvErr;
110
    stats->rx_errors = de_stat->ets_recvErr;
111
    stats->tx_errors = de_stat->ets_sendErr;
111
    stats->tx_errors = de_stat->ets_sendErr;
112
    stats->rx_crc_errors = de_stat->ets_CRCerr;
112
    stats->rx_crc_errors = de_stat->ets_CRCerr;
113
    stats->rx_frame_errors = de_stat->ets_frameAll;
113
    stats->rx_frame_errors = de_stat->ets_frameAll;
114
    stats->rx_missed_errors = de_stat->ets_missedP;
114
    stats->rx_missed_errors = de_stat->ets_missedP;
115
    stats->rx_packets = de_stat->ets_packetR;
115
    stats->rx_packets = de_stat->ets_packetR;
116
    stats->tx_packets = de_stat->ets_packetT;
116
    stats->tx_packets = de_stat->ets_packetT;
117
    stats->collisions = de_stat->ets_collision;
117
    stats->collisions = de_stat->ets_collision;
118
    stats->tx_aborted_errors = de_stat->ets_transAb;
118
    stats->tx_aborted_errors = de_stat->ets_transAb;
119
    stats->tx_carrier_errors = de_stat->ets_carrSense;
119
    stats->tx_carrier_errors = de_stat->ets_carrSense;
120
    stats->tx_heartbeat_errors = de_stat->ets_CDheartbeat;
120
    stats->tx_heartbeat_errors = de_stat->ets_CDheartbeat;
121
    stats->tx_window_errors = de_stat->ets_OWC;
121
    stats->tx_window_errors = de_stat->ets_OWC;
122
    return EOK;
122
    return EOK;
123
}
123
}
124
 
124
 
125
void netif_print_name( void ){
125
void netif_print_name( void ){
126
    printf( NAME );
126
    printf( "%s", NAME );
127
}
127
}
128
 
128
 
129
int get_addr_message( device_id_t device_id, measured_string_ref address ){
129
int get_addr_message( device_id_t device_id, measured_string_ref address ){
130
    ERROR_DECLARE;
130
    ERROR_DECLARE;
131
 
131
 
132
    device_ref  device;
132
    device_ref  device;
133
 
133
 
134
    if( ! address ) return EBADMEM;
134
    if( ! address ) return EBADMEM;
135
    ERROR_PROPAGATE( find_device( device_id, & device ));
135
    ERROR_PROPAGATE( find_device( device_id, & device ));
136
    address->value = ( char * ) ( & (( dpeth_t * ) device->specific )->de_address );
136
    address->value = ( char * ) ( & (( dpeth_t * ) device->specific )->de_address );
137
    address->length = CONVERT_SIZE( ether_addr_t, char, 1 );
137
    address->length = CONVERT_SIZE( ether_addr_t, char, 1 );
138
    return EOK;
138
    return EOK;
139
}
139
}
140
 
140
 
141
void irq_handler( ipc_callid_t iid, ipc_call_t * call )
141
void irq_handler( ipc_callid_t iid, ipc_call_t * call )
142
{
142
{
143
//  int irq;
143
//  int irq;
144
    device_ref  device;
144
    device_ref  device;
145
    dpeth_t *   dep;
145
    dpeth_t *   dep;
146
 
146
 
147
    printf( "\ndevice %d - irq %x", IPC_GET_DEVICE( call ), IPC_GET_ISR( call ));
147
    printf( "\ndevice %d - irq %x", IPC_GET_DEVICE( call ), IPC_GET_ISR( call ));
148
    if( find_device( IPC_GET_DEVICE( call ), & device ) != EOK ) return;
148
    if( find_device( IPC_GET_DEVICE( call ), & device ) != EOK ) return;
149
    dep = ( dpeth_t * ) device->specific;
149
    dep = ( dpeth_t * ) device->specific;
150
    printf( "\ndev %d, irq %x\n", device->device_id, IPC_GET_ISR( call ));
150
    printf( "\ndev %d, irq %x\n", device->device_id, IPC_GET_ISR( call ));
151
    if ( dep->de_mode != DEM_ENABLED)
151
    if ( dep->de_mode != DEM_ENABLED)
152
//      continue;
152
//      continue;
153
        return;
153
        return;
154
    assert( dep->de_flags & DEF_ENABLED);
154
    assert( dep->de_flags & DEF_ENABLED);
155
//  irq= dep.de_irq;
155
//  irq= dep.de_irq;
156
//  assert(irq >= 0 && irq < NR_IRQ_VECTORS);
156
//  assert(irq >= 0 && irq < NR_IRQ_VECTORS);
157
//      if ( dep->de_int_pending || 1)
157
//      if ( dep->de_int_pending || 1)
158
//      {
158
//      {
159
        dep->de_int_pending= 0;
159
        dep->de_int_pending= 0;
160
        dp_check_ints( dep );
160
        dp_check_ints( dep );
161
//      do_int(dep);
161
//      do_int(dep);
162
/*      r= sys_irqenable(&dep->de_hook);
162
/*      r= sys_irqenable(&dep->de_hook);
163
        if (r != OK)
163
        if (r != OK)
164
        {
164
        {
165
            panic("DP8390",
165
            panic("DP8390",
166
            "unable enable interrupts", r);
166
            "unable enable interrupts", r);
167
        }
167
        }
168
*///    }
168
*///    }
169
}
169
}
170
 
170
 
171
int probe_auto_message( void ){
171
int probe_auto_message( void ){
172
    return ENOTSUP;
172
    return ENOTSUP;
173
}
173
}
174
 
174
 
175
int probe_message( device_id_t device_id, int irq, int io ){
175
int probe_message( device_id_t device_id, int irq, int io ){
176
    ERROR_DECLARE;
176
    ERROR_DECLARE;
177
 
177
 
178
    device_ref  device;
178
    device_ref  device;
179
    dpeth_t *   dep;
179
    dpeth_t *   dep;
180
 
180
 
181
    printf( "\n" );
181
    printf( "\n" );
182
    device = ( device_ref ) malloc( sizeof( device_t ));
182
    device = ( device_ref ) malloc( sizeof( device_t ));
183
    if( ! device ) return ENOMEM;
183
    if( ! device ) return ENOMEM;
184
    dep = ( dpeth_t * ) malloc( sizeof( dpeth_t ));
184
    dep = ( dpeth_t * ) malloc( sizeof( dpeth_t ));
185
    if( ! dep ){
185
    if( ! dep ){
186
        free( device );
186
        free( device );
187
        return ENOMEM;
187
        return ENOMEM;
188
    }
188
    }
189
    bzero( device, sizeof( device_t ));
189
    bzero( device, sizeof( device_t ));
190
    bzero( dep, sizeof( dpeth_t ));
190
    bzero( dep, sizeof( dpeth_t ));
191
    device->device_id = device_id;
191
    device->device_id = device_id;
192
    device->nil_phone = -1;
192
    device->nil_phone = -1;
193
    device->specific = ( void * ) dep;
193
    device->specific = ( void * ) dep;
194
    device->state = NETIF_STOPPED;
194
    device->state = NETIF_STOPPED;
195
    dep->parent = device;
195
    dep->parent = device;
196
    dep->de_irq = irq;
196
    dep->de_irq = irq;
197
    dep->de_mode = DEM_DISABLED;
197
    dep->de_mode = DEM_DISABLED;
198
    //TODO address?
198
    //TODO address?
199
    if( ERROR_OCCURRED( pio_enable(( void * ) io, DP8390_IO_SIZE, ( void ** ) & dep->de_base_port ))
199
    if( ERROR_OCCURRED( pio_enable(( void * ) io, DP8390_IO_SIZE, ( void ** ) & dep->de_base_port ))
200
    || ERROR_OCCURRED( do_probe( dep ))){
200
    || ERROR_OCCURRED( do_probe( dep ))){
201
        free( dep );
201
        free( dep );
202
        free( device );
202
        free( device );
203
        return ERROR_CODE;
203
        return ERROR_CODE;
204
    }
204
    }
205
    if( ERROR_OCCURRED( device_map_add( & netif_globals.device_map, device->device_id, device ))){
205
    if( ERROR_OCCURRED( device_map_add( & netif_globals.device_map, device->device_id, device ))){
206
        free( dep );
206
        free( dep );
207
        free( device );
207
        free( device );
208
        return ERROR_CODE;
208
        return ERROR_CODE;
209
    }
209
    }
210
    return EOK;
210
    return EOK;
211
}
211
}
212
 
212
 
213
int send_message( device_id_t device_id, packet_t packet ){
213
int send_message( device_id_t device_id, packet_t packet ){
214
    ERROR_DECLARE;
214
    ERROR_DECLARE;
215
 
215
 
216
    device_ref  device;
216
    device_ref  device;
217
    dpeth_t *   dep;
217
    dpeth_t *   dep;
218
    packet_t    next;
218
    packet_t    next;
219
 
219
 
220
    ERROR_PROPAGATE( find_device( IPC_GET_DEVICE( call ), & device ));
220
    ERROR_PROPAGATE( find_device( device_id, & device ));
221
    dep = ( dpeth_t * ) device->specific;
221
    dep = ( dpeth_t * ) device->specific;
222
    // process packet queue
222
    // process packet queue
223
    do{
223
    do{
224
        next = pq_detach( packet );
224
        next = pq_detach( packet );
225
        if( do_pwrite( dep, packet, FALSE ) != EBUSY ){
225
        if( do_pwrite( dep, packet, FALSE ) != EBUSY ){
226
            netif_pq_release( packet_get_id( packet ));
226
            netif_pq_release( packet_get_id( packet ));
227
        }
227
        }
228
        packet = next;
228
        packet = next;
229
    }while( packet );
229
    }while( packet );
230
    return EOK;
230
    return EOK;
231
}
231
}
232
 
232
 
233
int start_message( device_id_t device_id ){
233
int start_message( device_id_t device_id ){
234
    ERROR_DECLARE;
234
    ERROR_DECLARE;
235
 
235
 
236
    device_ref  device;
236
    device_ref  device;
237
    dpeth_t *   dep;
237
    dpeth_t *   dep;
238
 
238
 
239
    ERROR_PROPAGATE( find_device( device_id, & device ));
239
    ERROR_PROPAGATE( find_device( device_id, & device ));
240
    if( device->state != NETIF_ACTIVE ){
240
    if( device->state != NETIF_ACTIVE ){
241
        dep = ( dpeth_t * ) device->specific;
241
        dep = ( dpeth_t * ) device->specific;
242
        dp8390_cmds[ 0 ].addr = ( void * ) ( uint32_t ) ( dep->de_dp8390_port + DP_ISR );
242
        dp8390_cmds[ 0 ].addr = ( void * ) ( uint32_t ) ( dep->de_dp8390_port + DP_ISR );
243
        dp8390_cmds[ 2 ].addr = dp8390_cmds[ 0 ].addr;
243
        dp8390_cmds[ 2 ].addr = dp8390_cmds[ 0 ].addr;
244
        ERROR_PROPAGATE( ipc_register_irq( dep->de_irq, device->device_id, device->device_id, & dp8390_code ));
244
        ERROR_PROPAGATE( ipc_register_irq( dep->de_irq, device->device_id, device->device_id, & dp8390_code ));
245
        if( ERROR_OCCURRED( do_init( dep, DL_BROAD_REQ ))){
245
        if( ERROR_OCCURRED( do_init( dep, DL_BROAD_REQ ))){
246
            ipc_unregister_irq( dep->de_irq, device->device_id );
246
            ipc_unregister_irq( dep->de_irq, device->device_id );
247
            return ERROR_CODE;
247
            return ERROR_CODE;
248
        }
248
        }
249
        change_state( device, NETIF_ACTIVE );
249
        change_state( device, NETIF_ACTIVE );
250
    }
250
    }
251
    return EOK;
251
    return EOK;
252
}
252
}
253
 
253
 
254
int stop_message( device_id_t device_id ){
254
int stop_message( device_id_t device_id ){
255
    ERROR_DECLARE;
255
    ERROR_DECLARE;
256
 
256
 
257
    device_ref  device;
257
    device_ref  device;
258
    dpeth_t *   dep;
258
    dpeth_t *   dep;
259
 
259
 
260
    ERROR_PROPAGATE( find_device( device_id, & device ));
260
    ERROR_PROPAGATE( find_device( device_id, & device ));
261
    if( device->state != NETIF_STOPPED ){
261
    if( device->state != NETIF_STOPPED ){
262
        dep = ( dpeth_t * ) device->specific;
262
        dep = ( dpeth_t * ) device->specific;
263
        do_stop( dep );
263
        do_stop( dep );
264
        ipc_unregister_irq( dep->de_irq, device->device_id );
264
        ipc_unregister_irq( dep->de_irq, device->device_id );
265
        change_state( device, NETIF_STOPPED );
265
        change_state( device, NETIF_STOPPED );
266
    }
266
    }
267
    return EOK;
267
    return EOK;
268
}
268
}
269
 
269
 
270
void change_state( device_ref device, device_state_t state ){
270
void change_state( device_ref device, device_state_t state ){
271
    device->state = state;
271
    device->state = state;
272
    nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL );
272
    nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL );
273
    printf( "\nState changed to %s", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
273
    printf( "\nState changed to %s", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
274
}
274
}
275
 
275
 
276
int initialize( void ){
276
int initialize( void ){
277
    ipcarg_t    phonehash;
277
    ipcarg_t    phonehash;
278
 
278
 
279
    async_set_interrupt_received( irq_handler );
279
    async_set_interrupt_received( irq_handler );
280
 
280
 
281
    return REGISTER_ME( SERVICE_DP8390, & phonehash );
281
    return REGISTER_ME( SERVICE_DP8390, & phonehash );
282
}
282
}
283
 
283
 
284
int netif_send_packet( dpeth_t * dep, packet_t packet ){
284
int netif_send_packet( dpeth_t * dep, packet_t packet ){
285
    if( !( dep && dep->parent )) return EINVAL;
285
    if( !( dep && dep->parent )) return EINVAL;
286
    nil_message(( device_ref ) dep->parent, NET_NIL_RECEIVED, packet_get_id( packet ), 0 );
286
    nil_message(( device_ref ) dep->parent, NET_NIL_RECEIVED, packet_get_id( packet ), 0 );
287
    return EOK;
287
    return EOK;
288
}
288
}
289
 
289
 
290
/** @}
290
/** @}
291
 */
291
 */
292
 
292