1 | /*
|
---|
2 | * Copyright (c) 2011 Zdenek Bouska
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @file e1k.c
|
---|
30 | *
|
---|
31 | * Driver for Intel Pro/1000 8254x Family of Gigabit Ethernet Controllers
|
---|
32 | *
|
---|
33 | */
|
---|
34 |
|
---|
35 | /* XXX Fix this */
|
---|
36 | #define _DDF_DATA_IMPLANT
|
---|
37 |
|
---|
38 | #include <assert.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <adt/list.h>
|
---|
42 | #include <align.h>
|
---|
43 | #include <byteorder.h>
|
---|
44 | #include <sysinfo.h>
|
---|
45 | #include <ipc/irc.h>
|
---|
46 | #include <ipc/ns.h>
|
---|
47 | #include <ddi.h>
|
---|
48 | #include <as.h>
|
---|
49 | #include <ddf/log.h>
|
---|
50 | #include <ddf/interrupt.h>
|
---|
51 | #include <device/hw_res_parsed.h>
|
---|
52 | #include <device/pci.h>
|
---|
53 | #include <nic.h>
|
---|
54 | #include <ops/nic.h>
|
---|
55 | #include "e1k.h"
|
---|
56 |
|
---|
57 | #define NAME "e1k"
|
---|
58 |
|
---|
59 | #define E1000_DEFAULT_INTERRUPT_INTERVAL_USEC 250
|
---|
60 |
|
---|
61 | /* Must be power of 8 */
|
---|
62 | #define E1000_RX_FRAME_COUNT 128
|
---|
63 | #define E1000_TX_FRAME_COUNT 128
|
---|
64 |
|
---|
65 | #define E1000_RECEIVE_ADDRESS 16
|
---|
66 |
|
---|
67 | /** Maximum sending frame size */
|
---|
68 | #define E1000_MAX_SEND_FRAME_SIZE 2048
|
---|
69 | /** Maximum receiving frame size */
|
---|
70 | #define E1000_MAX_RECEIVE_FRAME_SIZE 2048
|
---|
71 |
|
---|
72 | /** nic_driver_data_t* -> e1000_t* cast */
|
---|
73 | #define DRIVER_DATA_NIC(nic) \
|
---|
74 | ((e1000_t *) nic_get_specific(nic))
|
---|
75 |
|
---|
76 | /** ddf_fun_t * -> nic_driver_data_t* cast */
|
---|
77 | #define NIC_DATA_FUN(fun) \
|
---|
78 | ((nic_t *) ddf_fun_data_get(fun))
|
---|
79 |
|
---|
80 | /** ddf_dev_t * -> nic_driver_data_t* cast */
|
---|
81 | #define NIC_DATA_DEV(dev) \
|
---|
82 | ((nic_t *) ddf_dev_data_get(dev))
|
---|
83 |
|
---|
84 | /** ddf_dev_t * -> e1000_t* cast */
|
---|
85 | #define DRIVER_DATA_DEV(dev) \
|
---|
86 | (DRIVER_DATA_NIC(NIC_DATA_DEV(dev)))
|
---|
87 |
|
---|
88 | /** ddf_fun_t * -> e1000_t* cast */
|
---|
89 | #define DRIVER_DATA_FUN(fun) \
|
---|
90 | (DRIVER_DATA_NIC(NIC_DATA_FUN(fun)))
|
---|
91 |
|
---|
92 | /** Cast pointer to uint64_t
|
---|
93 | *
|
---|
94 | * @param ptr Pointer to cast
|
---|
95 | *
|
---|
96 | * @return The uint64_t pointer representation.
|
---|
97 | *
|
---|
98 | */
|
---|
99 | #define PTR_TO_U64(ptr) ((uint64_t) ((uintptr_t) (ptr)))
|
---|
100 |
|
---|
101 | /** Cast the memaddr part to the void*
|
---|
102 | *
|
---|
103 | * @param memaddr The memaddr value
|
---|
104 | *
|
---|
105 | */
|
---|
106 | #define MEMADDR_TO_PTR(memaddr) ((void *) ((size_t) (memaddr)))
|
---|
107 |
|
---|
108 | #define E1000_REG_BASE(e1000) \
|
---|
109 | ((e1000)->reg_base_virt)
|
---|
110 |
|
---|
111 | #define E1000_REG_ADDR(e1000, reg) \
|
---|
112 | ((uint32_t *) (E1000_REG_BASE(e1000) + reg))
|
---|
113 |
|
---|
114 | #define E1000_REG_READ(e1000, reg) \
|
---|
115 | (pio_read_32(E1000_REG_ADDR(e1000, reg)))
|
---|
116 |
|
---|
117 | #define E1000_REG_WRITE(e1000, reg, value) \
|
---|
118 | (pio_write_32(E1000_REG_ADDR(e1000, reg), value))
|
---|
119 |
|
---|
120 | /** E1000 device data */
|
---|
121 | typedef struct {
|
---|
122 | /** Device configuration */
|
---|
123 | e1000_info_t info;
|
---|
124 |
|
---|
125 | /** Physical registers base address */
|
---|
126 | void *reg_base_phys;
|
---|
127 | /** Virtual registers base address */
|
---|
128 | void *reg_base_virt;
|
---|
129 |
|
---|
130 | /** Physical tx ring address */
|
---|
131 | void *tx_ring_phys;
|
---|
132 | /** Virtual tx ring address */
|
---|
133 | void *tx_ring_virt;
|
---|
134 |
|
---|
135 | /** Ring of TX frames, physical address */
|
---|
136 | void **tx_frame_phys;
|
---|
137 | /** Ring of TX frames, virtual address */
|
---|
138 | void **tx_frame_virt;
|
---|
139 |
|
---|
140 | /** Physical rx ring address */
|
---|
141 | void *rx_ring_phys;
|
---|
142 | /** Virtual rx ring address */
|
---|
143 | void *rx_ring_virt;
|
---|
144 |
|
---|
145 | /** Ring of RX frames, physical address */
|
---|
146 | void **rx_frame_phys;
|
---|
147 | /** Ring of RX frames, virtual address */
|
---|
148 | void **rx_frame_virt;
|
---|
149 |
|
---|
150 | /** VLAN tag */
|
---|
151 | uint16_t vlan_tag;
|
---|
152 |
|
---|
153 | /** Add VLAN tag to frame */
|
---|
154 | bool vlan_tag_add;
|
---|
155 |
|
---|
156 | /** Used unicast Receive Address count */
|
---|
157 | unsigned int unicast_ra_count;
|
---|
158 |
|
---|
159 | /** Used milticast Receive addrress count */
|
---|
160 | unsigned int multicast_ra_count;
|
---|
161 |
|
---|
162 | /** The irq assigned */
|
---|
163 | int irq;
|
---|
164 |
|
---|
165 | /** Lock for CTRL register */
|
---|
166 | fibril_mutex_t ctrl_lock;
|
---|
167 |
|
---|
168 | /** Lock for receiver */
|
---|
169 | fibril_mutex_t rx_lock;
|
---|
170 |
|
---|
171 | /** Lock for transmitter */
|
---|
172 | fibril_mutex_t tx_lock;
|
---|
173 |
|
---|
174 | /** Lock for EEPROM access */
|
---|
175 | fibril_mutex_t eeprom_lock;
|
---|
176 | } e1000_t;
|
---|
177 |
|
---|
178 | /** Global mutex for work with shared irq structure */
|
---|
179 | FIBRIL_MUTEX_INITIALIZE(irq_reg_mutex);
|
---|
180 |
|
---|
181 | static int e1000_get_address(e1000_t *, nic_address_t *);
|
---|
182 | static void e1000_eeprom_get_address(e1000_t *, nic_address_t *);
|
---|
183 | static int e1000_set_addr(ddf_fun_t *, const nic_address_t *);
|
---|
184 |
|
---|
185 | static int e1000_defective_get_mode(ddf_fun_t *, uint32_t *);
|
---|
186 | static int e1000_defective_set_mode(ddf_fun_t *, uint32_t);
|
---|
187 |
|
---|
188 | static int e1000_get_cable_state(ddf_fun_t *, nic_cable_state_t *);
|
---|
189 | static int e1000_get_device_info(ddf_fun_t *, nic_device_info_t *);
|
---|
190 | static int e1000_get_operation_mode(ddf_fun_t *, int *,
|
---|
191 | nic_channel_mode_t *, nic_role_t *);
|
---|
192 | static int e1000_set_operation_mode(ddf_fun_t *, int,
|
---|
193 | nic_channel_mode_t, nic_role_t);
|
---|
194 | static int e1000_autoneg_enable(ddf_fun_t *, uint32_t);
|
---|
195 | static int e1000_autoneg_disable(ddf_fun_t *);
|
---|
196 | static int e1000_autoneg_restart(ddf_fun_t *);
|
---|
197 |
|
---|
198 | static int e1000_vlan_set_tag(ddf_fun_t *, uint16_t, bool, bool);
|
---|
199 |
|
---|
200 | /** Network interface options for E1000 card driver */
|
---|
201 | static nic_iface_t e1000_nic_iface;
|
---|
202 |
|
---|
203 | /** Network interface options for E1000 card driver */
|
---|
204 | static nic_iface_t e1000_nic_iface = {
|
---|
205 | .set_address = &e1000_set_addr,
|
---|
206 | .get_device_info = &e1000_get_device_info,
|
---|
207 | .get_cable_state = &e1000_get_cable_state,
|
---|
208 | .get_operation_mode = &e1000_get_operation_mode,
|
---|
209 | .set_operation_mode = &e1000_set_operation_mode,
|
---|
210 | .autoneg_enable = &e1000_autoneg_enable,
|
---|
211 | .autoneg_disable = &e1000_autoneg_disable,
|
---|
212 | .autoneg_restart = &e1000_autoneg_restart,
|
---|
213 | .vlan_set_tag = &e1000_vlan_set_tag,
|
---|
214 | .defective_get_mode = &e1000_defective_get_mode,
|
---|
215 | .defective_set_mode = &e1000_defective_set_mode,
|
---|
216 | };
|
---|
217 |
|
---|
218 | /** Basic device operations for E1000 driver */
|
---|
219 | static ddf_dev_ops_t e1000_dev_ops;
|
---|
220 |
|
---|
221 | static int e1000_dev_add(ddf_dev_t *);
|
---|
222 |
|
---|
223 | /** Basic driver operations for E1000 driver */
|
---|
224 | static driver_ops_t e1000_driver_ops = {
|
---|
225 | .dev_add = e1000_dev_add
|
---|
226 | };
|
---|
227 |
|
---|
228 | /** Driver structure for E1000 driver */
|
---|
229 | static driver_t e1000_driver = {
|
---|
230 | .name = NAME,
|
---|
231 | .driver_ops = &e1000_driver_ops
|
---|
232 | };
|
---|
233 |
|
---|
234 | /* The default implementation callbacks */
|
---|
235 | static int e1000_on_activating(nic_t *);
|
---|
236 | static int e1000_on_stopping(nic_t *);
|
---|
237 | static void e1000_send_frame(nic_t *, void *, size_t);
|
---|
238 |
|
---|
239 | /** PIO ranges used in the IRQ code. */
|
---|
240 | irq_pio_range_t e1000_irq_pio_ranges[] = {
|
---|
241 | {
|
---|
242 | .base = 0,
|
---|
243 | .size = PAGE_SIZE, /* XXX */
|
---|
244 | }
|
---|
245 | };
|
---|
246 |
|
---|
247 | /** Commands to deal with interrupt
|
---|
248 | *
|
---|
249 | */
|
---|
250 | irq_cmd_t e1000_irq_commands[] = {
|
---|
251 | {
|
---|
252 | /* Get the interrupt status */
|
---|
253 | .cmd = CMD_PIO_READ_32,
|
---|
254 | .addr = NULL,
|
---|
255 | .dstarg = 2
|
---|
256 | },
|
---|
257 | {
|
---|
258 | .cmd = CMD_PREDICATE,
|
---|
259 | .value = 2,
|
---|
260 | .srcarg = 2
|
---|
261 | },
|
---|
262 | {
|
---|
263 | /* Disable interrupts until interrupt routine is finished */
|
---|
264 | .cmd = CMD_PIO_WRITE_32,
|
---|
265 | .addr = NULL,
|
---|
266 | .value = 0xffffffff
|
---|
267 | },
|
---|
268 | {
|
---|
269 | .cmd = CMD_ACCEPT
|
---|
270 | }
|
---|
271 | };
|
---|
272 |
|
---|
273 | /** Interrupt code definition */
|
---|
274 | irq_code_t e1000_irq_code = {
|
---|
275 | .rangecount = sizeof(e1000_irq_pio_ranges) /
|
---|
276 | sizeof(irq_pio_range_t),
|
---|
277 | .ranges = e1000_irq_pio_ranges,
|
---|
278 | .cmdcount = sizeof(e1000_irq_commands) / sizeof(irq_cmd_t),
|
---|
279 | .cmds = e1000_irq_commands
|
---|
280 | };
|
---|
281 |
|
---|
282 | /** Get the device information
|
---|
283 | *
|
---|
284 | * @param dev NIC device
|
---|
285 | * @param info Information to fill
|
---|
286 | *
|
---|
287 | * @return EOK
|
---|
288 | *
|
---|
289 | */
|
---|
290 | static int e1000_get_device_info(ddf_fun_t *dev, nic_device_info_t *info)
|
---|
291 | {
|
---|
292 | assert(dev);
|
---|
293 | assert(info);
|
---|
294 |
|
---|
295 | bzero(info, sizeof(nic_device_info_t));
|
---|
296 |
|
---|
297 | info->vendor_id = 0x8086;
|
---|
298 | str_cpy(info->vendor_name, NIC_VENDOR_MAX_LENGTH,
|
---|
299 | "Intel Corporation");
|
---|
300 | str_cpy(info->model_name, NIC_MODEL_MAX_LENGTH,
|
---|
301 | "Intel Pro");
|
---|
302 |
|
---|
303 | info->ethernet_support[ETH_10M] = ETH_10BASE_T;
|
---|
304 | info->ethernet_support[ETH_100M] = ETH_100BASE_TX;
|
---|
305 | info->ethernet_support[ETH_1000M] = ETH_1000BASE_T;
|
---|
306 |
|
---|
307 | return EOK;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /** Check the cable state
|
---|
311 | *
|
---|
312 | * @param[in] dev device
|
---|
313 | * @param[out] state state to fill
|
---|
314 | *
|
---|
315 | * @return EOK
|
---|
316 | *
|
---|
317 | */
|
---|
318 | static int e1000_get_cable_state(ddf_fun_t *fun, nic_cable_state_t *state)
|
---|
319 | {
|
---|
320 | e1000_t *e1000 = DRIVER_DATA_FUN(fun);
|
---|
321 | if (E1000_REG_READ(e1000, E1000_STATUS) & (STATUS_LU))
|
---|
322 | *state = NIC_CS_PLUGGED;
|
---|
323 | else
|
---|
324 | *state = NIC_CS_UNPLUGGED;
|
---|
325 |
|
---|
326 | return EOK;
|
---|
327 | }
|
---|
328 |
|
---|
329 | static uint16_t e1000_calculate_itr_interval_from_usecs(suseconds_t useconds)
|
---|
330 | {
|
---|
331 | return useconds * 4;
|
---|
332 | }
|
---|
333 |
|
---|
334 | /** Get operation mode of the device
|
---|
335 | *
|
---|
336 | */
|
---|
337 | static int e1000_get_operation_mode(ddf_fun_t *fun, int *speed,
|
---|
338 | nic_channel_mode_t *duplex, nic_role_t *role)
|
---|
339 | {
|
---|
340 | e1000_t *e1000 = DRIVER_DATA_FUN(fun);
|
---|
341 | uint32_t status = E1000_REG_READ(e1000, E1000_STATUS);
|
---|
342 |
|
---|
343 | if (status & STATUS_FD)
|
---|
344 | *duplex = NIC_CM_FULL_DUPLEX;
|
---|
345 | else
|
---|
346 | *duplex = NIC_CM_HALF_DUPLEX;
|
---|
347 |
|
---|
348 | uint32_t speed_bits =
|
---|
349 | (status >> STATUS_SPEED_SHIFT) & STATUS_SPEED_ALL;
|
---|
350 |
|
---|
351 | if (speed_bits == STATUS_SPEED_10)
|
---|
352 | *speed = 10;
|
---|
353 | else if (speed_bits == STATUS_SPEED_100)
|
---|
354 | *speed = 100;
|
---|
355 | else if ((speed_bits == STATUS_SPEED_1000A) ||
|
---|
356 | (speed_bits == STATUS_SPEED_1000B))
|
---|
357 | *speed = 1000;
|
---|
358 |
|
---|
359 | *role = NIC_ROLE_UNKNOWN;
|
---|
360 | return EOK;
|
---|
361 | }
|
---|
362 |
|
---|
363 | static void e1000_link_restart(e1000_t *e1000)
|
---|
364 | {
|
---|
365 | fibril_mutex_lock(&e1000->ctrl_lock);
|
---|
366 |
|
---|
367 | uint32_t ctrl = E1000_REG_READ(e1000, E1000_CTRL);
|
---|
368 |
|
---|
369 | if (ctrl & CTRL_SLU) {
|
---|
370 | ctrl &= ~(CTRL_SLU);
|
---|
371 | fibril_mutex_unlock(&e1000->ctrl_lock);
|
---|
372 | usleep(10);
|
---|
373 | fibril_mutex_lock(&e1000->ctrl_lock);
|
---|
374 | ctrl |= CTRL_SLU;
|
---|
375 | }
|
---|
376 |
|
---|
377 | fibril_mutex_unlock(&e1000->ctrl_lock);
|
---|
378 |
|
---|
379 | e1000_link_restart(e1000);
|
---|
380 | }
|
---|
381 |
|
---|
382 | /** Set operation mode of the device
|
---|
383 | *
|
---|
384 | */
|
---|
385 | static int e1000_set_operation_mode(ddf_fun_t *fun, int speed,
|
---|
386 | nic_channel_mode_t duplex, nic_role_t role)
|
---|
387 | {
|
---|
388 | if ((speed != 10) && (speed != 100) && (speed != 1000))
|
---|
389 | return EINVAL;
|
---|
390 |
|
---|
391 | if ((duplex != NIC_CM_HALF_DUPLEX) && (duplex != NIC_CM_FULL_DUPLEX))
|
---|
392 | return EINVAL;
|
---|
393 |
|
---|
394 | e1000_t *e1000 = DRIVER_DATA_FUN(fun);
|
---|
395 |
|
---|
396 | fibril_mutex_lock(&e1000->ctrl_lock);
|
---|
397 | uint32_t ctrl = E1000_REG_READ(e1000, E1000_CTRL);
|
---|
398 |
|
---|
399 | ctrl |= CTRL_FRCSPD;
|
---|
400 | ctrl |= CTRL_FRCDPLX;
|
---|
401 | ctrl &= ~(CTRL_ASDE);
|
---|
402 |
|
---|
403 | if (duplex == NIC_CM_FULL_DUPLEX)
|
---|
404 | ctrl |= CTRL_FD;
|
---|
405 | else
|
---|
406 | ctrl &= ~(CTRL_FD);
|
---|
407 |
|
---|
408 | ctrl &= ~(CTRL_SPEED_MASK);
|
---|
409 | if (speed == 1000)
|
---|
410 | ctrl |= CTRL_SPEED_1000 << CTRL_SPEED_SHIFT;
|
---|
411 | else if (speed == 100)
|
---|
412 | ctrl |= CTRL_SPEED_100 << CTRL_SPEED_SHIFT;
|
---|
413 | else
|
---|
414 | ctrl |= CTRL_SPEED_10 << CTRL_SPEED_SHIFT;
|
---|
415 |
|
---|
416 | E1000_REG_WRITE(e1000, E1000_CTRL, ctrl);
|
---|
417 |
|
---|
418 | fibril_mutex_unlock(&e1000->ctrl_lock);
|
---|
419 |
|
---|
420 | e1000_link_restart(e1000);
|
---|
421 |
|
---|
422 | return EOK;
|
---|
423 | }
|
---|
424 |
|
---|
425 | /** Enable auto-negotiation
|
---|
426 | *
|
---|
427 | * @param dev Device to update
|
---|
428 | * @param advertisement Ignored on E1000
|
---|
429 | *
|
---|
430 | * @return EOK if advertisement mode set successfully
|
---|
431 | *
|
---|
432 | */
|
---|
433 | static int e1000_autoneg_enable(ddf_fun_t *fun, uint32_t advertisement)
|
---|
434 | {
|
---|
435 | e1000_t *e1000 = DRIVER_DATA_FUN(fun);
|
---|
436 |
|
---|
437 | fibril_mutex_lock(&e1000->ctrl_lock);
|
---|
438 |
|
---|
439 | uint32_t ctrl = E1000_REG_READ(e1000, E1000_CTRL);
|
---|
440 |
|
---|
441 | ctrl &= ~(CTRL_FRCSPD);
|
---|
442 | ctrl &= ~(CTRL_FRCDPLX);
|
---|
443 | ctrl |= CTRL_ASDE;
|
---|
444 |
|
---|
445 | E1000_REG_WRITE(e1000, E1000_CTRL, ctrl);
|
---|
446 |
|
---|
447 | fibril_mutex_unlock(&e1000->ctrl_lock);
|
---|
448 |
|
---|
449 | e1000_link_restart(e1000);
|
---|
450 |
|
---|
451 | return EOK;
|
---|
452 | }
|
---|
453 |
|
---|
454 | /** Disable auto-negotiation
|
---|
455 | *
|
---|
456 | * @param dev Device to update
|
---|
457 | *
|
---|
458 | * @return EOK
|
---|
459 | *
|
---|
460 | */
|
---|
461 | static int e1000_autoneg_disable(ddf_fun_t *fun)
|
---|
462 | {
|
---|
463 | e1000_t *e1000 = DRIVER_DATA_FUN(fun);
|
---|
464 |
|
---|
465 | fibril_mutex_lock(&e1000->ctrl_lock);
|
---|
466 |
|
---|
467 | uint32_t ctrl = E1000_REG_READ(e1000, E1000_CTRL);
|
---|
468 |
|
---|
469 | ctrl |= CTRL_FRCSPD;
|
---|
470 | ctrl |= CTRL_FRCDPLX;
|
---|
471 | ctrl &= ~(CTRL_ASDE);
|
---|
472 |
|
---|
473 | E1000_REG_WRITE(e1000, E1000_CTRL, ctrl);
|
---|
474 |
|
---|
475 | fibril_mutex_unlock(&e1000->ctrl_lock);
|
---|
476 |
|
---|
477 | e1000_link_restart(e1000);
|
---|
478 |
|
---|
479 | return EOK;
|
---|
480 | }
|
---|
481 |
|
---|
482 | /** Restart auto-negotiation
|
---|
483 | *
|
---|
484 | * @param dev Device to update
|
---|
485 | *
|
---|
486 | * @return EOK if advertisement mode set successfully
|
---|
487 | *
|
---|
488 | */
|
---|
489 | static int e1000_autoneg_restart(ddf_fun_t *dev)
|
---|
490 | {
|
---|
491 | return e1000_autoneg_enable(dev, 0);
|
---|
492 | }
|
---|
493 |
|
---|
494 | /** Get state of acceptance of weird frames
|
---|
495 | *
|
---|
496 | * @param device Device to check
|
---|
497 | * @param[out] mode Current mode
|
---|
498 | *
|
---|
499 | */
|
---|
500 | static int e1000_defective_get_mode(ddf_fun_t *fun, uint32_t *mode)
|
---|
501 | {
|
---|
502 | e1000_t *e1000 = DRIVER_DATA_FUN(fun);
|
---|
503 |
|
---|
504 | *mode = 0;
|
---|
505 | uint32_t rctl = E1000_REG_READ(e1000, E1000_RCTL);
|
---|
506 | if (rctl & RCTL_SBP)
|
---|
507 | *mode = NIC_DEFECTIVE_BAD_CRC | NIC_DEFECTIVE_SHORT;
|
---|
508 |
|
---|
509 | return EOK;
|
---|
510 | };
|
---|
511 |
|
---|
512 | /** Set acceptance of weird frames
|
---|
513 | *
|
---|
514 | * @param device Device to update
|
---|
515 | * @param mode Mode to set
|
---|
516 | *
|
---|
517 | * @return ENOTSUP if the mode is not supported
|
---|
518 | * @return EOK of mode was set
|
---|
519 | *
|
---|
520 | */
|
---|
521 | static int e1000_defective_set_mode(ddf_fun_t *fun, uint32_t mode)
|
---|
522 | {
|
---|
523 | e1000_t *e1000 = DRIVER_DATA_FUN(fun);
|
---|
524 | int rc = EOK;
|
---|
525 |
|
---|
526 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
527 |
|
---|
528 | uint32_t rctl = E1000_REG_READ(e1000, E1000_RCTL);
|
---|
529 | bool short_mode = (mode & NIC_DEFECTIVE_SHORT ? true : false);
|
---|
530 | bool bad_mode = (mode & NIC_DEFECTIVE_BAD_CRC ? true : false);
|
---|
531 |
|
---|
532 | if (short_mode && bad_mode)
|
---|
533 | rctl |= RCTL_SBP;
|
---|
534 | else if ((!short_mode) && (!bad_mode))
|
---|
535 | rctl &= ~RCTL_SBP;
|
---|
536 | else
|
---|
537 | rc = ENOTSUP;
|
---|
538 |
|
---|
539 | E1000_REG_WRITE(e1000, E1000_RCTL, rctl);
|
---|
540 |
|
---|
541 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
542 | return rc;
|
---|
543 | };
|
---|
544 |
|
---|
545 | /** Write receive address to RA registr
|
---|
546 | *
|
---|
547 | * @param e1000 E1000 data structure
|
---|
548 | * @param position RA register position
|
---|
549 | * @param address Ethernet address
|
---|
550 | * @param set_av_bit Set the Addtess Valid bit
|
---|
551 | *
|
---|
552 | */
|
---|
553 | static void e1000_write_receive_address(e1000_t *e1000, unsigned int position,
|
---|
554 | const nic_address_t * address, bool set_av_bit)
|
---|
555 | {
|
---|
556 | uint8_t *mac0 = (uint8_t *) address->address;
|
---|
557 | uint8_t *mac1 = (uint8_t *) address->address + 1;
|
---|
558 | uint8_t *mac2 = (uint8_t *) address->address + 2;
|
---|
559 | uint8_t *mac3 = (uint8_t *) address->address + 3;
|
---|
560 | uint8_t *mac4 = (uint8_t *) address->address + 4;
|
---|
561 | uint8_t *mac5 = (uint8_t *) address->address + 5;
|
---|
562 |
|
---|
563 | uint32_t rah;
|
---|
564 | uint32_t ral;
|
---|
565 |
|
---|
566 | ral = ((*mac3) << 24) | ((*mac2) << 16) | ((*mac1) << 8) | (*mac0);
|
---|
567 | rah = ((*mac5) << 8) | ((*mac4));
|
---|
568 |
|
---|
569 | if (set_av_bit)
|
---|
570 | rah |= RAH_AV;
|
---|
571 | else
|
---|
572 | rah |= E1000_REG_READ(e1000, E1000_RAH_ARRAY(position)) & RAH_AV;
|
---|
573 |
|
---|
574 | E1000_REG_WRITE(e1000, E1000_RAH_ARRAY(position), rah);
|
---|
575 | E1000_REG_WRITE(e1000, E1000_RAL_ARRAY(position), ral);
|
---|
576 | }
|
---|
577 |
|
---|
578 | /** Disable receive address in RA registr
|
---|
579 | *
|
---|
580 | * Clear Address Valid bit
|
---|
581 | *
|
---|
582 | * @param e1000 E1000 data structure
|
---|
583 | * @param position RA register position
|
---|
584 | *
|
---|
585 | */
|
---|
586 | static void e1000_disable_receive_address(e1000_t *e1000, unsigned int position)
|
---|
587 | {
|
---|
588 | uint32_t rah = E1000_REG_READ(e1000, E1000_RAH_ARRAY(position));
|
---|
589 | rah = rah & ~RAH_AV;
|
---|
590 | E1000_REG_WRITE(e1000, E1000_RAH_ARRAY(position), rah);
|
---|
591 | }
|
---|
592 |
|
---|
593 | /** Clear all unicast addresses from RA registers
|
---|
594 | *
|
---|
595 | * @param e1000 E1000 data structure
|
---|
596 | *
|
---|
597 | */
|
---|
598 | static void e1000_clear_unicast_receive_addresses(e1000_t *e1000)
|
---|
599 | {
|
---|
600 | for (unsigned int ra_num = 1;
|
---|
601 | ra_num <= e1000->unicast_ra_count;
|
---|
602 | ra_num++)
|
---|
603 | e1000_disable_receive_address(e1000, ra_num);
|
---|
604 |
|
---|
605 | e1000->unicast_ra_count = 0;
|
---|
606 | }
|
---|
607 |
|
---|
608 | /** Clear all multicast addresses from RA registers
|
---|
609 | *
|
---|
610 | * @param e1000 E1000 data structure
|
---|
611 | *
|
---|
612 | */
|
---|
613 | static void e1000_clear_multicast_receive_addresses(e1000_t *e1000)
|
---|
614 | {
|
---|
615 | unsigned int first_multicast_ra_num =
|
---|
616 | E1000_RECEIVE_ADDRESS - e1000->multicast_ra_count;
|
---|
617 |
|
---|
618 | for (unsigned int ra_num = E1000_RECEIVE_ADDRESS - 1;
|
---|
619 | ra_num >= first_multicast_ra_num;
|
---|
620 | ra_num--)
|
---|
621 | e1000_disable_receive_address(e1000, ra_num);
|
---|
622 |
|
---|
623 | e1000->multicast_ra_count = 0;
|
---|
624 | }
|
---|
625 |
|
---|
626 | /** Return receive address filter positions count usable for unicast
|
---|
627 | *
|
---|
628 | * @param e1000 E1000 data structure
|
---|
629 | *
|
---|
630 | * @return receive address filter positions count usable for unicast
|
---|
631 | *
|
---|
632 | */
|
---|
633 | static unsigned int get_free_unicast_address_count(e1000_t *e1000)
|
---|
634 | {
|
---|
635 | return E1000_RECEIVE_ADDRESS - 1 - e1000->multicast_ra_count;
|
---|
636 | }
|
---|
637 |
|
---|
638 | /** Return receive address filter positions count usable for multicast
|
---|
639 | *
|
---|
640 | * @param e1000 E1000 data structure
|
---|
641 | *
|
---|
642 | * @return receive address filter positions count usable for multicast
|
---|
643 | *
|
---|
644 | */
|
---|
645 | static unsigned int get_free_multicast_address_count(e1000_t *e1000)
|
---|
646 | {
|
---|
647 | return E1000_RECEIVE_ADDRESS - 1 - e1000->unicast_ra_count;
|
---|
648 | }
|
---|
649 |
|
---|
650 | /** Write unicast receive addresses to receive address filter registers
|
---|
651 | *
|
---|
652 | * @param e1000 E1000 data structure
|
---|
653 | * @param addr Pointer to address array
|
---|
654 | * @param addr_cnt Address array count
|
---|
655 | *
|
---|
656 | */
|
---|
657 | static void e1000_add_unicast_receive_addresses(e1000_t *e1000,
|
---|
658 | const nic_address_t *addr, size_t addr_cnt)
|
---|
659 | {
|
---|
660 | assert(addr_cnt <= get_free_unicast_address_count(e1000));
|
---|
661 |
|
---|
662 | nic_address_t *addr_iterator = (nic_address_t *) addr;
|
---|
663 |
|
---|
664 | /* ra_num = 0 is primary address */
|
---|
665 | for (unsigned int ra_num = 1;
|
---|
666 | ra_num <= addr_cnt;
|
---|
667 | ra_num++) {
|
---|
668 | e1000_write_receive_address(e1000, ra_num, addr_iterator, true);
|
---|
669 | addr_iterator++;
|
---|
670 | }
|
---|
671 | }
|
---|
672 |
|
---|
673 | /** Write multicast receive addresses to receive address filter registers
|
---|
674 | *
|
---|
675 | * @param e1000 E1000 data structure
|
---|
676 | * @param addr Pointer to address array
|
---|
677 | * @param addr_cnt Address array count
|
---|
678 | *
|
---|
679 | */
|
---|
680 | static void e1000_add_multicast_receive_addresses(e1000_t *e1000,
|
---|
681 | const nic_address_t *addr, size_t addr_cnt)
|
---|
682 | {
|
---|
683 | assert(addr_cnt <= get_free_multicast_address_count(e1000));
|
---|
684 |
|
---|
685 | nic_address_t *addr_iterator = (nic_address_t *) addr;
|
---|
686 |
|
---|
687 | unsigned int first_multicast_ra_num = E1000_RECEIVE_ADDRESS - addr_cnt;
|
---|
688 | for (unsigned int ra_num = E1000_RECEIVE_ADDRESS - 1;
|
---|
689 | ra_num >= first_multicast_ra_num;
|
---|
690 | ra_num--) {
|
---|
691 | e1000_write_receive_address(e1000, ra_num, addr_iterator, true);
|
---|
692 | addr_iterator++;
|
---|
693 | }
|
---|
694 | }
|
---|
695 |
|
---|
696 | /** Disable receiving frames for default address
|
---|
697 | *
|
---|
698 | * @param e1000 E1000 data structure
|
---|
699 | *
|
---|
700 | */
|
---|
701 | static void disable_ra0_address_filter(e1000_t *e1000)
|
---|
702 | {
|
---|
703 | uint32_t rah0 = E1000_REG_READ(e1000, E1000_RAH_ARRAY(0));
|
---|
704 | rah0 = rah0 & ~RAH_AV;
|
---|
705 | E1000_REG_WRITE(e1000, E1000_RAH_ARRAY(0), rah0);
|
---|
706 | }
|
---|
707 |
|
---|
708 | /** Enable receiving frames for default address
|
---|
709 | *
|
---|
710 | * @param e1000 E1000 data structure
|
---|
711 | *
|
---|
712 | */
|
---|
713 | static void enable_ra0_address_filter(e1000_t *e1000)
|
---|
714 | {
|
---|
715 | uint32_t rah0 = E1000_REG_READ(e1000, E1000_RAH_ARRAY(0));
|
---|
716 | rah0 = rah0 | RAH_AV;
|
---|
717 | E1000_REG_WRITE(e1000, E1000_RAH_ARRAY(0), rah0);
|
---|
718 | }
|
---|
719 |
|
---|
720 | /** Disable unicast promiscuous mode
|
---|
721 | *
|
---|
722 | * @param e1000 E1000 data structure
|
---|
723 | *
|
---|
724 | */
|
---|
725 | static void e1000_disable_unicast_promisc(e1000_t *e1000)
|
---|
726 | {
|
---|
727 | uint32_t rctl = E1000_REG_READ(e1000, E1000_RCTL);
|
---|
728 | rctl = rctl & ~RCTL_UPE;
|
---|
729 | E1000_REG_WRITE(e1000, E1000_RCTL, rctl);
|
---|
730 | }
|
---|
731 |
|
---|
732 | /** Enable unicast promiscuous mode
|
---|
733 | *
|
---|
734 | * @param e1000 E1000 data structure
|
---|
735 | *
|
---|
736 | */
|
---|
737 | static void e1000_enable_unicast_promisc(e1000_t *e1000)
|
---|
738 | {
|
---|
739 | uint32_t rctl = E1000_REG_READ(e1000, E1000_RCTL);
|
---|
740 | rctl = rctl | RCTL_UPE;
|
---|
741 | E1000_REG_WRITE(e1000, E1000_RCTL, rctl);
|
---|
742 | }
|
---|
743 |
|
---|
744 | /** Disable multicast promiscuous mode
|
---|
745 | *
|
---|
746 | * @param e1000 E1000 data structure
|
---|
747 | *
|
---|
748 | */
|
---|
749 | static void e1000_disable_multicast_promisc(e1000_t *e1000)
|
---|
750 | {
|
---|
751 | uint32_t rctl = E1000_REG_READ(e1000, E1000_RCTL);
|
---|
752 | rctl = rctl & ~RCTL_MPE;
|
---|
753 | E1000_REG_WRITE(e1000, E1000_RCTL, rctl);
|
---|
754 | }
|
---|
755 |
|
---|
756 | /** Enable multicast promiscuous mode
|
---|
757 | *
|
---|
758 | * @param e1000 E1000 data structure
|
---|
759 | *
|
---|
760 | */
|
---|
761 | static void e1000_enable_multicast_promisc(e1000_t *e1000)
|
---|
762 | {
|
---|
763 | uint32_t rctl = E1000_REG_READ(e1000, E1000_RCTL);
|
---|
764 | rctl = rctl | RCTL_MPE;
|
---|
765 | E1000_REG_WRITE(e1000, E1000_RCTL, rctl);
|
---|
766 | }
|
---|
767 |
|
---|
768 | /** Enable accepting of broadcast frames
|
---|
769 | *
|
---|
770 | * @param e1000 E1000 data structure
|
---|
771 | *
|
---|
772 | */
|
---|
773 | static void e1000_enable_broadcast_accept(e1000_t *e1000)
|
---|
774 | {
|
---|
775 | uint32_t rctl = E1000_REG_READ(e1000, E1000_RCTL);
|
---|
776 | rctl = rctl | RCTL_BAM;
|
---|
777 | E1000_REG_WRITE(e1000, E1000_RCTL, rctl);
|
---|
778 | }
|
---|
779 |
|
---|
780 | /** Disable accepting of broadcast frames
|
---|
781 | *
|
---|
782 | * @param e1000 E1000 data structure
|
---|
783 | *
|
---|
784 | */
|
---|
785 | static void e1000_disable_broadcast_accept(e1000_t *e1000)
|
---|
786 | {
|
---|
787 | uint32_t rctl = E1000_REG_READ(e1000, E1000_RCTL);
|
---|
788 | rctl = rctl & ~RCTL_BAM;
|
---|
789 | E1000_REG_WRITE(e1000, E1000_RCTL, rctl);
|
---|
790 | }
|
---|
791 |
|
---|
792 | /** Enable VLAN filtering according to VFTA registers
|
---|
793 | *
|
---|
794 | * @param e1000 E1000 data structure
|
---|
795 | *
|
---|
796 | */
|
---|
797 | static void e1000_enable_vlan_filter(e1000_t *e1000)
|
---|
798 | {
|
---|
799 | uint32_t rctl = E1000_REG_READ(e1000, E1000_RCTL);
|
---|
800 | rctl = rctl | RCTL_VFE;
|
---|
801 | E1000_REG_WRITE(e1000, E1000_RCTL, rctl);
|
---|
802 | }
|
---|
803 |
|
---|
804 | /** Disable VLAN filtering
|
---|
805 | *
|
---|
806 | * @param e1000 E1000 data structure
|
---|
807 | *
|
---|
808 | */
|
---|
809 | static void e1000_disable_vlan_filter(e1000_t *e1000)
|
---|
810 | {
|
---|
811 | uint32_t rctl = E1000_REG_READ(e1000, E1000_RCTL);
|
---|
812 | rctl = rctl & ~RCTL_VFE;
|
---|
813 | E1000_REG_WRITE(e1000, E1000_RCTL, rctl);
|
---|
814 | }
|
---|
815 |
|
---|
816 | /** Set multicast frames acceptance mode
|
---|
817 | *
|
---|
818 | * @param nic NIC device to update
|
---|
819 | * @param mode Mode to set
|
---|
820 | * @param addr Address list (used in mode = NIC_MULTICAST_LIST)
|
---|
821 | * @param addr_cnt Length of address list (used in mode = NIC_MULTICAST_LIST)
|
---|
822 | *
|
---|
823 | * @return EOK
|
---|
824 | *
|
---|
825 | */
|
---|
826 | static int e1000_on_multicast_mode_change(nic_t *nic, nic_multicast_mode_t mode,
|
---|
827 | const nic_address_t *addr, size_t addr_cnt)
|
---|
828 | {
|
---|
829 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
830 | int rc = EOK;
|
---|
831 |
|
---|
832 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
833 |
|
---|
834 | switch (mode) {
|
---|
835 | case NIC_MULTICAST_BLOCKED:
|
---|
836 | e1000_clear_multicast_receive_addresses(e1000);
|
---|
837 | e1000_disable_multicast_promisc(e1000);
|
---|
838 | nic_report_hw_filtering(nic, -1, 1, -1);
|
---|
839 | break;
|
---|
840 | case NIC_MULTICAST_LIST:
|
---|
841 | e1000_clear_multicast_receive_addresses(e1000);
|
---|
842 | if (addr_cnt > get_free_multicast_address_count(e1000)) {
|
---|
843 | /*
|
---|
844 | * Future work: fill MTA table
|
---|
845 | * Not strictly neccessary, it only saves some compares
|
---|
846 | * in the NIC library.
|
---|
847 | */
|
---|
848 | e1000_enable_multicast_promisc(e1000);
|
---|
849 | nic_report_hw_filtering(nic, -1, 0, -1);
|
---|
850 | } else {
|
---|
851 | e1000_disable_multicast_promisc(e1000);
|
---|
852 | e1000_add_multicast_receive_addresses(e1000, addr, addr_cnt);
|
---|
853 | nic_report_hw_filtering(nic, -1, 1, -1);
|
---|
854 | }
|
---|
855 | break;
|
---|
856 | case NIC_MULTICAST_PROMISC:
|
---|
857 | e1000_enable_multicast_promisc(e1000);
|
---|
858 | e1000_clear_multicast_receive_addresses(e1000);
|
---|
859 | nic_report_hw_filtering(nic, -1, 1, -1);
|
---|
860 | break;
|
---|
861 | default:
|
---|
862 | rc = ENOTSUP;
|
---|
863 | break;
|
---|
864 | }
|
---|
865 |
|
---|
866 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
867 | return rc;
|
---|
868 | }
|
---|
869 |
|
---|
870 | /** Set unicast frames acceptance mode
|
---|
871 | *
|
---|
872 | * @param nic NIC device to update
|
---|
873 | * @param mode Mode to set
|
---|
874 | * @param addr Address list (used in mode = NIC_MULTICAST_LIST)
|
---|
875 | * @param addr_cnt Length of address list (used in mode = NIC_MULTICAST_LIST)
|
---|
876 | *
|
---|
877 | * @return EOK
|
---|
878 | *
|
---|
879 | */
|
---|
880 | static int e1000_on_unicast_mode_change(nic_t *nic, nic_unicast_mode_t mode,
|
---|
881 | const nic_address_t *addr, size_t addr_cnt)
|
---|
882 | {
|
---|
883 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
884 | int rc = EOK;
|
---|
885 |
|
---|
886 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
887 |
|
---|
888 | switch (mode) {
|
---|
889 | case NIC_UNICAST_BLOCKED:
|
---|
890 | disable_ra0_address_filter(e1000);
|
---|
891 | e1000_clear_unicast_receive_addresses(e1000);
|
---|
892 | e1000_disable_unicast_promisc(e1000);
|
---|
893 | nic_report_hw_filtering(nic, 1, -1, -1);
|
---|
894 | break;
|
---|
895 | case NIC_UNICAST_DEFAULT:
|
---|
896 | enable_ra0_address_filter(e1000);
|
---|
897 | e1000_clear_unicast_receive_addresses(e1000);
|
---|
898 | e1000_disable_unicast_promisc(e1000);
|
---|
899 | nic_report_hw_filtering(nic, 1, -1, -1);
|
---|
900 | break;
|
---|
901 | case NIC_UNICAST_LIST:
|
---|
902 | enable_ra0_address_filter(e1000);
|
---|
903 | e1000_clear_unicast_receive_addresses(e1000);
|
---|
904 | if (addr_cnt > get_free_unicast_address_count(e1000)) {
|
---|
905 | e1000_enable_unicast_promisc(e1000);
|
---|
906 | nic_report_hw_filtering(nic, 0, -1, -1);
|
---|
907 | } else {
|
---|
908 | e1000_disable_unicast_promisc(e1000);
|
---|
909 | e1000_add_unicast_receive_addresses(e1000, addr, addr_cnt);
|
---|
910 | nic_report_hw_filtering(nic, 1, -1, -1);
|
---|
911 | }
|
---|
912 | break;
|
---|
913 | case NIC_UNICAST_PROMISC:
|
---|
914 | e1000_enable_unicast_promisc(e1000);
|
---|
915 | enable_ra0_address_filter(e1000);
|
---|
916 | e1000_clear_unicast_receive_addresses(e1000);
|
---|
917 | nic_report_hw_filtering(nic, 1, -1, -1);
|
---|
918 | break;
|
---|
919 | default:
|
---|
920 | rc = ENOTSUP;
|
---|
921 | break;
|
---|
922 | }
|
---|
923 |
|
---|
924 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
925 | return rc;
|
---|
926 | }
|
---|
927 |
|
---|
928 | /** Set broadcast frames acceptance mode
|
---|
929 | *
|
---|
930 | * @param nic NIC device to update
|
---|
931 | * @param mode Mode to set
|
---|
932 | *
|
---|
933 | * @return EOK
|
---|
934 | *
|
---|
935 | */
|
---|
936 | static int e1000_on_broadcast_mode_change(nic_t *nic, nic_broadcast_mode_t mode)
|
---|
937 | {
|
---|
938 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
939 | int rc = EOK;
|
---|
940 |
|
---|
941 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
942 |
|
---|
943 | switch (mode) {
|
---|
944 | case NIC_BROADCAST_BLOCKED:
|
---|
945 | e1000_disable_broadcast_accept(e1000);
|
---|
946 | break;
|
---|
947 | case NIC_BROADCAST_ACCEPTED:
|
---|
948 | e1000_enable_broadcast_accept(e1000);
|
---|
949 | break;
|
---|
950 | default:
|
---|
951 | rc = ENOTSUP;
|
---|
952 | break;
|
---|
953 | }
|
---|
954 |
|
---|
955 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
956 | return rc;
|
---|
957 | }
|
---|
958 |
|
---|
959 | /** Check if receiving is enabled
|
---|
960 | *
|
---|
961 | * @param e1000 E1000 data structure
|
---|
962 | *
|
---|
963 | * @return true if receiving is enabled
|
---|
964 | *
|
---|
965 | */
|
---|
966 | static bool e1000_is_rx_enabled(e1000_t *e1000)
|
---|
967 | {
|
---|
968 | if (E1000_REG_READ(e1000, E1000_RCTL) & (RCTL_EN))
|
---|
969 | return true;
|
---|
970 |
|
---|
971 | return false;
|
---|
972 | }
|
---|
973 |
|
---|
974 | /** Enable receiving
|
---|
975 | *
|
---|
976 | * @param e1000 E1000 data structure
|
---|
977 | *
|
---|
978 | */
|
---|
979 | static void e1000_enable_rx(e1000_t *e1000)
|
---|
980 | {
|
---|
981 | /* Set Receive Enable Bit */
|
---|
982 | E1000_REG_WRITE(e1000, E1000_RCTL,
|
---|
983 | E1000_REG_READ(e1000, E1000_RCTL) | (RCTL_EN));
|
---|
984 | }
|
---|
985 |
|
---|
986 | /** Disable receiving
|
---|
987 | *
|
---|
988 | * @param e1000 E1000 data structure
|
---|
989 | *
|
---|
990 | */
|
---|
991 | static void e1000_disable_rx(e1000_t *e1000)
|
---|
992 | {
|
---|
993 | /* Clear Receive Enable Bit */
|
---|
994 | E1000_REG_WRITE(e1000, E1000_RCTL,
|
---|
995 | E1000_REG_READ(e1000, E1000_RCTL) & ~(RCTL_EN));
|
---|
996 | }
|
---|
997 |
|
---|
998 | /** Set VLAN mask
|
---|
999 | *
|
---|
1000 | * @param nic NIC device to update
|
---|
1001 | * @param vlan_mask VLAN mask
|
---|
1002 | *
|
---|
1003 | */
|
---|
1004 | static void e1000_on_vlan_mask_change(nic_t *nic,
|
---|
1005 | const nic_vlan_mask_t *vlan_mask)
|
---|
1006 | {
|
---|
1007 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
1008 |
|
---|
1009 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
1010 |
|
---|
1011 | if (vlan_mask) {
|
---|
1012 | /*
|
---|
1013 | * Disable receiving, so that frame matching
|
---|
1014 | * partially written VLAN is not received.
|
---|
1015 | */
|
---|
1016 | bool rx_enabled = e1000_is_rx_enabled(e1000);
|
---|
1017 | if (rx_enabled)
|
---|
1018 | e1000_disable_rx(e1000);
|
---|
1019 |
|
---|
1020 | for (unsigned int i = 0; i < NIC_VLAN_BITMAP_SIZE; i += 4) {
|
---|
1021 | uint32_t bitmap_part =
|
---|
1022 | ((uint32_t) vlan_mask->bitmap[i]) |
|
---|
1023 | (((uint32_t) vlan_mask->bitmap[i + 1]) << 8) |
|
---|
1024 | (((uint32_t) vlan_mask->bitmap[i + 2]) << 16) |
|
---|
1025 | (((uint32_t) vlan_mask->bitmap[i + 3]) << 24);
|
---|
1026 | E1000_REG_WRITE(e1000, E1000_VFTA_ARRAY(i / 4), bitmap_part);
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | e1000_enable_vlan_filter(e1000);
|
---|
1030 | if (rx_enabled)
|
---|
1031 | e1000_enable_rx(e1000);
|
---|
1032 | } else
|
---|
1033 | e1000_disable_vlan_filter(e1000);
|
---|
1034 |
|
---|
1035 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 | /** Set VLAN mask
|
---|
1039 | *
|
---|
1040 | * @param device E1000 device
|
---|
1041 | * @param tag VLAN tag
|
---|
1042 | *
|
---|
1043 | * @return EOK
|
---|
1044 | * @return ENOTSUP
|
---|
1045 | *
|
---|
1046 | */
|
---|
1047 | static int e1000_vlan_set_tag(ddf_fun_t *fun, uint16_t tag, bool add,
|
---|
1048 | bool strip)
|
---|
1049 | {
|
---|
1050 | /* VLAN CFI bit cannot be set */
|
---|
1051 | if (tag & VLANTAG_CFI)
|
---|
1052 | return ENOTSUP;
|
---|
1053 |
|
---|
1054 | /*
|
---|
1055 | * CTRL.VME is neccessary for both strip and add
|
---|
1056 | * but CTRL.VME means stripping tags on receive.
|
---|
1057 | */
|
---|
1058 | if (!strip && add)
|
---|
1059 | return ENOTSUP;
|
---|
1060 |
|
---|
1061 | e1000_t *e1000 = DRIVER_DATA_FUN(fun);
|
---|
1062 |
|
---|
1063 | e1000->vlan_tag = tag;
|
---|
1064 | e1000->vlan_tag_add = add;
|
---|
1065 |
|
---|
1066 | fibril_mutex_lock(&e1000->ctrl_lock);
|
---|
1067 |
|
---|
1068 | uint32_t ctrl = E1000_REG_READ(e1000, E1000_CTRL);
|
---|
1069 | if (strip)
|
---|
1070 | ctrl |= CTRL_VME;
|
---|
1071 | else
|
---|
1072 | ctrl &= ~CTRL_VME;
|
---|
1073 |
|
---|
1074 | E1000_REG_WRITE(e1000, E1000_CTRL, ctrl);
|
---|
1075 |
|
---|
1076 | fibril_mutex_unlock(&e1000->ctrl_lock);
|
---|
1077 | return EOK;
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | /** Fill receive descriptor with new empty buffer
|
---|
1081 | *
|
---|
1082 | * Store frame in e1000->rx_frame_phys
|
---|
1083 | *
|
---|
1084 | * @param nic NIC data stricture
|
---|
1085 | * @param offset Receive descriptor offset
|
---|
1086 | *
|
---|
1087 | */
|
---|
1088 | static void e1000_fill_new_rx_descriptor(nic_t *nic, size_t offset)
|
---|
1089 | {
|
---|
1090 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
1091 |
|
---|
1092 | e1000_rx_descriptor_t *rx_descriptor = (e1000_rx_descriptor_t *)
|
---|
1093 | (e1000->rx_ring_virt + offset * sizeof(e1000_rx_descriptor_t));
|
---|
1094 |
|
---|
1095 | rx_descriptor->phys_addr = PTR_TO_U64(e1000->rx_frame_phys[offset]);
|
---|
1096 | rx_descriptor->length = 0;
|
---|
1097 | rx_descriptor->checksum = 0;
|
---|
1098 | rx_descriptor->status = 0;
|
---|
1099 | rx_descriptor->errors = 0;
|
---|
1100 | rx_descriptor->special = 0;
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | /** Clear receive descriptor
|
---|
1104 | *
|
---|
1105 | * @param e1000 E1000 data
|
---|
1106 | * @param offset Receive descriptor offset
|
---|
1107 | *
|
---|
1108 | */
|
---|
1109 | static void e1000_clear_rx_descriptor(e1000_t *e1000, unsigned int offset)
|
---|
1110 | {
|
---|
1111 | e1000_rx_descriptor_t *rx_descriptor = (e1000_rx_descriptor_t *)
|
---|
1112 | (e1000->rx_ring_virt + offset * sizeof(e1000_rx_descriptor_t));
|
---|
1113 |
|
---|
1114 | rx_descriptor->length = 0;
|
---|
1115 | rx_descriptor->checksum = 0;
|
---|
1116 | rx_descriptor->status = 0;
|
---|
1117 | rx_descriptor->errors = 0;
|
---|
1118 | rx_descriptor->special = 0;
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | /** Clear receive descriptor
|
---|
1122 | *
|
---|
1123 | * @param nic NIC data
|
---|
1124 | * @param offset Receive descriptor offset
|
---|
1125 | *
|
---|
1126 | */
|
---|
1127 | static void e1000_clear_tx_descriptor(nic_t *nic, unsigned int offset)
|
---|
1128 | {
|
---|
1129 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
1130 |
|
---|
1131 | e1000_tx_descriptor_t *tx_descriptor = (e1000_tx_descriptor_t *)
|
---|
1132 | (e1000->tx_ring_virt + offset * sizeof(e1000_tx_descriptor_t));
|
---|
1133 |
|
---|
1134 | tx_descriptor->phys_addr = 0;
|
---|
1135 | tx_descriptor->length = 0;
|
---|
1136 | tx_descriptor->checksum_offset = 0;
|
---|
1137 | tx_descriptor->command = 0;
|
---|
1138 | tx_descriptor->status = 0;
|
---|
1139 | tx_descriptor->checksum_start_field = 0;
|
---|
1140 | tx_descriptor->special = 0;
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | /** Increment tail pointer for receive or transmit ring
|
---|
1144 | *
|
---|
1145 | * @param tail Old Tail
|
---|
1146 | * @param descriptors_count Ring length
|
---|
1147 | *
|
---|
1148 | * @return New tail
|
---|
1149 | *
|
---|
1150 | */
|
---|
1151 | static uint32_t e1000_inc_tail(uint32_t tail, uint32_t descriptors_count)
|
---|
1152 | {
|
---|
1153 | if (tail + 1 == descriptors_count)
|
---|
1154 | return 0;
|
---|
1155 | else
|
---|
1156 | return tail + 1;
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | /** Receive frames
|
---|
1160 | *
|
---|
1161 | * @param nic NIC data
|
---|
1162 | *
|
---|
1163 | */
|
---|
1164 | static void e1000_receive_frames(nic_t *nic)
|
---|
1165 | {
|
---|
1166 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
1167 |
|
---|
1168 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
1169 |
|
---|
1170 | uint32_t *tail_addr = E1000_REG_ADDR(e1000, E1000_RDT);
|
---|
1171 | uint32_t next_tail = e1000_inc_tail(*tail_addr, E1000_RX_FRAME_COUNT);
|
---|
1172 |
|
---|
1173 | e1000_rx_descriptor_t *rx_descriptor = (e1000_rx_descriptor_t *)
|
---|
1174 | (e1000->rx_ring_virt + next_tail * sizeof(e1000_rx_descriptor_t));
|
---|
1175 |
|
---|
1176 | while (rx_descriptor->status & 0x01) {
|
---|
1177 | uint32_t frame_size = rx_descriptor->length - E1000_CRC_SIZE;
|
---|
1178 |
|
---|
1179 | nic_frame_t *frame = nic_alloc_frame(nic, frame_size);
|
---|
1180 | if (frame != NULL) {
|
---|
1181 | memcpy(frame->data, e1000->rx_frame_virt[next_tail], frame_size);
|
---|
1182 | nic_received_frame(nic, frame);
|
---|
1183 | } else {
|
---|
1184 | ddf_msg(LVL_ERROR, "Memory allocation failed. Frame dropped.");
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | e1000_fill_new_rx_descriptor(nic, next_tail);
|
---|
1188 |
|
---|
1189 | *tail_addr = e1000_inc_tail(*tail_addr, E1000_RX_FRAME_COUNT);
|
---|
1190 | next_tail = e1000_inc_tail(*tail_addr, E1000_RX_FRAME_COUNT);
|
---|
1191 |
|
---|
1192 | rx_descriptor = (e1000_rx_descriptor_t *)
|
---|
1193 | (e1000->rx_ring_virt + next_tail * sizeof(e1000_rx_descriptor_t));
|
---|
1194 | }
|
---|
1195 |
|
---|
1196 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | /** Enable E1000 interupts
|
---|
1200 | *
|
---|
1201 | * @param e1000 E1000 data structure
|
---|
1202 | *
|
---|
1203 | */
|
---|
1204 | static void e1000_enable_interrupts(e1000_t *e1000)
|
---|
1205 | {
|
---|
1206 | E1000_REG_WRITE(e1000, E1000_IMS, ICR_RXT0);
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | /** Disable E1000 interupts
|
---|
1210 | *
|
---|
1211 | * @param e1000 E1000 data structure
|
---|
1212 | *
|
---|
1213 | */
|
---|
1214 | static void e1000_disable_interrupts(e1000_t *e1000)
|
---|
1215 | {
|
---|
1216 | E1000_REG_WRITE(e1000, E1000_IMS, 0);
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | /** Interrupt handler implementation
|
---|
1220 | *
|
---|
1221 | * This function is called from e1000_interrupt_handler()
|
---|
1222 | * and e1000_poll()
|
---|
1223 | *
|
---|
1224 | * @param nic NIC data
|
---|
1225 | * @param icr ICR register value
|
---|
1226 | *
|
---|
1227 | */
|
---|
1228 | static void e1000_interrupt_handler_impl(nic_t *nic, uint32_t icr)
|
---|
1229 | {
|
---|
1230 | if (icr & ICR_RXT0)
|
---|
1231 | e1000_receive_frames(nic);
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | /** Handle device interrupt
|
---|
1235 | *
|
---|
1236 | * @param dev E1000 device
|
---|
1237 | * @param iid IPC call id
|
---|
1238 | * @param icall IPC call structure
|
---|
1239 | *
|
---|
1240 | */
|
---|
1241 | static void e1000_interrupt_handler(ddf_dev_t *dev, ipc_callid_t iid,
|
---|
1242 | ipc_call_t *icall)
|
---|
1243 | {
|
---|
1244 | uint32_t icr = (uint32_t) IPC_GET_ARG2(*icall);
|
---|
1245 | nic_t *nic = NIC_DATA_DEV(dev);
|
---|
1246 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
1247 |
|
---|
1248 | e1000_interrupt_handler_impl(nic, icr);
|
---|
1249 | e1000_enable_interrupts(e1000);
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | /** Register interrupt handler for the card in the system
|
---|
1253 | *
|
---|
1254 | * Note: The global irq_reg_mutex is locked because of work with global
|
---|
1255 | * structure.
|
---|
1256 | *
|
---|
1257 | * @param nic Driver data
|
---|
1258 | *
|
---|
1259 | * @return EOK if the handler was registered
|
---|
1260 | * @return Negative error code otherwise
|
---|
1261 | *
|
---|
1262 | */
|
---|
1263 | inline static int e1000_register_int_handler(nic_t *nic)
|
---|
1264 | {
|
---|
1265 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
1266 |
|
---|
1267 | /* Lock the mutex in whole driver while working with global structure */
|
---|
1268 | fibril_mutex_lock(&irq_reg_mutex);
|
---|
1269 |
|
---|
1270 | e1000_irq_code.ranges[0].base = (uintptr_t) e1000->reg_base_phys;
|
---|
1271 | e1000_irq_code.cmds[0].addr = e1000->reg_base_phys + E1000_ICR;
|
---|
1272 | e1000_irq_code.cmds[2].addr = e1000->reg_base_phys + E1000_IMC;
|
---|
1273 |
|
---|
1274 | int rc = register_interrupt_handler(nic_get_ddf_dev(nic),
|
---|
1275 | e1000->irq, e1000_interrupt_handler, &e1000_irq_code);
|
---|
1276 |
|
---|
1277 | fibril_mutex_unlock(&irq_reg_mutex);
|
---|
1278 | return rc;
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | /** Force receiving all frames in the receive buffer
|
---|
1282 | *
|
---|
1283 | * @param nic NIC data
|
---|
1284 | *
|
---|
1285 | */
|
---|
1286 | static void e1000_poll(nic_t *nic)
|
---|
1287 | {
|
---|
1288 | assert(nic);
|
---|
1289 |
|
---|
1290 | e1000_t *e1000 = nic_get_specific(nic);
|
---|
1291 | assert(e1000);
|
---|
1292 |
|
---|
1293 | uint32_t icr = E1000_REG_READ(e1000, E1000_ICR);
|
---|
1294 | e1000_interrupt_handler_impl(nic, icr);
|
---|
1295 | }
|
---|
1296 |
|
---|
1297 | /** Calculates ITR register interrupt from timeval structure
|
---|
1298 | *
|
---|
1299 | * @param period Period
|
---|
1300 | *
|
---|
1301 | */
|
---|
1302 | static uint16_t e1000_calculate_itr_interval(const struct timeval *period)
|
---|
1303 | {
|
---|
1304 | // TODO: use also tv_sec
|
---|
1305 | return e1000_calculate_itr_interval_from_usecs(period->tv_usec);
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 | /** Set polling mode
|
---|
1309 | *
|
---|
1310 | * @param device Device to set
|
---|
1311 | * @param mode Mode to set
|
---|
1312 | * @param period Period for NIC_POLL_PERIODIC
|
---|
1313 | *
|
---|
1314 | * @return EOK if succeed
|
---|
1315 | * @return ENOTSUP if the mode is not supported
|
---|
1316 | *
|
---|
1317 | */
|
---|
1318 | static int e1000_poll_mode_change(nic_t *nic, nic_poll_mode_t mode,
|
---|
1319 | const struct timeval *period)
|
---|
1320 | {
|
---|
1321 | assert(nic);
|
---|
1322 |
|
---|
1323 | e1000_t *e1000 = nic_get_specific(nic);
|
---|
1324 | assert(e1000);
|
---|
1325 |
|
---|
1326 | switch (mode) {
|
---|
1327 | case NIC_POLL_IMMEDIATE:
|
---|
1328 | E1000_REG_WRITE(e1000, E1000_ITR, 0);
|
---|
1329 | e1000_enable_interrupts(e1000);
|
---|
1330 | break;
|
---|
1331 | case NIC_POLL_ON_DEMAND:
|
---|
1332 | e1000_disable_interrupts(e1000);
|
---|
1333 | break;
|
---|
1334 | case NIC_POLL_PERIODIC:
|
---|
1335 | assert(period);
|
---|
1336 | uint16_t itr_interval = e1000_calculate_itr_interval(period);
|
---|
1337 | E1000_REG_WRITE(e1000, E1000_ITR, (uint32_t) itr_interval);
|
---|
1338 | e1000_enable_interrupts(e1000);
|
---|
1339 | break;
|
---|
1340 | default:
|
---|
1341 | return ENOTSUP;
|
---|
1342 | }
|
---|
1343 |
|
---|
1344 | return EOK;
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | /** Initialize receive registers
|
---|
1348 | *
|
---|
1349 | * @param e1000 E1000 data structure
|
---|
1350 | *
|
---|
1351 | */
|
---|
1352 | static void e1000_initialize_rx_registers(e1000_t *e1000)
|
---|
1353 | {
|
---|
1354 | E1000_REG_WRITE(e1000, E1000_RDLEN, E1000_RX_FRAME_COUNT * 16);
|
---|
1355 | E1000_REG_WRITE(e1000, E1000_RDH, 0);
|
---|
1356 |
|
---|
1357 | /* It is not posible to let HW use all descriptors */
|
---|
1358 | E1000_REG_WRITE(e1000, E1000_RDT, E1000_RX_FRAME_COUNT - 1);
|
---|
1359 |
|
---|
1360 | /* Set Broadcast Enable Bit */
|
---|
1361 | E1000_REG_WRITE(e1000, E1000_RCTL, RCTL_BAM);
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 | /** Initialize receive structure
|
---|
1365 | *
|
---|
1366 | * @param nic NIC data
|
---|
1367 | *
|
---|
1368 | * @return EOK if succeed
|
---|
1369 | * @return Negative error code otherwise
|
---|
1370 | *
|
---|
1371 | */
|
---|
1372 | static int e1000_initialize_rx_structure(nic_t *nic)
|
---|
1373 | {
|
---|
1374 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
1375 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
1376 |
|
---|
1377 | int rc = dmamem_map_anonymous(
|
---|
1378 | E1000_RX_FRAME_COUNT * sizeof(e1000_rx_descriptor_t),
|
---|
1379 | AS_AREA_READ | AS_AREA_WRITE, 0, &e1000->rx_ring_phys,
|
---|
1380 | &e1000->rx_ring_virt);
|
---|
1381 | if (rc != EOK)
|
---|
1382 | return rc;
|
---|
1383 |
|
---|
1384 | E1000_REG_WRITE(e1000, E1000_RDBAH,
|
---|
1385 | (uint32_t) (PTR_TO_U64(e1000->rx_ring_phys) >> 32));
|
---|
1386 | E1000_REG_WRITE(e1000, E1000_RDBAL,
|
---|
1387 | (uint32_t) PTR_TO_U64(e1000->rx_ring_phys));
|
---|
1388 |
|
---|
1389 | e1000->rx_frame_phys =
|
---|
1390 | calloc(E1000_RX_FRAME_COUNT, sizeof(void *));
|
---|
1391 | e1000->rx_frame_virt =
|
---|
1392 | calloc(E1000_RX_FRAME_COUNT, sizeof(void *));
|
---|
1393 | if (e1000->rx_frame_phys == NULL || e1000->rx_frame_virt == NULL) {
|
---|
1394 | rc = ENOMEM;
|
---|
1395 | goto error;
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | size_t i;
|
---|
1399 | void *frame_virt;
|
---|
1400 | void *frame_phys;
|
---|
1401 |
|
---|
1402 | for (i = 0; i < E1000_RX_FRAME_COUNT; i++) {
|
---|
1403 | rc = dmamem_map_anonymous(
|
---|
1404 | E1000_MAX_SEND_FRAME_SIZE, AS_AREA_READ | AS_AREA_WRITE,
|
---|
1405 | 0, &frame_phys, &frame_virt);
|
---|
1406 | if (rc != EOK)
|
---|
1407 | goto error;
|
---|
1408 |
|
---|
1409 | e1000->rx_frame_virt[i] = frame_virt;
|
---|
1410 | e1000->rx_frame_phys[i] = frame_phys;
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | /* Write descriptor */
|
---|
1414 | for (i = 0; i < E1000_RX_FRAME_COUNT; i++)
|
---|
1415 | e1000_fill_new_rx_descriptor(nic, i);
|
---|
1416 |
|
---|
1417 | e1000_initialize_rx_registers(e1000);
|
---|
1418 |
|
---|
1419 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
1420 | return EOK;
|
---|
1421 |
|
---|
1422 | error:
|
---|
1423 | for (i = 0; i < E1000_RX_FRAME_COUNT; i++) {
|
---|
1424 | if (e1000->rx_frame_virt[i] != NULL) {
|
---|
1425 | dmamem_unmap_anonymous(e1000->rx_frame_virt[i]);
|
---|
1426 | e1000->rx_frame_virt[i] = NULL;
|
---|
1427 | e1000->rx_frame_phys[i] = NULL;
|
---|
1428 | }
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | if (e1000->rx_frame_phys != NULL) {
|
---|
1432 | free(e1000->rx_frame_phys);
|
---|
1433 | e1000->rx_frame_phys = NULL;
|
---|
1434 | }
|
---|
1435 |
|
---|
1436 | if (e1000->rx_frame_virt != NULL) {
|
---|
1437 | free(e1000->rx_frame_virt);
|
---|
1438 | e1000->rx_frame_phys = NULL;
|
---|
1439 | }
|
---|
1440 |
|
---|
1441 | return rc;
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 | /** Uninitialize receive structure
|
---|
1445 | *
|
---|
1446 | * @param nic NIC data
|
---|
1447 | *
|
---|
1448 | */
|
---|
1449 | static void e1000_uninitialize_rx_structure(nic_t *nic)
|
---|
1450 | {
|
---|
1451 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
1452 |
|
---|
1453 | /* Write descriptor */
|
---|
1454 | for (unsigned int offset = 0; offset < E1000_RX_FRAME_COUNT; offset++) {
|
---|
1455 | dmamem_unmap_anonymous(e1000->rx_frame_virt[offset]);
|
---|
1456 | e1000->rx_frame_virt[offset] = NULL;
|
---|
1457 | e1000->rx_frame_phys[offset] = NULL;
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 | free(e1000->rx_frame_virt);
|
---|
1461 | free(e1000->rx_frame_phys);
|
---|
1462 | e1000->rx_frame_virt = NULL;
|
---|
1463 | e1000->rx_frame_phys = NULL;
|
---|
1464 | dmamem_unmap_anonymous(e1000->rx_ring_virt);
|
---|
1465 | }
|
---|
1466 |
|
---|
1467 | /** Clear receive descriptor ring
|
---|
1468 | *
|
---|
1469 | * @param e1000 E1000 data
|
---|
1470 | *
|
---|
1471 | */
|
---|
1472 | static void e1000_clear_rx_ring(e1000_t *e1000)
|
---|
1473 | {
|
---|
1474 | /* Write descriptor */
|
---|
1475 | for (unsigned int offset = 0;
|
---|
1476 | offset < E1000_RX_FRAME_COUNT;
|
---|
1477 | offset++)
|
---|
1478 | e1000_clear_rx_descriptor(e1000, offset);
|
---|
1479 | }
|
---|
1480 |
|
---|
1481 | /** Initialize filters
|
---|
1482 | *
|
---|
1483 | * @param e1000 E1000 data
|
---|
1484 | *
|
---|
1485 | */
|
---|
1486 | static void e1000_initialize_filters(e1000_t *e1000)
|
---|
1487 | {
|
---|
1488 | /* Initialize address filter */
|
---|
1489 | e1000->unicast_ra_count = 0;
|
---|
1490 | e1000->multicast_ra_count = 0;
|
---|
1491 | e1000_clear_unicast_receive_addresses(e1000);
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 | /** Initialize VLAN
|
---|
1495 | *
|
---|
1496 | * @param e1000 E1000 data
|
---|
1497 | *
|
---|
1498 | */
|
---|
1499 | static void e1000_initialize_vlan(e1000_t *e1000)
|
---|
1500 | {
|
---|
1501 | e1000->vlan_tag_add = false;
|
---|
1502 | }
|
---|
1503 |
|
---|
1504 | /** Fill MAC address from EEPROM to RA[0] register
|
---|
1505 | *
|
---|
1506 | * @param e1000 E1000 data
|
---|
1507 | *
|
---|
1508 | */
|
---|
1509 | static void e1000_fill_mac_from_eeprom(e1000_t *e1000)
|
---|
1510 | {
|
---|
1511 | /* MAC address from eeprom to RA[0] */
|
---|
1512 | nic_address_t address;
|
---|
1513 | e1000_eeprom_get_address(e1000, &address);
|
---|
1514 | e1000_write_receive_address(e1000, 0, &address, true);
|
---|
1515 | }
|
---|
1516 |
|
---|
1517 | /** Initialize other registers
|
---|
1518 | *
|
---|
1519 | * @param dev E1000 data.
|
---|
1520 | *
|
---|
1521 | * @return EOK if succeed
|
---|
1522 | * @return Negative error code otherwise
|
---|
1523 | *
|
---|
1524 | */
|
---|
1525 | static void e1000_initialize_registers(e1000_t *e1000)
|
---|
1526 | {
|
---|
1527 | E1000_REG_WRITE(e1000, E1000_ITR,
|
---|
1528 | e1000_calculate_itr_interval_from_usecs(
|
---|
1529 | E1000_DEFAULT_INTERRUPT_INTERVAL_USEC));
|
---|
1530 | E1000_REG_WRITE(e1000, E1000_FCAH, 0);
|
---|
1531 | E1000_REG_WRITE(e1000, E1000_FCAL, 0);
|
---|
1532 | E1000_REG_WRITE(e1000, E1000_FCT, 0);
|
---|
1533 | E1000_REG_WRITE(e1000, E1000_FCTTV, 0);
|
---|
1534 | E1000_REG_WRITE(e1000, E1000_VET, VET_VALUE);
|
---|
1535 | E1000_REG_WRITE(e1000, E1000_CTRL, CTRL_ASDE);
|
---|
1536 | }
|
---|
1537 |
|
---|
1538 | /** Initialize transmit registers
|
---|
1539 | *
|
---|
1540 | * @param e1000 E1000 data.
|
---|
1541 | *
|
---|
1542 | */
|
---|
1543 | static void e1000_initialize_tx_registers(e1000_t *e1000)
|
---|
1544 | {
|
---|
1545 | E1000_REG_WRITE(e1000, E1000_TDLEN, E1000_TX_FRAME_COUNT * 16);
|
---|
1546 | E1000_REG_WRITE(e1000, E1000_TDH, 0);
|
---|
1547 | E1000_REG_WRITE(e1000, E1000_TDT, 0);
|
---|
1548 |
|
---|
1549 | E1000_REG_WRITE(e1000, E1000_TIPG,
|
---|
1550 | 10 << TIPG_IPGT_SHIFT |
|
---|
1551 | 8 << TIPG_IPGR1_SHIFT |
|
---|
1552 | 6 << TIPG_IPGR2_SHIFT);
|
---|
1553 |
|
---|
1554 | E1000_REG_WRITE(e1000, E1000_TCTL,
|
---|
1555 | 0x0F << TCTL_CT_SHIFT /* Collision Threshold */ |
|
---|
1556 | 0x40 << TCTL_COLD_SHIFT /* Collision Distance */ |
|
---|
1557 | TCTL_PSP /* Pad Short Packets */);
|
---|
1558 | }
|
---|
1559 |
|
---|
1560 | /** Initialize transmit structure
|
---|
1561 | *
|
---|
1562 | * @param e1000 E1000 data.
|
---|
1563 | *
|
---|
1564 | */
|
---|
1565 | static int e1000_initialize_tx_structure(e1000_t *e1000)
|
---|
1566 | {
|
---|
1567 | size_t i;
|
---|
1568 |
|
---|
1569 | fibril_mutex_lock(&e1000->tx_lock);
|
---|
1570 |
|
---|
1571 | e1000->tx_ring_phys = NULL;
|
---|
1572 | e1000->tx_ring_virt = NULL;
|
---|
1573 | e1000->tx_frame_phys = NULL;
|
---|
1574 | e1000->tx_frame_virt = NULL;
|
---|
1575 |
|
---|
1576 | int rc = dmamem_map_anonymous(
|
---|
1577 | E1000_TX_FRAME_COUNT * sizeof(e1000_tx_descriptor_t),
|
---|
1578 | AS_AREA_READ | AS_AREA_WRITE, 0, &e1000->tx_ring_phys,
|
---|
1579 | &e1000->tx_ring_virt);
|
---|
1580 | if (rc != EOK)
|
---|
1581 | goto error;
|
---|
1582 |
|
---|
1583 | bzero(e1000->tx_ring_virt,
|
---|
1584 | E1000_TX_FRAME_COUNT * sizeof(e1000_tx_descriptor_t));
|
---|
1585 |
|
---|
1586 | e1000->tx_frame_phys = calloc(E1000_TX_FRAME_COUNT, sizeof(void *));
|
---|
1587 | e1000->tx_frame_virt = calloc(E1000_TX_FRAME_COUNT, sizeof(void *));
|
---|
1588 |
|
---|
1589 | if (e1000->tx_frame_phys == NULL || e1000->tx_frame_virt == NULL) {
|
---|
1590 | rc = ENOMEM;
|
---|
1591 | goto error;
|
---|
1592 | }
|
---|
1593 |
|
---|
1594 | for (i = 0; i < E1000_TX_FRAME_COUNT; i++) {
|
---|
1595 | rc = dmamem_map_anonymous(
|
---|
1596 | E1000_MAX_SEND_FRAME_SIZE, AS_AREA_READ | AS_AREA_WRITE,
|
---|
1597 | 0, &e1000->tx_frame_phys[i], &e1000->tx_frame_virt[i]);
|
---|
1598 | if (rc != EOK)
|
---|
1599 | goto error;
|
---|
1600 | }
|
---|
1601 |
|
---|
1602 | E1000_REG_WRITE(e1000, E1000_TDBAH,
|
---|
1603 | (uint32_t) (PTR_TO_U64(e1000->tx_ring_phys) >> 32));
|
---|
1604 | E1000_REG_WRITE(e1000, E1000_TDBAL,
|
---|
1605 | (uint32_t) PTR_TO_U64(e1000->tx_ring_phys));
|
---|
1606 |
|
---|
1607 | e1000_initialize_tx_registers(e1000);
|
---|
1608 |
|
---|
1609 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
1610 | return EOK;
|
---|
1611 |
|
---|
1612 | error:
|
---|
1613 | if (e1000->tx_ring_virt != NULL) {
|
---|
1614 | dmamem_unmap_anonymous(e1000->tx_ring_virt);
|
---|
1615 | e1000->tx_ring_virt = NULL;
|
---|
1616 | }
|
---|
1617 |
|
---|
1618 | if (e1000->tx_frame_phys != NULL && e1000->tx_frame_virt != NULL) {
|
---|
1619 | for (i = 0; i < E1000_TX_FRAME_COUNT; i++) {
|
---|
1620 | if (e1000->tx_frame_virt[i] != NULL) {
|
---|
1621 | dmamem_unmap_anonymous(e1000->tx_frame_virt[i]);
|
---|
1622 | e1000->tx_frame_virt[i] = NULL;
|
---|
1623 | e1000->tx_frame_phys[i] = NULL;
|
---|
1624 | }
|
---|
1625 | }
|
---|
1626 | }
|
---|
1627 |
|
---|
1628 | if (e1000->tx_frame_phys != NULL) {
|
---|
1629 | free(e1000->tx_frame_phys);
|
---|
1630 | e1000->tx_frame_phys = NULL;
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | if (e1000->tx_frame_virt != NULL) {
|
---|
1634 | free(e1000->tx_frame_virt);
|
---|
1635 | e1000->tx_frame_phys = NULL;
|
---|
1636 | }
|
---|
1637 |
|
---|
1638 | return rc;
|
---|
1639 | }
|
---|
1640 |
|
---|
1641 | /** Uninitialize transmit structure
|
---|
1642 | *
|
---|
1643 | * @param nic NIC data
|
---|
1644 | *
|
---|
1645 | */
|
---|
1646 | static void e1000_uninitialize_tx_structure(e1000_t *e1000)
|
---|
1647 | {
|
---|
1648 | size_t i;
|
---|
1649 |
|
---|
1650 | for (i = 0; i < E1000_TX_FRAME_COUNT; i++) {
|
---|
1651 | dmamem_unmap_anonymous(e1000->tx_frame_virt[i]);
|
---|
1652 | e1000->tx_frame_virt[i] = NULL;
|
---|
1653 | e1000->tx_frame_phys[i] = NULL;
|
---|
1654 | }
|
---|
1655 |
|
---|
1656 | if (e1000->tx_frame_phys != NULL) {
|
---|
1657 | free(e1000->tx_frame_phys);
|
---|
1658 | e1000->tx_frame_phys = NULL;
|
---|
1659 | }
|
---|
1660 |
|
---|
1661 | if (e1000->tx_frame_virt != NULL) {
|
---|
1662 | free(e1000->tx_frame_virt);
|
---|
1663 | e1000->tx_frame_phys = NULL;
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 | dmamem_unmap_anonymous(e1000->tx_ring_virt);
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 | /** Clear transmit descriptor ring
|
---|
1670 | *
|
---|
1671 | * @param nic NIC data
|
---|
1672 | *
|
---|
1673 | */
|
---|
1674 | static void e1000_clear_tx_ring(nic_t *nic)
|
---|
1675 | {
|
---|
1676 | /* Write descriptor */
|
---|
1677 | for (unsigned int offset = 0;
|
---|
1678 | offset < E1000_TX_FRAME_COUNT;
|
---|
1679 | offset++)
|
---|
1680 | e1000_clear_tx_descriptor(nic, offset);
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | /** Enable transmit
|
---|
1684 | *
|
---|
1685 | * @param e1000 E1000 data
|
---|
1686 | *
|
---|
1687 | */
|
---|
1688 | static void e1000_enable_tx(e1000_t *e1000)
|
---|
1689 | {
|
---|
1690 | /* Set Transmit Enable Bit */
|
---|
1691 | E1000_REG_WRITE(e1000, E1000_TCTL,
|
---|
1692 | E1000_REG_READ(e1000, E1000_TCTL) | (TCTL_EN));
|
---|
1693 | }
|
---|
1694 |
|
---|
1695 | /** Disable transmit
|
---|
1696 | *
|
---|
1697 | * @param e1000 E1000 data
|
---|
1698 | *
|
---|
1699 | */
|
---|
1700 | static void e1000_disable_tx(e1000_t *e1000)
|
---|
1701 | {
|
---|
1702 | /* Clear Transmit Enable Bit */
|
---|
1703 | E1000_REG_WRITE(e1000, E1000_TCTL,
|
---|
1704 | E1000_REG_READ(e1000, E1000_TCTL) & ~(TCTL_EN));
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | /** Reset E1000 device
|
---|
1708 | *
|
---|
1709 | * @param e1000 The E1000 data
|
---|
1710 | *
|
---|
1711 | */
|
---|
1712 | static int e1000_reset(nic_t *nic)
|
---|
1713 | {
|
---|
1714 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
1715 |
|
---|
1716 | E1000_REG_WRITE(e1000, E1000_CTRL, CTRL_RST);
|
---|
1717 |
|
---|
1718 | /* Wait for the reset */
|
---|
1719 | usleep(20);
|
---|
1720 |
|
---|
1721 | /* check if RST_BIT cleared */
|
---|
1722 | if (E1000_REG_READ(e1000, E1000_CTRL) & (CTRL_RST))
|
---|
1723 | return EINVAL;
|
---|
1724 |
|
---|
1725 | e1000_initialize_registers(e1000);
|
---|
1726 | e1000_initialize_rx_registers(e1000);
|
---|
1727 | e1000_initialize_tx_registers(e1000);
|
---|
1728 | e1000_fill_mac_from_eeprom(e1000);
|
---|
1729 | e1000_initialize_filters(e1000);
|
---|
1730 | e1000_initialize_vlan(e1000);
|
---|
1731 |
|
---|
1732 | return EOK;
|
---|
1733 | }
|
---|
1734 |
|
---|
1735 | /** Activate the device to receive and transmit frames
|
---|
1736 | *
|
---|
1737 | * @param nic NIC driver data
|
---|
1738 | *
|
---|
1739 | * @return EOK if activated successfully
|
---|
1740 | * @return Error code otherwise
|
---|
1741 | *
|
---|
1742 | */
|
---|
1743 | static int e1000_on_activating(nic_t *nic)
|
---|
1744 | {
|
---|
1745 | assert(nic);
|
---|
1746 |
|
---|
1747 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
1748 |
|
---|
1749 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
1750 | fibril_mutex_lock(&e1000->tx_lock);
|
---|
1751 | fibril_mutex_lock(&e1000->ctrl_lock);
|
---|
1752 |
|
---|
1753 | e1000_enable_interrupts(e1000);
|
---|
1754 |
|
---|
1755 | nic_enable_interrupt(nic, e1000->irq);
|
---|
1756 |
|
---|
1757 | e1000_clear_rx_ring(e1000);
|
---|
1758 | e1000_enable_rx(e1000);
|
---|
1759 |
|
---|
1760 | e1000_clear_tx_ring(nic);
|
---|
1761 | e1000_enable_tx(e1000);
|
---|
1762 |
|
---|
1763 | uint32_t ctrl = E1000_REG_READ(e1000, E1000_CTRL);
|
---|
1764 | ctrl |= CTRL_SLU;
|
---|
1765 | E1000_REG_WRITE(e1000, E1000_CTRL, ctrl);
|
---|
1766 |
|
---|
1767 | fibril_mutex_unlock(&e1000->ctrl_lock);
|
---|
1768 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
1769 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
1770 |
|
---|
1771 | return EOK;
|
---|
1772 | }
|
---|
1773 |
|
---|
1774 | /** Callback for NIC_STATE_DOWN change
|
---|
1775 | *
|
---|
1776 | * @param nic NIC driver data
|
---|
1777 | *
|
---|
1778 | * @return EOK if succeed
|
---|
1779 | * @return Error code otherwise
|
---|
1780 | *
|
---|
1781 | */
|
---|
1782 | static int e1000_on_down_unlocked(nic_t *nic)
|
---|
1783 | {
|
---|
1784 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
1785 |
|
---|
1786 | uint32_t ctrl = E1000_REG_READ(e1000, E1000_CTRL);
|
---|
1787 | ctrl &= ~CTRL_SLU;
|
---|
1788 | E1000_REG_WRITE(e1000, E1000_CTRL, ctrl);
|
---|
1789 |
|
---|
1790 | e1000_disable_tx(e1000);
|
---|
1791 | e1000_disable_rx(e1000);
|
---|
1792 |
|
---|
1793 | nic_disable_interrupt(nic, e1000->irq);
|
---|
1794 | e1000_disable_interrupts(e1000);
|
---|
1795 |
|
---|
1796 | /*
|
---|
1797 | * Wait for the for the end of all data
|
---|
1798 | * transfers to descriptors.
|
---|
1799 | */
|
---|
1800 | usleep(100);
|
---|
1801 |
|
---|
1802 | return EOK;
|
---|
1803 | }
|
---|
1804 |
|
---|
1805 | /** Callback for NIC_STATE_DOWN change
|
---|
1806 | *
|
---|
1807 | * @param nic NIC driver data
|
---|
1808 | *
|
---|
1809 | * @return EOK if succeed
|
---|
1810 | * @return Error code otherwise
|
---|
1811 | *
|
---|
1812 | */
|
---|
1813 | static int e1000_on_down(nic_t *nic)
|
---|
1814 | {
|
---|
1815 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
1816 |
|
---|
1817 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
1818 | fibril_mutex_lock(&e1000->tx_lock);
|
---|
1819 | fibril_mutex_lock(&e1000->ctrl_lock);
|
---|
1820 |
|
---|
1821 | int rc = e1000_on_down_unlocked(nic);
|
---|
1822 |
|
---|
1823 | fibril_mutex_unlock(&e1000->ctrl_lock);
|
---|
1824 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
1825 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
1826 |
|
---|
1827 | return rc;
|
---|
1828 | }
|
---|
1829 |
|
---|
1830 | /** Callback for NIC_STATE_STOPPED change
|
---|
1831 | *
|
---|
1832 | * @param nic NIC driver data
|
---|
1833 | *
|
---|
1834 | * @return EOK if succeed
|
---|
1835 | * @return Error code otherwise
|
---|
1836 | *
|
---|
1837 | */
|
---|
1838 | static int e1000_on_stopping(nic_t *nic)
|
---|
1839 | {
|
---|
1840 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
1841 |
|
---|
1842 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
1843 | fibril_mutex_lock(&e1000->tx_lock);
|
---|
1844 | fibril_mutex_lock(&e1000->ctrl_lock);
|
---|
1845 |
|
---|
1846 | int rc = e1000_on_down_unlocked(nic);
|
---|
1847 | if (rc == EOK)
|
---|
1848 | rc = e1000_reset(nic);
|
---|
1849 |
|
---|
1850 | fibril_mutex_unlock(&e1000->ctrl_lock);
|
---|
1851 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
1852 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
1853 |
|
---|
1854 | return rc;
|
---|
1855 | }
|
---|
1856 |
|
---|
1857 | /** Create driver data structure
|
---|
1858 | *
|
---|
1859 | * @return Intialized device data structure or NULL
|
---|
1860 | *
|
---|
1861 | */
|
---|
1862 | static e1000_t *e1000_create_dev_data(ddf_dev_t *dev)
|
---|
1863 | {
|
---|
1864 | nic_t *nic = nic_create_and_bind(dev);
|
---|
1865 | if (!nic)
|
---|
1866 | return NULL;
|
---|
1867 |
|
---|
1868 | e1000_t *e1000 = malloc(sizeof(e1000_t));
|
---|
1869 | if (!e1000) {
|
---|
1870 | nic_unbind_and_destroy(dev);
|
---|
1871 | return NULL;
|
---|
1872 | }
|
---|
1873 |
|
---|
1874 | bzero(e1000, sizeof(e1000_t));
|
---|
1875 |
|
---|
1876 | nic_set_specific(nic, e1000);
|
---|
1877 | nic_set_send_frame_handler(nic, e1000_send_frame);
|
---|
1878 | nic_set_state_change_handlers(nic, e1000_on_activating,
|
---|
1879 | e1000_on_down, e1000_on_stopping);
|
---|
1880 | nic_set_filtering_change_handlers(nic,
|
---|
1881 | e1000_on_unicast_mode_change, e1000_on_multicast_mode_change,
|
---|
1882 | e1000_on_broadcast_mode_change, NULL, e1000_on_vlan_mask_change);
|
---|
1883 | nic_set_poll_handlers(nic, e1000_poll_mode_change, e1000_poll);
|
---|
1884 |
|
---|
1885 | fibril_mutex_initialize(&e1000->ctrl_lock);
|
---|
1886 | fibril_mutex_initialize(&e1000->rx_lock);
|
---|
1887 | fibril_mutex_initialize(&e1000->tx_lock);
|
---|
1888 | fibril_mutex_initialize(&e1000->eeprom_lock);
|
---|
1889 |
|
---|
1890 | return e1000;
|
---|
1891 | }
|
---|
1892 |
|
---|
1893 | /** Delete driver data structure
|
---|
1894 | *
|
---|
1895 | * @param data E1000 device data structure
|
---|
1896 | *
|
---|
1897 | */
|
---|
1898 | inline static void e1000_delete_dev_data(ddf_dev_t *dev)
|
---|
1899 | {
|
---|
1900 | assert(dev);
|
---|
1901 |
|
---|
1902 | if (ddf_dev_data_get(dev) != NULL)
|
---|
1903 | nic_unbind_and_destroy(dev);
|
---|
1904 | }
|
---|
1905 |
|
---|
1906 | /** Clean up the E1000 device structure.
|
---|
1907 | *
|
---|
1908 | * @param dev Device structure.
|
---|
1909 | *
|
---|
1910 | */
|
---|
1911 | static void e1000_dev_cleanup(ddf_dev_t *dev)
|
---|
1912 | {
|
---|
1913 | assert(dev);
|
---|
1914 |
|
---|
1915 | e1000_delete_dev_data(dev);
|
---|
1916 | }
|
---|
1917 |
|
---|
1918 | /** Fill the irq and io_addr part of device data structure
|
---|
1919 | *
|
---|
1920 | * The hw_resources must be obtained before calling this function
|
---|
1921 | *
|
---|
1922 | * @param dev Device structure
|
---|
1923 | * @param hw_resources Hardware resources obtained from the parent device
|
---|
1924 | *
|
---|
1925 | * @return EOK if succeed
|
---|
1926 | * @return Negative error code otherwise
|
---|
1927 | *
|
---|
1928 | */
|
---|
1929 | static int e1000_fill_resource_info(ddf_dev_t *dev,
|
---|
1930 | const hw_res_list_parsed_t *hw_resources)
|
---|
1931 | {
|
---|
1932 | e1000_t *e1000 = DRIVER_DATA_DEV(dev);
|
---|
1933 |
|
---|
1934 | if (hw_resources->irqs.count != 1)
|
---|
1935 | return EINVAL;
|
---|
1936 |
|
---|
1937 | e1000->irq = hw_resources->irqs.irqs[0];
|
---|
1938 | e1000->reg_base_phys =
|
---|
1939 | MEMADDR_TO_PTR(hw_resources->mem_ranges.ranges[0].address);
|
---|
1940 |
|
---|
1941 | return EOK;
|
---|
1942 | }
|
---|
1943 |
|
---|
1944 | /** Obtain information about hardware resources of the device
|
---|
1945 | *
|
---|
1946 | * The device must be connected to the parent
|
---|
1947 | *
|
---|
1948 | * @param dev Device structure
|
---|
1949 | *
|
---|
1950 | * @return EOK if succeed
|
---|
1951 | * @return Negative error code otherwise
|
---|
1952 | *
|
---|
1953 | */
|
---|
1954 | static int e1000_get_resource_info(ddf_dev_t *dev)
|
---|
1955 | {
|
---|
1956 | assert(dev != NULL);
|
---|
1957 | assert(NIC_DATA_DEV(dev) != NULL);
|
---|
1958 |
|
---|
1959 | hw_res_list_parsed_t hw_res_parsed;
|
---|
1960 | hw_res_list_parsed_init(&hw_res_parsed);
|
---|
1961 |
|
---|
1962 | /* Get hw resources form parent driver */
|
---|
1963 | int rc = nic_get_resources(NIC_DATA_DEV(dev), &hw_res_parsed);
|
---|
1964 | if (rc != EOK)
|
---|
1965 | return rc;
|
---|
1966 |
|
---|
1967 | /* Fill resources information to the device */
|
---|
1968 | rc = e1000_fill_resource_info(dev, &hw_res_parsed);
|
---|
1969 | hw_res_list_parsed_clean(&hw_res_parsed);
|
---|
1970 |
|
---|
1971 | return rc;
|
---|
1972 | }
|
---|
1973 |
|
---|
1974 | /** Initialize the E1000 device structure
|
---|
1975 | *
|
---|
1976 | * @param dev Device information
|
---|
1977 | *
|
---|
1978 | * @return EOK if succeed
|
---|
1979 | * @return Negative error code otherwise
|
---|
1980 | *
|
---|
1981 | */
|
---|
1982 | static int e1000_device_initialize(ddf_dev_t *dev)
|
---|
1983 | {
|
---|
1984 | /* Allocate driver data for the device. */
|
---|
1985 | e1000_t *e1000 = e1000_create_dev_data(dev);
|
---|
1986 | if (e1000 == NULL) {
|
---|
1987 | ddf_msg(LVL_ERROR, "Unable to allocate device softstate");
|
---|
1988 | return ENOMEM;
|
---|
1989 | }
|
---|
1990 |
|
---|
1991 | /* Obtain and fill hardware resources info */
|
---|
1992 | int rc = e1000_get_resource_info(dev);
|
---|
1993 | if (rc != EOK) {
|
---|
1994 | ddf_msg(LVL_ERROR, "Cannot obtain hardware resources");
|
---|
1995 | e1000_dev_cleanup(dev);
|
---|
1996 | return rc;
|
---|
1997 | }
|
---|
1998 |
|
---|
1999 | uint16_t device_id;
|
---|
2000 | rc = pci_config_space_read_16(ddf_dev_parent_sess_get(dev), PCI_DEVICE_ID,
|
---|
2001 | &device_id);
|
---|
2002 | if (rc != EOK) {
|
---|
2003 | ddf_msg(LVL_ERROR, "Cannot access PCI configuration space");
|
---|
2004 | e1000_dev_cleanup(dev);
|
---|
2005 | return rc;
|
---|
2006 | }
|
---|
2007 |
|
---|
2008 | e1000_board_t board;
|
---|
2009 | switch (device_id) {
|
---|
2010 | case 0x100e:
|
---|
2011 | case 0x1015:
|
---|
2012 | case 0x1016:
|
---|
2013 | case 0x1017:
|
---|
2014 | board = E1000_82540;
|
---|
2015 | break;
|
---|
2016 | case 0x1013:
|
---|
2017 | case 0x1018:
|
---|
2018 | case 0x1078:
|
---|
2019 | board = E1000_82541;
|
---|
2020 | break;
|
---|
2021 | case 0x1076:
|
---|
2022 | case 0x1077:
|
---|
2023 | case 0x107c:
|
---|
2024 | board = E1000_82541REV2;
|
---|
2025 | break;
|
---|
2026 | case 0x100f:
|
---|
2027 | case 0x1011:
|
---|
2028 | case 0x1026:
|
---|
2029 | case 0x1027:
|
---|
2030 | case 0x1028:
|
---|
2031 | board = E1000_82545;
|
---|
2032 | break;
|
---|
2033 | case 0x1010:
|
---|
2034 | case 0x1012:
|
---|
2035 | case 0x101d:
|
---|
2036 | case 0x1079:
|
---|
2037 | case 0x107a:
|
---|
2038 | case 0x107b:
|
---|
2039 | board = E1000_82546;
|
---|
2040 | break;
|
---|
2041 | case 0x1019:
|
---|
2042 | case 0x101a:
|
---|
2043 | board = E1000_82547;
|
---|
2044 | break;
|
---|
2045 | case 0x10b9:
|
---|
2046 | board = E1000_82572;
|
---|
2047 | break;
|
---|
2048 | case 0x1096:
|
---|
2049 | board = E1000_80003ES2;
|
---|
2050 | break;
|
---|
2051 | default:
|
---|
2052 | ddf_msg(LVL_ERROR, "Device not supported (%#" PRIx16 ")",
|
---|
2053 | device_id);
|
---|
2054 | e1000_dev_cleanup(dev);
|
---|
2055 | return ENOTSUP;
|
---|
2056 | }
|
---|
2057 |
|
---|
2058 | switch (board) {
|
---|
2059 | case E1000_82540:
|
---|
2060 | case E1000_82541:
|
---|
2061 | case E1000_82541REV2:
|
---|
2062 | case E1000_82545:
|
---|
2063 | case E1000_82546:
|
---|
2064 | e1000->info.eerd_start = 0x01;
|
---|
2065 | e1000->info.eerd_done = 0x10;
|
---|
2066 | e1000->info.eerd_address_offset = 8;
|
---|
2067 | e1000->info.eerd_data_offset = 16;
|
---|
2068 | break;
|
---|
2069 | case E1000_82547:
|
---|
2070 | case E1000_82572:
|
---|
2071 | case E1000_80003ES2:
|
---|
2072 | e1000->info.eerd_start = 0x01;
|
---|
2073 | e1000->info.eerd_done = 0x02;
|
---|
2074 | e1000->info.eerd_address_offset = 2;
|
---|
2075 | e1000->info.eerd_data_offset = 16;
|
---|
2076 | break;
|
---|
2077 | }
|
---|
2078 |
|
---|
2079 | return EOK;
|
---|
2080 | }
|
---|
2081 |
|
---|
2082 | /** Enable the I/O ports of the device.
|
---|
2083 | *
|
---|
2084 | * @param dev E1000 device.
|
---|
2085 | *
|
---|
2086 | * @return EOK if successed
|
---|
2087 | * @return Negative error code otherwise
|
---|
2088 | *
|
---|
2089 | */
|
---|
2090 | static int e1000_pio_enable(ddf_dev_t *dev)
|
---|
2091 | {
|
---|
2092 | e1000_t *e1000 = DRIVER_DATA_DEV(dev);
|
---|
2093 |
|
---|
2094 | int rc = pio_enable(e1000->reg_base_phys, 8 * PAGE_SIZE,
|
---|
2095 | &e1000->reg_base_virt);
|
---|
2096 | if (rc != EOK)
|
---|
2097 | return EADDRNOTAVAIL;
|
---|
2098 |
|
---|
2099 | return EOK;
|
---|
2100 | }
|
---|
2101 |
|
---|
2102 | /** Probe and initialize the newly added device.
|
---|
2103 | *
|
---|
2104 | * @param dev E1000 device.
|
---|
2105 | *
|
---|
2106 | */
|
---|
2107 | int e1000_dev_add(ddf_dev_t *dev)
|
---|
2108 | {
|
---|
2109 | ddf_fun_t *fun;
|
---|
2110 | assert(dev);
|
---|
2111 |
|
---|
2112 | /* Initialize device structure for E1000 */
|
---|
2113 | int rc = e1000_device_initialize(dev);
|
---|
2114 | if (rc != EOK)
|
---|
2115 | return rc;
|
---|
2116 |
|
---|
2117 | /* Device initialization */
|
---|
2118 | nic_t *nic = ddf_dev_data_get(dev);
|
---|
2119 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
2120 |
|
---|
2121 | /* Map registers */
|
---|
2122 | rc = e1000_pio_enable(dev);
|
---|
2123 | if (rc != EOK)
|
---|
2124 | goto err_destroy;
|
---|
2125 |
|
---|
2126 | e1000_initialize_registers(e1000);
|
---|
2127 | rc = e1000_initialize_tx_structure(e1000);
|
---|
2128 | if (rc != EOK)
|
---|
2129 | goto err_pio;
|
---|
2130 |
|
---|
2131 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
2132 |
|
---|
2133 | e1000_fill_mac_from_eeprom(e1000);
|
---|
2134 | e1000_initialize_filters(e1000);
|
---|
2135 |
|
---|
2136 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
2137 |
|
---|
2138 | e1000_initialize_vlan(e1000);
|
---|
2139 |
|
---|
2140 | fun = ddf_fun_create(nic_get_ddf_dev(nic), fun_exposed, "port0");
|
---|
2141 | if (fun == NULL)
|
---|
2142 | goto err_tx_structure;
|
---|
2143 | nic_set_ddf_fun(nic, fun);
|
---|
2144 | ddf_fun_set_ops(fun, &e1000_dev_ops);
|
---|
2145 | ddf_fun_data_implant(fun, nic);
|
---|
2146 |
|
---|
2147 | rc = e1000_register_int_handler(nic);
|
---|
2148 | if (rc != EOK)
|
---|
2149 | goto err_fun_create;
|
---|
2150 |
|
---|
2151 | rc = nic_connect_to_services(nic);
|
---|
2152 | if (rc != EOK)
|
---|
2153 | goto err_irq;
|
---|
2154 |
|
---|
2155 | rc = e1000_initialize_rx_structure(nic);
|
---|
2156 | if (rc != EOK)
|
---|
2157 | goto err_irq;
|
---|
2158 |
|
---|
2159 | nic_address_t e1000_address;
|
---|
2160 | e1000_get_address(e1000, &e1000_address);
|
---|
2161 | rc = nic_report_address(nic, &e1000_address);
|
---|
2162 | if (rc != EOK)
|
---|
2163 | goto err_rx_structure;
|
---|
2164 |
|
---|
2165 | struct timeval period;
|
---|
2166 | period.tv_sec = 0;
|
---|
2167 | period.tv_usec = E1000_DEFAULT_INTERRUPT_INTERVAL_USEC;
|
---|
2168 | rc = nic_report_poll_mode(nic, NIC_POLL_PERIODIC, &period);
|
---|
2169 | if (rc != EOK)
|
---|
2170 | goto err_rx_structure;
|
---|
2171 |
|
---|
2172 | rc = ddf_fun_bind(fun);
|
---|
2173 | if (rc != EOK)
|
---|
2174 | goto err_fun_bind;
|
---|
2175 |
|
---|
2176 | rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
|
---|
2177 | if (rc != EOK)
|
---|
2178 | goto err_add_to_cat;
|
---|
2179 |
|
---|
2180 | return EOK;
|
---|
2181 |
|
---|
2182 | err_add_to_cat:
|
---|
2183 | ddf_fun_unbind(fun);
|
---|
2184 | err_fun_bind:
|
---|
2185 | err_rx_structure:
|
---|
2186 | e1000_uninitialize_rx_structure(nic);
|
---|
2187 | err_irq:
|
---|
2188 | unregister_interrupt_handler(dev, DRIVER_DATA_DEV(dev)->irq);
|
---|
2189 | err_fun_create:
|
---|
2190 | ddf_fun_destroy(fun);
|
---|
2191 | nic_set_ddf_fun(nic, NULL);
|
---|
2192 | err_tx_structure:
|
---|
2193 | e1000_uninitialize_tx_structure(e1000);
|
---|
2194 | err_pio:
|
---|
2195 | // TODO: e1000_pio_disable(dev);
|
---|
2196 | err_destroy:
|
---|
2197 | e1000_dev_cleanup(dev);
|
---|
2198 | return rc;
|
---|
2199 | }
|
---|
2200 |
|
---|
2201 | /** Read 16-bit value from EEPROM of E1000 adapter
|
---|
2202 | *
|
---|
2203 | * Read using the EERD register.
|
---|
2204 | *
|
---|
2205 | * @param device E1000 device
|
---|
2206 | * @param eeprom_address 8-bit EEPROM address
|
---|
2207 | *
|
---|
2208 | * @return 16-bit value from EEPROM
|
---|
2209 | *
|
---|
2210 | */
|
---|
2211 | static uint16_t e1000_eeprom_read(e1000_t *e1000, uint8_t eeprom_address)
|
---|
2212 | {
|
---|
2213 | fibril_mutex_lock(&e1000->eeprom_lock);
|
---|
2214 |
|
---|
2215 | /* Write address and START bit to EERD register */
|
---|
2216 | uint32_t write_data = e1000->info.eerd_start |
|
---|
2217 | (((uint32_t) eeprom_address) <<
|
---|
2218 | e1000->info.eerd_address_offset);
|
---|
2219 | E1000_REG_WRITE(e1000, E1000_EERD, write_data);
|
---|
2220 |
|
---|
2221 | uint32_t eerd = E1000_REG_READ(e1000, E1000_EERD);
|
---|
2222 | while ((eerd & e1000->info.eerd_done) == 0) {
|
---|
2223 | usleep(1);
|
---|
2224 | eerd = E1000_REG_READ(e1000, E1000_EERD);
|
---|
2225 | }
|
---|
2226 |
|
---|
2227 | fibril_mutex_unlock(&e1000->eeprom_lock);
|
---|
2228 |
|
---|
2229 | return (uint16_t) (eerd >> e1000->info.eerd_data_offset);
|
---|
2230 | }
|
---|
2231 |
|
---|
2232 | /** Get MAC address of the E1000 adapter
|
---|
2233 | *
|
---|
2234 | * @param device E1000 device
|
---|
2235 | * @param address Place to store the address
|
---|
2236 | * @param max_len Maximal addresss length to store
|
---|
2237 | *
|
---|
2238 | * @return EOK if succeed
|
---|
2239 | * @return Negative error code otherwise
|
---|
2240 | *
|
---|
2241 | */
|
---|
2242 | static int e1000_get_address(e1000_t *e1000, nic_address_t *address)
|
---|
2243 | {
|
---|
2244 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
2245 |
|
---|
2246 | uint8_t *mac0_dest = (uint8_t *) address->address;
|
---|
2247 | uint8_t *mac1_dest = (uint8_t *) address->address + 1;
|
---|
2248 | uint8_t *mac2_dest = (uint8_t *) address->address + 2;
|
---|
2249 | uint8_t *mac3_dest = (uint8_t *) address->address + 3;
|
---|
2250 | uint8_t *mac4_dest = (uint8_t *) address->address + 4;
|
---|
2251 | uint8_t *mac5_dest = (uint8_t *) address->address + 5;
|
---|
2252 |
|
---|
2253 | uint32_t rah = E1000_REG_READ(e1000, E1000_RAH_ARRAY(0));
|
---|
2254 | uint32_t ral = E1000_REG_READ(e1000, E1000_RAL_ARRAY(0));
|
---|
2255 |
|
---|
2256 | *mac0_dest = (uint8_t) ral;
|
---|
2257 | *mac1_dest = (uint8_t) (ral >> 8);
|
---|
2258 | *mac2_dest = (uint8_t) (ral >> 16);
|
---|
2259 | *mac3_dest = (uint8_t) (ral >> 24);
|
---|
2260 | *mac4_dest = (uint8_t) rah;
|
---|
2261 | *mac5_dest = (uint8_t) (rah >> 8);
|
---|
2262 |
|
---|
2263 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
2264 | return EOK;
|
---|
2265 | };
|
---|
2266 |
|
---|
2267 | /** Set card MAC address
|
---|
2268 | *
|
---|
2269 | * @param device E1000 device
|
---|
2270 | * @param address Address
|
---|
2271 | *
|
---|
2272 | * @return EOK if succeed
|
---|
2273 | * @return Negative error code otherwise
|
---|
2274 | */
|
---|
2275 | static int e1000_set_addr(ddf_fun_t *fun, const nic_address_t *addr)
|
---|
2276 | {
|
---|
2277 | nic_t *nic = NIC_DATA_FUN(fun);
|
---|
2278 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
2279 |
|
---|
2280 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
2281 | fibril_mutex_lock(&e1000->tx_lock);
|
---|
2282 |
|
---|
2283 | int rc = nic_report_address(nic, addr);
|
---|
2284 | if (rc == EOK)
|
---|
2285 | e1000_write_receive_address(e1000, 0, addr, false);
|
---|
2286 |
|
---|
2287 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
2288 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
2289 |
|
---|
2290 | return rc;
|
---|
2291 | }
|
---|
2292 |
|
---|
2293 | static void e1000_eeprom_get_address(e1000_t *e1000,
|
---|
2294 | nic_address_t *address)
|
---|
2295 | {
|
---|
2296 | uint16_t *mac0_dest = (uint16_t *) address->address;
|
---|
2297 | uint16_t *mac2_dest = (uint16_t *) (address->address + 2);
|
---|
2298 | uint16_t *mac4_dest = (uint16_t *) (address->address + 4);
|
---|
2299 |
|
---|
2300 | *mac0_dest = e1000_eeprom_read(e1000, 0);
|
---|
2301 | *mac2_dest = e1000_eeprom_read(e1000, 1);
|
---|
2302 | *mac4_dest = e1000_eeprom_read(e1000, 2);
|
---|
2303 | }
|
---|
2304 |
|
---|
2305 | /** Send frame
|
---|
2306 | *
|
---|
2307 | * @param nic NIC driver data structure
|
---|
2308 | * @param data Frame data
|
---|
2309 | * @param size Frame size in bytes
|
---|
2310 | *
|
---|
2311 | * @return EOK if succeed
|
---|
2312 | * @return Error code in the case of error
|
---|
2313 | *
|
---|
2314 | */
|
---|
2315 | static void e1000_send_frame(nic_t *nic, void *data, size_t size)
|
---|
2316 | {
|
---|
2317 | assert(nic);
|
---|
2318 |
|
---|
2319 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
2320 | fibril_mutex_lock(&e1000->tx_lock);
|
---|
2321 |
|
---|
2322 | uint32_t tdt = E1000_REG_READ(e1000, E1000_TDT);
|
---|
2323 | e1000_tx_descriptor_t *tx_descriptor_addr = (e1000_tx_descriptor_t *)
|
---|
2324 | (e1000->tx_ring_virt + tdt * sizeof(e1000_tx_descriptor_t));
|
---|
2325 |
|
---|
2326 | bool descriptor_available = false;
|
---|
2327 |
|
---|
2328 | /* Descriptor never used */
|
---|
2329 | if (tx_descriptor_addr->length == 0)
|
---|
2330 | descriptor_available = true;
|
---|
2331 |
|
---|
2332 | /* Descriptor done */
|
---|
2333 | if (tx_descriptor_addr->status & TXDESCRIPTOR_STATUS_DD)
|
---|
2334 | descriptor_available = true;
|
---|
2335 |
|
---|
2336 | if (!descriptor_available) {
|
---|
2337 | /* Frame lost */
|
---|
2338 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
2339 | return;
|
---|
2340 | }
|
---|
2341 |
|
---|
2342 | memcpy(e1000->tx_frame_virt[tdt], data, size);
|
---|
2343 |
|
---|
2344 | tx_descriptor_addr->phys_addr = PTR_TO_U64(e1000->tx_frame_phys[tdt]);
|
---|
2345 | tx_descriptor_addr->length = size;
|
---|
2346 |
|
---|
2347 | /*
|
---|
2348 | * Report status to STATUS.DD (descriptor done),
|
---|
2349 | * add ethernet CRC, end of packet.
|
---|
2350 | */
|
---|
2351 | tx_descriptor_addr->command = TXDESCRIPTOR_COMMAND_RS |
|
---|
2352 | TXDESCRIPTOR_COMMAND_IFCS |
|
---|
2353 | TXDESCRIPTOR_COMMAND_EOP;
|
---|
2354 |
|
---|
2355 | tx_descriptor_addr->checksum_offset = 0;
|
---|
2356 | tx_descriptor_addr->status = 0;
|
---|
2357 | if (e1000->vlan_tag_add) {
|
---|
2358 | tx_descriptor_addr->special = e1000->vlan_tag;
|
---|
2359 | tx_descriptor_addr->command |= TXDESCRIPTOR_COMMAND_VLE;
|
---|
2360 | } else
|
---|
2361 | tx_descriptor_addr->special = 0;
|
---|
2362 |
|
---|
2363 | tx_descriptor_addr->checksum_start_field = 0;
|
---|
2364 |
|
---|
2365 | tdt++;
|
---|
2366 | if (tdt == E1000_TX_FRAME_COUNT)
|
---|
2367 | tdt = 0;
|
---|
2368 |
|
---|
2369 | E1000_REG_WRITE(e1000, E1000_TDT, tdt);
|
---|
2370 |
|
---|
2371 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
2372 | }
|
---|
2373 |
|
---|
2374 | int main(void)
|
---|
2375 | {
|
---|
2376 | int rc = nic_driver_init(NAME);
|
---|
2377 | if (rc != EOK)
|
---|
2378 | return rc;
|
---|
2379 |
|
---|
2380 | nic_driver_implement(&e1000_driver_ops, &e1000_dev_ops,
|
---|
2381 | &e1000_nic_iface);
|
---|
2382 |
|
---|
2383 | ddf_log_init(NAME);
|
---|
2384 | ddf_msg(LVL_NOTE, "HelenOS E1000 driver started");
|
---|
2385 | return ddf_driver_main(&e1000_driver);
|
---|
2386 | }
|
---|