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