source: mainline/uspace/drv/nic/ne2k/ne2k.c@ 961a5ee

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 961a5ee was 5a6cc679, checked in by Jenda <jenda.jzqk73@…>, 8 years ago

Merge commit '50f19b7ee8e94570b5c63896736c4eb49cfa18db' into forwardport

Not all ints are converted to errno_t in xhci tree yet, however it compiles and works :)

  • Property mode set to 100644
File size: 10.9 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 <device/hw_res.h>
43#include <stdlib.h>
44#include <str_error.h>
45#include <async.h>
46#include "dp8390.h"
47
48#define NAME "ne2k"
49
50/** Return the ISR from the interrupt call.
51 *
52 * @param[in] call The interrupt call.
53 *
54 */
55#define IRQ_GET_ISR(call) ((int) IPC_GET_ARG2(call))
56
57/** Return the TSR from the interrupt call.
58 *
59 * @param[in] call The interrupt call.
60 *
61 */
62#define IRQ_GET_TSR(call) ((int) IPC_GET_ARG3(call))
63
64#define DRIVER_DATA(dev) ((nic_t *) ddf_dev_data_get(dev))
65#define NE2K(device) ((ne2k_t *) nic_get_specific(DRIVER_DATA(device)))
66
67static irq_pio_range_t ne2k_ranges_prototype[] = {
68 {
69 .base = 0,
70 .size = NE2K_IO_SIZE,
71 }
72};
73
74/** NE2000 kernel interrupt command sequence.
75 *
76 */
77static irq_cmd_t ne2k_cmds_prototype[] = {
78 {
79 /* Read Interrupt Status Register */
80 .cmd = CMD_PIO_READ_8,
81 .addr = NULL,
82 .dstarg = 2
83 },
84 {
85 /* Mask supported interrupt causes */
86 .cmd = CMD_AND,
87 .value = (ISR_PRX | ISR_PTX | ISR_RXE | ISR_TXE | ISR_OVW |
88 ISR_CNT | ISR_RDC),
89 .srcarg = 2,
90 .dstarg = 3,
91 },
92 {
93 /* Predicate for accepting the interrupt */
94 .cmd = CMD_PREDICATE,
95 .value = 4,
96 .srcarg = 3
97 },
98 {
99 /*
100 * Mask future interrupts via
101 * Interrupt Mask Register
102 */
103 .cmd = CMD_PIO_WRITE_8,
104 .addr = NULL,
105 .value = 0
106 },
107 {
108 /* Acknowledge the current interrupt */
109 .cmd = CMD_PIO_WRITE_A_8,
110 .addr = NULL,
111 .srcarg = 3
112 },
113 {
114 /* Read Transmit Status Register */
115 .cmd = CMD_PIO_READ_8,
116 .addr = NULL,
117 .dstarg = 3
118 },
119 {
120 .cmd = CMD_ACCEPT
121 }
122};
123
124static void ne2k_interrupt_handler(ipc_call_t *, ddf_dev_t *);
125
126static errno_t ne2k_register_interrupt(nic_t *nic_data, cap_handle_t *handle)
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 return register_interrupt_handler(nic_get_ddf_dev(nic_data),
163 ne2k->irq, ne2k_interrupt_handler, &ne2k->code, handle);
164}
165
166static ddf_dev_ops_t ne2k_dev_ops;
167
168static void ne2k_dev_cleanup(ddf_dev_t *dev)
169{
170 if (ddf_dev_data_get(dev) != NULL) {
171 ne2k_t *ne2k = NE2K(dev);
172 if (ne2k) {
173 free(ne2k->code.ranges);
174 free(ne2k->code.cmds);
175 }
176 nic_unbind_and_destroy(dev);
177 }
178}
179
180static errno_t ne2k_dev_init(nic_t *nic_data)
181{
182 /* Get HW resources */
183 hw_res_list_parsed_t hw_res_parsed;
184 hw_res_list_parsed_init(&hw_res_parsed);
185
186 errno_t rc = nic_get_resources(nic_data, &hw_res_parsed);
187
188 if (rc != EOK)
189 goto failed;
190
191 if (hw_res_parsed.irqs.count == 0) {
192 rc = EINVAL;
193 goto failed;
194 }
195
196 if (hw_res_parsed.io_ranges.count == 0) {
197 rc = EINVAL;
198 goto failed;
199 }
200
201 if (hw_res_parsed.io_ranges.ranges[0].size < NE2K_IO_SIZE) {
202 rc = EINVAL;
203 goto failed;
204 }
205
206 ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
207 ne2k->irq = hw_res_parsed.irqs.irqs[0];
208
209 addr_range_t regs = hw_res_parsed.io_ranges.ranges[0];
210 ne2k->base_port = RNGABSPTR(regs);
211
212 hw_res_list_parsed_clean(&hw_res_parsed);
213
214 /* Enable programmed I/O */
215 if (pio_enable_range(&regs, &ne2k->port) != EOK)
216 return EADDRNOTAVAIL;
217
218 ne2k->data_port = ne2k->port + NE2K_DATA;
219 ne2k->receive_configuration = RCR_AB | RCR_AM;
220 ne2k->probed = false;
221 ne2k->up = false;
222
223 /* Find out whether the device is present. */
224 if (ne2k_probe(ne2k) != EOK)
225 return ENOENT;
226
227 ne2k->probed = true;
228
229 if (ne2k_register_interrupt(nic_data, NULL) != EOK)
230 return EINVAL;
231
232 return EOK;
233
234failed:
235 hw_res_list_parsed_clean(&hw_res_parsed);
236 return rc;
237}
238
239void ne2k_interrupt_handler(ipc_call_t *call, ddf_dev_t *dev)
240{
241 nic_t *nic_data = DRIVER_DATA(dev);
242 ne2k_interrupt(nic_data, IRQ_GET_ISR(*call), IRQ_GET_TSR(*call));
243}
244
245static errno_t ne2k_on_activating(nic_t *nic_data)
246{
247 ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
248
249 if (!ne2k->up) {
250 errno_t rc = ne2k_up(ne2k);
251 if (rc != EOK)
252 return rc;
253
254 rc = hw_res_enable_interrupt(ne2k->parent_sess, ne2k->irq);
255 if (rc != EOK) {
256 ne2k_down(ne2k);
257 return rc;
258 }
259 }
260 return EOK;
261}
262
263static errno_t ne2k_on_stopping(nic_t *nic_data)
264{
265 ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
266
267 (void) hw_res_disable_interrupt(ne2k->parent_sess, ne2k->irq);
268 ne2k->receive_configuration = RCR_AB | RCR_AM;
269 ne2k_down(ne2k);
270 return EOK;
271}
272
273static errno_t ne2k_set_address(ddf_fun_t *fun, const nic_address_t *address)
274{
275 nic_t *nic_data = DRIVER_DATA(ddf_fun_get_dev(fun));
276 errno_t rc = nic_report_address(nic_data, address);
277 if (rc != EOK) {
278 return EINVAL;
279 }
280 /* Note: some frame with previous physical address may slip to NIL here
281 * (for a moment the filtering is not exact), but ethernet should be OK with
282 * that. Some frames may also be lost, but this is not a problem.
283 */
284 ne2k_set_physical_address((ne2k_t *) nic_get_specific(nic_data), address);
285 return EOK;
286}
287
288static errno_t ne2k_on_unicast_mode_change(nic_t *nic_data,
289 nic_unicast_mode_t new_mode,
290 const nic_address_t *address_list, size_t address_count)
291{
292 ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
293 switch (new_mode) {
294 case NIC_UNICAST_BLOCKED:
295 ne2k_set_promisc_phys(ne2k, false);
296 nic_report_hw_filtering(nic_data, 0, -1, -1);
297 return EOK;
298 case NIC_UNICAST_DEFAULT:
299 ne2k_set_promisc_phys(ne2k, false);
300 nic_report_hw_filtering(nic_data, 1, -1, -1);
301 return EOK;
302 case NIC_UNICAST_LIST:
303 ne2k_set_promisc_phys(ne2k, true);
304 nic_report_hw_filtering(nic_data, 0, -1, -1);
305 return EOK;
306 case NIC_UNICAST_PROMISC:
307 ne2k_set_promisc_phys(ne2k, true);
308 nic_report_hw_filtering(nic_data, 1, -1, -1);
309 return EOK;
310 default:
311 return ENOTSUP;
312 }
313}
314
315static errno_t ne2k_on_multicast_mode_change(nic_t *nic_data,
316 nic_multicast_mode_t new_mode,
317 const nic_address_t *address_list, size_t address_count)
318{
319 ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
320 switch (new_mode) {
321 case NIC_MULTICAST_BLOCKED:
322 ne2k_set_accept_mcast(ne2k, false);
323 nic_report_hw_filtering(nic_data, -1, 1, -1);
324 return EOK;
325 case NIC_MULTICAST_LIST:
326 ne2k_set_accept_mcast(ne2k, true);
327 ne2k_set_mcast_hash(ne2k,
328 nic_mcast_hash(address_list, address_count));
329 nic_report_hw_filtering(nic_data, -1, 0, -1);
330 return EOK;
331 case NIC_MULTICAST_PROMISC:
332 ne2k_set_accept_mcast(ne2k, true);
333 ne2k_set_mcast_hash(ne2k, 0xFFFFFFFFFFFFFFFFllu);
334 nic_report_hw_filtering(nic_data, -1, 1, -1);
335 return EOK;
336 default:
337 return ENOTSUP;
338 }
339}
340
341static errno_t ne2k_on_broadcast_mode_change(nic_t *nic_data,
342 nic_broadcast_mode_t new_mode)
343{
344 ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
345 switch (new_mode) {
346 case NIC_BROADCAST_BLOCKED:
347 ne2k_set_accept_bcast(ne2k, false);
348 return EOK;
349 case NIC_BROADCAST_ACCEPTED:
350 ne2k_set_accept_bcast(ne2k, true);
351 return EOK;
352 default:
353 return ENOTSUP;
354 }
355}
356
357static errno_t ne2k_dev_add(ddf_dev_t *dev)
358{
359 ddf_fun_t *fun;
360
361 /* Allocate driver data for the device. */
362 nic_t *nic_data = nic_create_and_bind(dev);
363 if (nic_data == NULL)
364 return ENOMEM;
365
366 nic_set_send_frame_handler(nic_data, ne2k_send);
367 nic_set_state_change_handlers(nic_data,
368 ne2k_on_activating, NULL, ne2k_on_stopping);
369 nic_set_filtering_change_handlers(nic_data,
370 ne2k_on_unicast_mode_change, ne2k_on_multicast_mode_change,
371 ne2k_on_broadcast_mode_change, NULL, NULL);
372
373 ne2k_t *ne2k = malloc(sizeof(ne2k_t));
374 if (NULL != ne2k) {
375 memset(ne2k, 0, sizeof(ne2k_t));
376 nic_set_specific(nic_data, ne2k);
377 } else {
378 nic_unbind_and_destroy(dev);
379 return ENOMEM;
380 }
381
382 ne2k->dev = dev;
383 ne2k->parent_sess = ddf_dev_parent_sess_get(dev);
384 if (ne2k->parent_sess == NULL) {
385 ne2k_dev_cleanup(dev);
386 return ENOMEM;
387 }
388
389 errno_t rc = ne2k_dev_init(nic_data);
390 if (rc != EOK) {
391 ne2k_dev_cleanup(dev);
392 return rc;
393 }
394
395 rc = nic_report_address(nic_data, &ne2k->mac);
396 if (rc != EOK) {
397 ne2k_dev_cleanup(dev);
398 return rc;
399 }
400
401 fun = ddf_fun_create(nic_get_ddf_dev(nic_data), fun_exposed, "port0");
402 if (fun == NULL) {
403 ne2k_dev_cleanup(dev);
404 return ENOMEM;
405 }
406
407 nic_set_ddf_fun(nic_data, fun);
408 ddf_fun_set_ops(fun, &ne2k_dev_ops);
409
410 rc = ddf_fun_bind(fun);
411 if (rc != EOK) {
412 ddf_fun_destroy(fun);
413 ne2k_dev_cleanup(dev);
414 return rc;
415 }
416
417 rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
418 if (rc != EOK) {
419 ddf_fun_unbind(fun);
420 ddf_fun_destroy(fun);
421 return rc;
422 }
423
424 return EOK;
425}
426
427static nic_iface_t ne2k_nic_iface = {
428 .set_address = ne2k_set_address
429};
430
431static driver_ops_t ne2k_driver_ops = {
432 .dev_add = ne2k_dev_add
433};
434
435static driver_t ne2k_driver = {
436 .name = NAME,
437 .driver_ops = &ne2k_driver_ops
438};
439
440int main(int argc, char *argv[])
441{
442 printf("%s: HelenOS NE 2000 network adapter driver\n", NAME);
443
444 nic_driver_init(NAME);
445 nic_driver_implement(&ne2k_driver_ops, &ne2k_dev_ops, &ne2k_nic_iface);
446
447 return ddf_driver_main(&ne2k_driver);
448}
449
450/** @}
451 */
Note: See TracBrowser for help on using the repository browser.