source: mainline/uspace/srv/hw/netif/dp8390/dp8390_module.c@ 61bfc370

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