source: mainline/uspace/srv/hw/netif/dp8390/dp8390_module.c@ 39d70ec

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

remove additional packet queueing

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