source: mainline/uspace/drv/nic/ne2k/ne2k.c@ 41998ec

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 41998ec was cfb79747, checked in by Jakub Jermar <jakub@…>, 14 years ago

Merge mainline changes.

  • Property mode set to 100644
File size: 10.8 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Decky
3 * Copyright (c) 2011 Radim Vansa
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/**
31 * @addtogroup drv_ne2k
32 * @brief Novell NE2000 NIC driver
33 * @{
34 */
35/**
36 * @file
37 * @brief Bridge between NICF, DDF and business logic for the NIC
38 */
39
40#include <stdio.h>
41#include <errno.h>
42#include <stdlib.h>
43#include <str_error.h>
44#include <async.h>
45#include "dp8390.h"
46
47#define NAME "ne2k"
48
49/** Return the ISR from the interrupt call.
50 *
51 * @param[in] call The interrupt call.
52 *
53 */
54#define IRQ_GET_ISR(call) ((int) IPC_GET_ARG2(call))
55
56/** Return the TSR from the interrupt call.
57 *
58 * @param[in] call The interrupt call.
59 *
60 */
61#define IRQ_GET_TSR(call) ((int) IPC_GET_ARG3(call))
62
63#define DRIVER_DATA(dev) ((nic_t *) ((dev)->driver_data))
64#define NE2K(device) ((ne2k_t *) nic_get_specific(DRIVER_DATA(device)))
65
66static irq_pio_range_t ne2k_ranges_prototype[] = {
67 {
68 .base = 0,
69 .size = NE2K_IO_SIZE,
70 }
71};
72
73/** NE2000 kernel interrupt command sequence.
74 *
75 */
76static irq_cmd_t ne2k_cmds_prototype[] = {
77 {
78 /* Read Interrupt Status Register */
79 .cmd = CMD_PIO_READ_8,
80 .addr = NULL,
81 .dstarg = 2
82 },
83 {
84 /* Mask supported interrupt causes */
85 .cmd = CMD_BTEST,
86 .value = (ISR_PRX | ISR_PTX | ISR_RXE | ISR_TXE | ISR_OVW |
87 ISR_CNT | ISR_RDC),
88 .srcarg = 2,
89 .dstarg = 3,
90 },
91 {
92 /* Predicate for accepting the interrupt */
93 .cmd = CMD_PREDICATE,
94 .value = 4,
95 .srcarg = 3
96 },
97 {
98 /*
99 * Mask future interrupts via
100 * Interrupt Mask Register
101 */
102 .cmd = CMD_PIO_WRITE_8,
103 .addr = NULL,
104 .value = 0
105 },
106 {
107 /* Acknowledge the current interrupt */
108 .cmd = CMD_PIO_WRITE_A_8,
109 .addr = NULL,
110 .srcarg = 3
111 },
112 {
113 /* Read Transmit Status Register */
114 .cmd = CMD_PIO_READ_8,
115 .addr = NULL,
116 .dstarg = 3
117 },
118 {
119 .cmd = CMD_ACCEPT
120 }
121};
122
123static void ne2k_interrupt_handler(ddf_dev_t *dev, ipc_callid_t iid,
124 ipc_call_t *call);
125
126static int ne2k_register_interrupt(nic_t *nic_data)
127{
128 ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
129
130 if (ne2k->code.cmdcount == 0) {
131 irq_pio_range_t *ne2k_ranges;
132 irq_cmd_t *ne2k_cmds;
133
134 ne2k_ranges = malloc(sizeof(ne2k_ranges_prototype));
135 if (!ne2k_ranges)
136 return ENOMEM;
137 memcpy(ne2k_ranges, ne2k_ranges_prototype,
138 sizeof(ne2k_ranges_prototype));
139 ne2k_ranges[0].base = (uintptr_t) ne2k->base_port;
140
141 ne2k_cmds = malloc(sizeof(ne2k_cmds_prototype));
142 if (!ne2k_cmds) {
143 free(ne2k_ranges);
144 return ENOMEM;
145 }
146 memcpy(ne2k_cmds, ne2k_cmds_prototype,
147 sizeof(ne2k_cmds_prototype));
148 ne2k_cmds[0].addr = ne2k->base_port + DP_ISR;
149 ne2k_cmds[3].addr = ne2k->base_port + DP_IMR;
150 ne2k_cmds[4].addr = ne2k_cmds[0].addr;
151 ne2k_cmds[5].addr = ne2k->base_port + DP_TSR;
152
153 ne2k->code.rangecount = sizeof(ne2k_ranges_prototype) /
154 sizeof(irq_pio_range_t);
155 ne2k->code.ranges = ne2k_ranges;
156
157 ne2k->code.cmdcount = sizeof(ne2k_cmds_prototype) /
158 sizeof(irq_cmd_t);
159 ne2k->code.cmds = ne2k_cmds;
160 }
161
162 int rc = register_interrupt_handler(nic_get_ddf_dev(nic_data),
163 ne2k->irq, ne2k_interrupt_handler, &ne2k->code);
164 return rc;
165}
166
167static ddf_dev_ops_t ne2k_dev_ops;
168
169static void ne2k_dev_cleanup(ddf_dev_t *dev)
170{
171 if (dev->driver_data != NULL) {
172 ne2k_t *ne2k = NE2K(dev);
173 if (ne2k) {
174 free(ne2k->code.ranges);
175 free(ne2k->code.cmds);
176 }
177 nic_unbind_and_destroy(dev);
178 }
179 if (dev->parent_sess != NULL) {
180 async_hangup(dev->parent_sess);
181 dev->parent_sess = NULL;
182 }
183}
184
185static int ne2k_dev_init(nic_t *nic_data)
186{
187 /* Get HW resources */
188 hw_res_list_parsed_t hw_res_parsed;
189 hw_res_list_parsed_init(&hw_res_parsed);
190
191 int rc = nic_get_resources(nic_data, &hw_res_parsed);
192
193 if (rc != EOK)
194 goto failed;
195
196 if (hw_res_parsed.irqs.count == 0) {
197 rc = EINVAL;
198 goto failed;
199 }
200
201 if (hw_res_parsed.io_ranges.count == 0) {
202 rc = EINVAL;
203 goto failed;
204 }
205
206 if (hw_res_parsed.io_ranges.ranges[0].size < NE2K_IO_SIZE) {
207 rc = EINVAL;
208 goto failed;
209 }
210
211 ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
212 ne2k->irq = hw_res_parsed.irqs.irqs[0];
213
214 ne2k->base_port = (void *) (uintptr_t)
215 hw_res_parsed.io_ranges.ranges[0].address;
216
217 hw_res_list_parsed_clean(&hw_res_parsed);
218
219 /* Enable port I/O */
220 if (pio_enable(ne2k->base_port, NE2K_IO_SIZE, &ne2k->port) != EOK)
221 return EADDRNOTAVAIL;
222
223
224 ne2k->data_port = ne2k->port + NE2K_DATA;
225 ne2k->receive_configuration = RCR_AB | RCR_AM;
226 ne2k->probed = false;
227 ne2k->up = false;
228
229 /* Find out whether the device is present. */
230 if (ne2k_probe(ne2k) != EOK)
231 return ENOENT;
232
233 ne2k->probed = true;
234
235 rc = ne2k_register_interrupt(nic_data);
236 if (rc != EOK)
237 return EINVAL;
238
239 return EOK;
240
241failed:
242 hw_res_list_parsed_clean(&hw_res_parsed);
243 return rc;
244}
245
246void ne2k_interrupt_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
247{
248 nic_t *nic_data = DRIVER_DATA(dev);
249 ne2k_interrupt(nic_data, IRQ_GET_ISR(*call), IRQ_GET_TSR(*call));
250
251 async_answer_0(iid, EOK);
252}
253
254static int ne2k_on_activating(nic_t *nic_data)
255{
256 ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
257
258 if (!ne2k->up) {
259 int rc = ne2k_up(ne2k);
260 if (rc != EOK) {
261 return rc;
262 }
263
264 nic_enable_interrupt(nic_data, ne2k->irq);
265 }
266 return EOK;
267}
268
269static int ne2k_on_stopping(nic_t *nic_data)
270{
271 ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
272
273 nic_disable_interrupt(nic_data, ne2k->irq);
274 ne2k->receive_configuration = RCR_AB | RCR_AM;
275 ne2k_down(ne2k);
276 return EOK;
277}
278
279static int ne2k_set_address(ddf_fun_t *fun, const nic_address_t *address)
280{
281 nic_t *nic_data = DRIVER_DATA(fun);
282 int rc = nic_report_address(nic_data, address);
283 if (rc != EOK) {
284 return EINVAL;
285 }
286 /* Note: some frame with previous physical address may slip to NIL here
287 * (for a moment the filtering is not exact), but ethernet should be OK with
288 * that. Some frames may also be lost, but this is not a problem.
289 */
290 ne2k_set_physical_address((ne2k_t *) nic_get_specific(nic_data), address);
291 return EOK;
292}
293
294static int ne2k_on_unicast_mode_change(nic_t *nic_data,
295 nic_unicast_mode_t new_mode,
296 const nic_address_t *address_list, size_t address_count)
297{
298 ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
299 switch (new_mode) {
300 case NIC_UNICAST_BLOCKED:
301 ne2k_set_promisc_phys(ne2k, false);
302 nic_report_hw_filtering(nic_data, 0, -1, -1);
303 return EOK;
304 case NIC_UNICAST_DEFAULT:
305 ne2k_set_promisc_phys(ne2k, false);
306 nic_report_hw_filtering(nic_data, 1, -1, -1);
307 return EOK;
308 case NIC_UNICAST_LIST:
309 ne2k_set_promisc_phys(ne2k, true);
310 nic_report_hw_filtering(nic_data, 0, -1, -1);
311 return EOK;
312 case NIC_UNICAST_PROMISC:
313 ne2k_set_promisc_phys(ne2k, true);
314 nic_report_hw_filtering(nic_data, 1, -1, -1);
315 return EOK;
316 default:
317 return ENOTSUP;
318 }
319}
320
321static int ne2k_on_multicast_mode_change(nic_t *nic_data,
322 nic_multicast_mode_t new_mode,
323 const nic_address_t *address_list, size_t address_count)
324{
325 ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
326 switch (new_mode) {
327 case NIC_MULTICAST_BLOCKED:
328 ne2k_set_accept_mcast(ne2k, false);
329 nic_report_hw_filtering(nic_data, -1, 1, -1);
330 return EOK;
331 case NIC_MULTICAST_LIST:
332 ne2k_set_accept_mcast(ne2k, true);
333 ne2k_set_mcast_hash(ne2k,
334 nic_mcast_hash(address_list, address_count));
335 nic_report_hw_filtering(nic_data, -1, 0, -1);
336 return EOK;
337 case NIC_MULTICAST_PROMISC:
338 ne2k_set_accept_mcast(ne2k, true);
339 ne2k_set_mcast_hash(ne2k, 0xFFFFFFFFFFFFFFFFllu);
340 nic_report_hw_filtering(nic_data, -1, 1, -1);
341 return EOK;
342 default:
343 return ENOTSUP;
344 }
345}
346
347static int ne2k_on_broadcast_mode_change(nic_t *nic_data,
348 nic_broadcast_mode_t new_mode)
349{
350 ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
351 switch (new_mode) {
352 case NIC_BROADCAST_BLOCKED:
353 ne2k_set_accept_bcast(ne2k, false);
354 return EOK;
355 case NIC_BROADCAST_ACCEPTED:
356 ne2k_set_accept_bcast(ne2k, true);
357 return EOK;
358 default:
359 return ENOTSUP;
360 }
361}
362
363static int ne2k_dev_add(ddf_dev_t *dev)
364{
365 ddf_fun_t *fun;
366
367 /* Allocate driver data for the device. */
368 nic_t *nic_data = nic_create_and_bind(dev);
369 if (nic_data == NULL)
370 return ENOMEM;
371
372 nic_set_send_frame_handler(nic_data, ne2k_send);
373 nic_set_state_change_handlers(nic_data,
374 ne2k_on_activating, NULL, ne2k_on_stopping);
375 nic_set_filtering_change_handlers(nic_data,
376 ne2k_on_unicast_mode_change, ne2k_on_multicast_mode_change,
377 ne2k_on_broadcast_mode_change, NULL, NULL);
378
379 ne2k_t *ne2k = malloc(sizeof(ne2k_t));
380 if (NULL != ne2k) {
381 memset(ne2k, 0, sizeof(ne2k_t));
382 nic_set_specific(nic_data, ne2k);
383 } else {
384 nic_unbind_and_destroy(dev);
385 return ENOMEM;
386 }
387
388 int rc = ne2k_dev_init(nic_data);
389 if (rc != EOK) {
390 ne2k_dev_cleanup(dev);
391 return rc;
392 }
393
394 rc = nic_report_address(nic_data, &ne2k->mac);
395 if (rc != EOK) {
396 ne2k_dev_cleanup(dev);
397 return rc;
398 }
399
400 rc = nic_connect_to_services(nic_data);
401 if (rc != EOK) {
402 ne2k_dev_cleanup(dev);
403 return rc;
404 }
405
406 fun = ddf_fun_create(nic_get_ddf_dev(nic_data), fun_exposed, "port0");
407 if (fun == NULL) {
408 ne2k_dev_cleanup(dev);
409 return ENOMEM;
410 }
411 nic_set_ddf_fun(nic_data, fun);
412 fun->ops = &ne2k_dev_ops;
413 fun->driver_data = nic_data;
414
415 rc = ddf_fun_bind(fun);
416 if (rc != EOK) {
417 ddf_fun_destroy(fun);
418 ne2k_dev_cleanup(dev);
419 return rc;
420 }
421
422 rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
423 if (rc != EOK) {
424 ddf_fun_unbind(fun);
425 ddf_fun_destroy(fun);
426 return rc;
427 }
428
429 return EOK;
430}
431
432static nic_iface_t ne2k_nic_iface = {
433 .set_address = ne2k_set_address
434};
435
436static driver_ops_t ne2k_driver_ops = {
437 .dev_add = ne2k_dev_add
438};
439
440static driver_t ne2k_driver = {
441 .name = NAME,
442 .driver_ops = &ne2k_driver_ops
443};
444
445int main(int argc, char *argv[])
446{
447 nic_driver_init(NAME);
448 nic_driver_implement(&ne2k_driver_ops, &ne2k_dev_ops, &ne2k_nic_iface);
449
450 return ddf_driver_main(&ne2k_driver);
451}
452
453/** @}
454 */
Note: See TracBrowser for help on using the repository browser.