source: mainline/uspace/drv/nic/ne2k/ne2k.c@ 902f0906

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 902f0906 was 56fd7cf, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Make ddf_dev_t and ddf_fun_t opaque. This further tighthens the DDF interface.

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