source: mainline/uspace/srv/hw/netif/dp8390/dp8390_module.c@ 0777f4c5

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

improve dp_check_ints

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