2 * Copyright (c) 2011 Martin Decky
3 * Copyright (c) 2011 Radim Vansa
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
31 * @addtogroup drv_ne2k
36 * @brief Bridge between NICF, DDF and business logic for the NIC
41 #include <device/hw_res.h>
43 #include <str_error.h>
49 /** Return the ISR from the interrupt call.
51 * @param[in] call The interrupt call.
54 #define IRQ_GET_ISR(call) ((int) ipc_get_arg2(&call))
56 /** Return the TSR from the interrupt call.
58 * @param[in] call The interrupt call.
61 #define IRQ_GET_TSR(call) ((int) ipc_get_arg3(&call))
63 #define DRIVER_DATA(dev) ((nic_t *) ddf_dev_data_get(dev))
64 #define NE2K(device) ((ne2k_t *) nic_get_specific(DRIVER_DATA(device)))
66 static irq_pio_range_t ne2k_ranges_prototype
[] = {
73 /** NE2000 kernel interrupt command sequence.
76 static irq_cmd_t ne2k_cmds_prototype
[] = {
78 /* Read Interrupt Status Register */
79 .cmd
= CMD_PIO_READ_8
,
84 /* Mask supported interrupt causes */
86 .value
= (ISR_PRX
| ISR_PTX
| ISR_RXE
| ISR_TXE
| ISR_OVW
|
92 /* Predicate for accepting the interrupt */
99 * Mask future interrupts via
100 * Interrupt Mask Register
102 .cmd
= CMD_PIO_WRITE_8
,
107 /* Acknowledge the current interrupt */
108 .cmd
= CMD_PIO_WRITE_A_8
,
113 /* Read Transmit Status Register */
114 .cmd
= CMD_PIO_READ_8
,
123 static void ne2k_interrupt_handler(ipc_call_t
*, void *);
125 static errno_t
ne2k_register_interrupt(nic_t
*nic_data
,
126 cap_irq_handle_t
*handle
)
128 ne2k_t
*ne2k
= (ne2k_t
*) nic_get_specific(nic_data
);
130 if (ne2k
->code
.cmdcount
== 0) {
131 irq_pio_range_t
*ne2k_ranges
;
132 irq_cmd_t
*ne2k_cmds
;
134 ne2k_ranges
= malloc(sizeof(ne2k_ranges_prototype
));
137 memcpy(ne2k_ranges
, ne2k_ranges_prototype
,
138 sizeof(ne2k_ranges_prototype
));
139 ne2k_ranges
[0].base
= (uintptr_t) ne2k
->base_port
;
141 ne2k_cmds
= malloc(sizeof(ne2k_cmds_prototype
));
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
;
153 ne2k
->code
.rangecount
= sizeof(ne2k_ranges_prototype
) /
154 sizeof(irq_pio_range_t
);
155 ne2k
->code
.ranges
= ne2k_ranges
;
157 ne2k
->code
.cmdcount
= sizeof(ne2k_cmds_prototype
) /
159 ne2k
->code
.cmds
= ne2k_cmds
;
162 return register_interrupt_handler(nic_get_ddf_dev(nic_data
),
163 ne2k
->irq
, ne2k_interrupt_handler
, (void *)nic_data
, &ne2k
->code
,
167 static ddf_dev_ops_t ne2k_dev_ops
;
169 static void ne2k_dev_cleanup(ddf_dev_t
*dev
)
171 if (ddf_dev_data_get(dev
) != NULL
) {
172 ne2k_t
*ne2k
= NE2K(dev
);
174 free(ne2k
->code
.ranges
);
175 free(ne2k
->code
.cmds
);
177 nic_unbind_and_destroy(dev
);
181 static errno_t
ne2k_dev_init(nic_t
*nic_data
)
183 /* Get HW resources */
184 hw_res_list_parsed_t hw_res_parsed
;
185 hw_res_list_parsed_init(&hw_res_parsed
);
187 errno_t rc
= nic_get_resources(nic_data
, &hw_res_parsed
);
192 if (hw_res_parsed
.irqs
.count
== 0) {
197 if (hw_res_parsed
.io_ranges
.count
== 0) {
202 if (hw_res_parsed
.io_ranges
.ranges
[0].size
< NE2K_IO_SIZE
) {
207 ne2k_t
*ne2k
= (ne2k_t
*) nic_get_specific(nic_data
);
208 ne2k
->irq
= hw_res_parsed
.irqs
.irqs
[0];
210 addr_range_t regs
= hw_res_parsed
.io_ranges
.ranges
[0];
211 ne2k
->base_port
= RNGABSPTR(regs
);
213 hw_res_list_parsed_clean(&hw_res_parsed
);
215 /* Enable programmed I/O */
216 if (pio_enable_range(®s
, &ne2k
->port
) != EOK
)
217 return EADDRNOTAVAIL
;
219 ne2k
->data_port
= ne2k
->port
+ NE2K_DATA
;
220 ne2k
->receive_configuration
= RCR_AB
| RCR_AM
;
221 ne2k
->probed
= false;
224 /* Find out whether the device is present. */
225 if (ne2k_probe(ne2k
) != EOK
)
230 if (ne2k_register_interrupt(nic_data
, NULL
) != EOK
)
236 hw_res_list_parsed_clean(&hw_res_parsed
);
240 /** NE2K interrupt handler
242 * @param call IRQ event notification
243 * @param arg Argument (nic_t *)
245 void ne2k_interrupt_handler(ipc_call_t
*call
, void *arg
)
247 nic_t
*nic_data
= (nic_t
*)arg
;
248 ne2k_interrupt(nic_data
, IRQ_GET_ISR(*call
), IRQ_GET_TSR(*call
));
251 static errno_t
ne2k_on_activating(nic_t
*nic_data
)
253 ne2k_t
*ne2k
= (ne2k_t
*) nic_get_specific(nic_data
);
256 errno_t rc
= ne2k_up(ne2k
);
260 rc
= hw_res_enable_interrupt(ne2k
->parent_sess
, ne2k
->irq
);
269 static errno_t
ne2k_on_stopping(nic_t
*nic_data
)
271 ne2k_t
*ne2k
= (ne2k_t
*) nic_get_specific(nic_data
);
273 (void) hw_res_disable_interrupt(ne2k
->parent_sess
, ne2k
->irq
);
274 ne2k
->receive_configuration
= RCR_AB
| RCR_AM
;
279 static errno_t
ne2k_set_address(ddf_fun_t
*fun
, const nic_address_t
*address
)
281 nic_t
*nic_data
= DRIVER_DATA(ddf_fun_get_dev(fun
));
282 errno_t rc
= nic_report_address(nic_data
, address
);
287 * Note: some frame with previous physical address may slip to NIL here
288 * (for a moment the filtering is not exact), but ethernet should be OK with
289 * that. Some frames may also be lost, but this is not a problem.
291 ne2k_set_physical_address((ne2k_t
*) nic_get_specific(nic_data
), address
);
295 static errno_t
ne2k_get_device_info(ddf_fun_t
*fun
, nic_device_info_t
*info
)
297 nic_t
*nic_data
= nic_get_from_ddf_fun(fun
);
301 str_cpy(info
->vendor_name
, sizeof(info
->vendor_name
), "Novell");
302 str_cpy(info
->model_name
, sizeof(info
->model_name
), "NE2000");
307 static errno_t
ne2k_get_cable_state(ddf_fun_t
*fun
, nic_cable_state_t
*state
)
309 *state
= NIC_CS_PLUGGED
;
313 static errno_t
ne2k_get_operation_mode(ddf_fun_t
*fun
, int *speed
,
314 nic_channel_mode_t
*duplex
, nic_role_t
*role
)
317 *duplex
= NIC_CM_HALF_DUPLEX
; // XXX
318 *role
= NIC_ROLE_UNKNOWN
;
322 static errno_t
ne2k_on_unicast_mode_change(nic_t
*nic_data
,
323 nic_unicast_mode_t new_mode
,
324 const nic_address_t
*address_list
, size_t address_count
)
326 ne2k_t
*ne2k
= (ne2k_t
*) nic_get_specific(nic_data
);
328 case NIC_UNICAST_BLOCKED
:
329 ne2k_set_promisc_phys(ne2k
, false);
330 nic_report_hw_filtering(nic_data
, 0, -1, -1);
332 case NIC_UNICAST_DEFAULT
:
333 ne2k_set_promisc_phys(ne2k
, false);
334 nic_report_hw_filtering(nic_data
, 1, -1, -1);
336 case NIC_UNICAST_LIST
:
337 ne2k_set_promisc_phys(ne2k
, true);
338 nic_report_hw_filtering(nic_data
, 0, -1, -1);
340 case NIC_UNICAST_PROMISC
:
341 ne2k_set_promisc_phys(ne2k
, true);
342 nic_report_hw_filtering(nic_data
, 1, -1, -1);
349 static errno_t
ne2k_on_multicast_mode_change(nic_t
*nic_data
,
350 nic_multicast_mode_t new_mode
,
351 const nic_address_t
*address_list
, size_t address_count
)
353 ne2k_t
*ne2k
= (ne2k_t
*) nic_get_specific(nic_data
);
355 case NIC_MULTICAST_BLOCKED
:
356 ne2k_set_accept_mcast(ne2k
, false);
357 nic_report_hw_filtering(nic_data
, -1, 1, -1);
359 case NIC_MULTICAST_LIST
:
360 ne2k_set_accept_mcast(ne2k
, true);
361 ne2k_set_mcast_hash(ne2k
,
362 nic_mcast_hash(address_list
, address_count
));
363 nic_report_hw_filtering(nic_data
, -1, 0, -1);
365 case NIC_MULTICAST_PROMISC
:
366 ne2k_set_accept_mcast(ne2k
, true);
367 ne2k_set_mcast_hash(ne2k
, 0xFFFFFFFFFFFFFFFFllu
);
368 nic_report_hw_filtering(nic_data
, -1, 1, -1);
375 static errno_t
ne2k_on_broadcast_mode_change(nic_t
*nic_data
,
376 nic_broadcast_mode_t new_mode
)
378 ne2k_t
*ne2k
= (ne2k_t
*) nic_get_specific(nic_data
);
380 case NIC_BROADCAST_BLOCKED
:
381 ne2k_set_accept_bcast(ne2k
, false);
383 case NIC_BROADCAST_ACCEPTED
:
384 ne2k_set_accept_bcast(ne2k
, true);
391 static errno_t
ne2k_dev_add(ddf_dev_t
*dev
)
395 /* Allocate driver data for the device. */
396 nic_t
*nic_data
= nic_create_and_bind(dev
);
397 if (nic_data
== NULL
)
400 nic_set_send_frame_handler(nic_data
, ne2k_send
);
401 nic_set_state_change_handlers(nic_data
,
402 ne2k_on_activating
, NULL
, ne2k_on_stopping
);
403 nic_set_filtering_change_handlers(nic_data
,
404 ne2k_on_unicast_mode_change
, ne2k_on_multicast_mode_change
,
405 ne2k_on_broadcast_mode_change
, NULL
, NULL
);
407 ne2k_t
*ne2k
= malloc(sizeof(ne2k_t
));
409 memset(ne2k
, 0, sizeof(ne2k_t
));
410 nic_set_specific(nic_data
, ne2k
);
412 nic_unbind_and_destroy(dev
);
417 ne2k
->parent_sess
= ddf_dev_parent_sess_get(dev
);
418 if (ne2k
->parent_sess
== NULL
) {
419 ne2k_dev_cleanup(dev
);
423 errno_t rc
= ne2k_dev_init(nic_data
);
425 ne2k_dev_cleanup(dev
);
429 rc
= nic_report_address(nic_data
, &ne2k
->mac
);
431 ne2k_dev_cleanup(dev
);
435 fun
= ddf_fun_create(nic_get_ddf_dev(nic_data
), fun_exposed
, "port0");
437 ne2k_dev_cleanup(dev
);
441 nic_set_ddf_fun(nic_data
, fun
);
442 ddf_fun_set_ops(fun
, &ne2k_dev_ops
);
444 rc
= ddf_fun_bind(fun
);
446 ddf_fun_destroy(fun
);
447 ne2k_dev_cleanup(dev
);
451 rc
= ddf_fun_add_to_category(fun
, DEVICE_CATEGORY_NIC
);
454 ddf_fun_destroy(fun
);
461 static nic_iface_t ne2k_nic_iface
= {
462 .set_address
= ne2k_set_address
,
463 .get_device_info
= ne2k_get_device_info
,
464 .get_cable_state
= ne2k_get_cable_state
,
465 .get_operation_mode
= ne2k_get_operation_mode
,
468 static driver_ops_t ne2k_driver_ops
= {
469 .dev_add
= ne2k_dev_add
472 static driver_t ne2k_driver
= {
474 .driver_ops
= &ne2k_driver_ops
477 int main(int argc
, char *argv
[])
479 printf("%s: HelenOS NE 2000 network adapter driver\n", NAME
);
481 nic_driver_init(NAME
);
482 nic_driver_implement(&ne2k_driver_ops
, &ne2k_dev_ops
, &ne2k_nic_iface
);
484 return ddf_driver_main(&ne2k_driver
);