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