source: mainline/uspace/srv/net/netif/dp8390/dp8390_module.c@ 6092b56e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6092b56e was b48ebd19, checked in by Lukas Mejdrech <lukasmejdrech@…>, 16 years ago
  • debug prints in debug mode only
  • Property mode set to 100644
File size: 10.8 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 <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
53#include "../../include/device.h"
54#include "../../include/nil_interface.h"
55
56#include "../netif.h"
57#include "../netif_module.h"
58
59#include "dp8390.h"
60#include "dp8390_drv.h"
61#include "dp8390_port.h"
62
63/** DP8390 module name.
64 */
65#define NAME "dp8390 network interface"
66
67/** Returns the device from the interrupt call.
68 * @param[in] call The interrupt call.
69 */
70#define IRQ_GET_DEVICE(call) (device_id_t) IPC_GET_METHOD(*call)
71
72/** Returns the interrupt status register from the interrupt call.
73 * @param[in] call The interrupt call.
74 */
75#define IPC_GET_ISR(call) (int) IPC_GET_ARG2(*call)
76
77/** DP8390 kernel interrupt command sequence.
78 */
79static irq_cmd_t dp8390_cmds[] = {
80 { .cmd = CMD_PIO_READ_8,
81 .addr = NULL,
82 .dstarg = 2
83 },
84 {
85 .cmd = CMD_PREDICATE,
86 .value = 1,
87 .srcarg = 2
88 },
89 {
90 .cmd = CMD_ACCEPT
91 }
92};
93
94/** DP8390 kernel interrupt code.
95 */
96static irq_code_t dp8390_code = {
97 sizeof(dp8390_cmds) / sizeof(irq_cmd_t),
98 dp8390_cmds
99};
100
101/** Network interface module global data.
102 */
103netif_globals_t netif_globals;
104
105/** Prints the module name.
106 * @see NAME
107 */
108void module_print_name(void);
109
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 */
115void irq_handler(ipc_callid_t iid, ipc_call_t * call);
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 */
122int change_state(device_ref device, device_state_t state);
123
124int netif_specific_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
125 return ENOTSUP;
126}
127
128int netif_get_device_stats(device_id_t device_id, device_stats_ref stats){
129 ERROR_DECLARE;
130
131 device_ref device;
132 eth_stat_t * de_stat;
133
134 if(! stats){
135 return EBADMEM;
136 }
137 ERROR_PROPAGATE(find_device(device_id, &device));
138 de_stat = &((dpeth_t *) device->specific)->de_stat;
139 null_device_stats(stats);
140 stats->receive_errors = de_stat->ets_recvErr;
141 stats->send_errors = de_stat->ets_sendErr;
142 stats->receive_crc_errors = de_stat->ets_CRCerr;
143 stats->receive_frame_errors = de_stat->ets_frameAll;
144 stats->receive_missed_errors = de_stat->ets_missedP;
145 stats->receive_packets = de_stat->ets_packetR;
146 stats->send_packets = de_stat->ets_packetT;
147 stats->collisions = de_stat->ets_collision;
148 stats->send_aborted_errors = de_stat->ets_transAb;
149 stats->send_carrier_errors = de_stat->ets_carrSense;
150 stats->send_heartbeat_errors = de_stat->ets_CDheartbeat;
151 stats->send_window_errors = de_stat->ets_OWC;
152 return EOK;
153}
154
155void module_print_name(void){
156 printf("%s", NAME);
157}
158
159int netif_get_addr_message(device_id_t device_id, measured_string_ref address){
160 ERROR_DECLARE;
161
162 device_ref device;
163
164 if(! address){
165 return EBADMEM;
166 }
167 ERROR_PROPAGATE(find_device(device_id, &device));
168 address->value = (char *) (&((dpeth_t *) device->specific)->de_address);
169 address->length = CONVERT_SIZE(ether_addr_t, char, 1);
170 return EOK;
171}
172
173void irq_handler(ipc_callid_t iid, ipc_call_t * call)
174{
175 device_ref device;
176 dpeth_t * dep;
177 packet_t received;
178 device_id_t device_id;
179 int phone;
180
181 device_id = IRQ_GET_DEVICE(call);
182 fibril_rwlock_write_lock(&netif_globals.lock);
183 if(find_device(device_id, &device) != EOK){
184 fibril_rwlock_write_unlock(&netif_globals.lock);
185 return;
186 }
187 dep = (dpeth_t *) device->specific;
188 if (dep->de_mode != DEM_ENABLED){
189 fibril_rwlock_write_unlock(&netif_globals.lock);
190 return;
191 }
192 assert(dep->de_flags &DEF_ENABLED);
193 dep->de_int_pending = 0;
194// remove debug print:
195#ifdef CONFIG_DEBUG
196 printf("I%d: 0x%x\n", device_id, IPC_GET_ISR(call));
197#endif
198 dp_check_ints(dep, IPC_GET_ISR(call));
199 if(dep->received_queue){
200 received = dep->received_queue;
201 phone = device->nil_phone;
202 dep->received_queue = NULL;
203 dep->received_count = 0;
204 fibril_rwlock_write_unlock(&netif_globals.lock);
205// remove debug dump:
206#ifdef CONFIG_DEBUG
207 uint8_t * data;
208 data = packet_get_data(received);
209 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]);
210#endif
211 nil_received_msg(phone, device_id, received, NULL);
212 }else{
213 fibril_rwlock_write_unlock(&netif_globals.lock);
214 }
215 ipc_answer_0(iid, EOK);
216}
217
218int netif_probe_message(device_id_t device_id, int irq, uintptr_t io){
219 ERROR_DECLARE;
220
221 device_ref device;
222 dpeth_t * dep;
223
224 device = (device_ref) malloc(sizeof(device_t));
225 if(! device){
226 return ENOMEM;
227 }
228 dep = (dpeth_t *) malloc(sizeof(dpeth_t));
229 if(! dep){
230 free(device);
231 return ENOMEM;
232 }
233 bzero(device, sizeof(device_t));
234 bzero(dep, sizeof(dpeth_t));
235 device->device_id = device_id;
236 device->nil_phone = -1;
237 device->specific = (void *) dep;
238 device->state = NETIF_STOPPED;
239 dep->de_irq = irq;
240 dep->de_mode = DEM_DISABLED;
241 //TODO address?
242 if(ERROR_OCCURRED(pio_enable((void *) io, DP8390_IO_SIZE, (void **) &dep->de_base_port))
243 || ERROR_OCCURRED(do_probe(dep))){
244 free(dep);
245 free(device);
246 return ERROR_CODE;
247 }
248 if(ERROR_OCCURRED(device_map_add(&netif_globals.device_map, device->device_id, device))){
249 free(dep);
250 free(device);
251 return ERROR_CODE;
252 }
253 return EOK;
254}
255
256int netif_send_message(device_id_t device_id, packet_t packet, services_t sender){
257 ERROR_DECLARE;
258
259 device_ref device;
260 dpeth_t * dep;
261 packet_t next;
262
263 ERROR_PROPAGATE(find_device(device_id, &device));
264 if(device->state != NETIF_ACTIVE){
265 netif_pq_release(packet_get_id(packet));
266 return EFORWARD;
267 }
268 dep = (dpeth_t *) device->specific;
269 // process packet queue
270 do{
271 next = pq_detach(packet);
272// remove debug dump:
273#ifdef CONFIG_DEBUG
274 uint8_t * data;
275 data = packet_get_data(packet);
276 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]);
277#endif
278 if(do_pwrite(dep, packet, FALSE) != EBUSY){
279 netif_pq_release(packet_get_id(packet));
280 }
281 packet = next;
282 }while(packet);
283 return EOK;
284}
285
286int netif_start_message(device_ref device){
287 ERROR_DECLARE;
288
289 dpeth_t * dep;
290
291 if(device->state != NETIF_ACTIVE){
292 dep = (dpeth_t *) device->specific;
293 dp8390_cmds[0].addr = (void *) (uintptr_t) (dep->de_dp8390_port + DP_ISR);
294 dp8390_cmds[2].addr = dp8390_cmds[0].addr;
295 ERROR_PROPAGATE(ipc_register_irq(dep->de_irq, device->device_id, device->device_id, &dp8390_code));
296 if(ERROR_OCCURRED(do_init(dep, DL_BROAD_REQ))){
297 ipc_unregister_irq(dep->de_irq, device->device_id);
298 return ERROR_CODE;
299 }
300 return change_state(device, NETIF_ACTIVE);
301 }
302 return EOK;
303}
304
305int netif_stop_message(device_ref device){
306 dpeth_t * dep;
307
308 if(device->state != NETIF_STOPPED){
309 dep = (dpeth_t *) device->specific;
310 do_stop(dep);
311 ipc_unregister_irq(dep->de_irq, device->device_id);
312 return change_state(device, NETIF_STOPPED);
313 }
314 return EOK;
315}
316
317int change_state(device_ref device, device_state_t state){
318 device->state = state;
319 printf("State changed to %s\n", (state == NETIF_ACTIVE) ? "ACTIVE" : "STOPPED");
320 return state;
321}
322
323int netif_initialize(void){
324 ipcarg_t phonehash;
325
326 async_set_interrupt_received(irq_handler);
327
328 return REGISTER_ME(SERVICE_DP8390, &phonehash);
329}
330
331/** @}
332 */
Note: See TracBrowser for help on using the repository browser.