source: mainline/uspace/drv/nic/ne2k/ne2k.c@ 0c0f823b

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

Rename DDF entry point add_device to dev_add.

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