[bf84871] | 1 | /*
|
---|
[8300c72] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[bf84871] | 3 | * Copyright (c) 2011 Zdenek Bouska
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[1df224c] | 30 | /** @file e1k.c
|
---|
| 31 | *
|
---|
| 32 | * Driver for Intel Pro/1000 8254x Family of Gigabit Ethernet Controllers
|
---|
[bf84871] | 33 | *
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[f300523] | 36 | #include <async.h>
|
---|
[bf84871] | 37 | #include <assert.h>
|
---|
| 38 | #include <stdio.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <adt/list.h>
|
---|
| 41 | #include <align.h>
|
---|
| 42 | #include <byteorder.h>
|
---|
| 43 | #include <as.h>
|
---|
[869d936] | 44 | #include <ddi.h>
|
---|
[77c2b02] | 45 | #include <ddf/log.h>
|
---|
[bf84871] | 46 | #include <ddf/interrupt.h>
|
---|
[d51838f] | 47 | #include <device/hw_res.h>
|
---|
[bf84871] | 48 | #include <device/hw_res_parsed.h>
|
---|
[99e8fb7b] | 49 | #include <pci_dev_iface.h>
|
---|
[bf84871] | 50 | #include <nic.h>
|
---|
| 51 | #include <ops/nic.h>
|
---|
[1df224c] | 52 | #include "e1k.h"
|
---|
[bf84871] | 53 |
|
---|
[1df224c] | 54 | #define NAME "e1k"
|
---|
[bf84871] | 55 |
|
---|
[9916841] | 56 | #define E1000_DEFAULT_INTERRUPT_INTERVAL_USEC 250
|
---|
[bf84871] | 57 |
|
---|
[ee7f78a] | 58 | /* Must be power of 8 */
|
---|
| 59 | #define E1000_RX_FRAME_COUNT 128
|
---|
| 60 | #define E1000_TX_FRAME_COUNT 128
|
---|
[bf84871] | 61 |
|
---|
[1df224c] | 62 | #define E1000_RECEIVE_ADDRESS 16
|
---|
[bf84871] | 63 |
|
---|
[1bc35b5] | 64 | /** Maximum sending frame size */
|
---|
[6d8455d] | 65 | #define E1000_MAX_SEND_FRAME_SIZE 2048
|
---|
[321052f7] | 66 | /** Maximum receiving frame size */
|
---|
[1bc35b5] | 67 | #define E1000_MAX_RECEIVE_FRAME_SIZE 2048
|
---|
[bf84871] | 68 |
|
---|
| 69 | /** nic_driver_data_t* -> e1000_t* cast */
|
---|
[c4be33a] | 70 | #define DRIVER_DATA_NIC(nic) \
|
---|
| 71 | ((e1000_t *) nic_get_specific(nic))
|
---|
[1df224c] | 72 |
|
---|
[56fd7cf] | 73 | /** ddf_fun_t * -> nic_driver_data_t* cast */
|
---|
| 74 | #define NIC_DATA_FUN(fun) \
|
---|
[7eb6c96] | 75 | ((nic_t *) ddf_dev_data_get(ddf_fun_get_dev(fun)))
|
---|
[56fd7cf] | 76 |
|
---|
| 77 | /** ddf_dev_t * -> nic_driver_data_t* cast */
|
---|
[1df224c] | 78 | #define NIC_DATA_DEV(dev) \
|
---|
[56fd7cf] | 79 | ((nic_t *) ddf_dev_data_get(dev))
|
---|
[1df224c] | 80 |
|
---|
[56fd7cf] | 81 | /** ddf_dev_t * -> e1000_t* cast */
|
---|
[1df224c] | 82 | #define DRIVER_DATA_DEV(dev) \
|
---|
| 83 | (DRIVER_DATA_NIC(NIC_DATA_DEV(dev)))
|
---|
[bf84871] | 84 |
|
---|
[56fd7cf] | 85 | /** ddf_fun_t * -> e1000_t* cast */
|
---|
| 86 | #define DRIVER_DATA_FUN(fun) \
|
---|
| 87 | (DRIVER_DATA_NIC(NIC_DATA_FUN(fun)))
|
---|
| 88 |
|
---|
[1df224c] | 89 | /** Cast pointer to uint64_t
|
---|
| 90 | *
|
---|
| 91 | * @param ptr Pointer to cast
|
---|
| 92 | *
|
---|
| 93 | * @return The uint64_t pointer representation.
|
---|
[bf84871] | 94 | *
|
---|
| 95 | */
|
---|
[1df224c] | 96 | #define PTR_TO_U64(ptr) ((uint64_t) ((uintptr_t) (ptr)))
|
---|
[bf84871] | 97 |
|
---|
| 98 | /** Cast the memaddr part to the void*
|
---|
| 99 | *
|
---|
[1df224c] | 100 | * @param memaddr The memaddr value
|
---|
| 101 | *
|
---|
[bf84871] | 102 | */
|
---|
[1df224c] | 103 | #define MEMADDR_TO_PTR(memaddr) ((void *) ((size_t) (memaddr)))
|
---|
| 104 |
|
---|
[c4be33a] | 105 | #define E1000_REG_BASE(e1000) \
|
---|
| 106 | ((e1000)->reg_base_virt)
|
---|
[bf84871] | 107 |
|
---|
[c4be33a] | 108 | #define E1000_REG_ADDR(e1000, reg) \
|
---|
| 109 | ((uint32_t *) (E1000_REG_BASE(e1000) + reg))
|
---|
[bf84871] | 110 |
|
---|
[c4be33a] | 111 | #define E1000_REG_READ(e1000, reg) \
|
---|
| 112 | (pio_read_32(E1000_REG_ADDR(e1000, reg)))
|
---|
[1df224c] | 113 |
|
---|
[c4be33a] | 114 | #define E1000_REG_WRITE(e1000, reg, value) \
|
---|
| 115 | (pio_write_32(E1000_REG_ADDR(e1000, reg), value))
|
---|
[bf84871] | 116 |
|
---|
| 117 | /** E1000 device data */
|
---|
[c4be33a] | 118 | typedef struct {
|
---|
[d51838f] | 119 | /** DDF device */
|
---|
| 120 | ddf_dev_t *dev;
|
---|
| 121 | /** Parent session */
|
---|
| 122 | async_sess_t *parent_sess;
|
---|
[77c2b02] | 123 | /** Device configuration */
|
---|
| 124 | e1000_info_t info;
|
---|
[a35b458] | 125 |
|
---|
[bf84871] | 126 | /** Physical registers base address */
|
---|
[c4be33a] | 127 | void *reg_base_phys;
|
---|
[bf84871] | 128 | /** Virtual registers base address */
|
---|
[c4be33a] | 129 | void *reg_base_virt;
|
---|
[a35b458] | 130 |
|
---|
[c4be33a] | 131 | /** Physical tx ring address */
|
---|
[8442d10] | 132 | uintptr_t tx_ring_phys;
|
---|
[c4be33a] | 133 | /** Virtual tx ring address */
|
---|
| 134 | void *tx_ring_virt;
|
---|
[a35b458] | 135 |
|
---|
[6d8455d] | 136 | /** Ring of TX frames, physical address */
|
---|
[8442d10] | 137 | uintptr_t *tx_frame_phys;
|
---|
[6d8455d] | 138 | /** Ring of TX frames, virtual address */
|
---|
| 139 | void **tx_frame_virt;
|
---|
[a35b458] | 140 |
|
---|
[c4be33a] | 141 | /** Physical rx ring address */
|
---|
[8442d10] | 142 | uintptr_t rx_ring_phys;
|
---|
[c4be33a] | 143 | /** Virtual rx ring address */
|
---|
| 144 | void *rx_ring_virt;
|
---|
[a35b458] | 145 |
|
---|
[1bc35b5] | 146 | /** Ring of RX frames, physical address */
|
---|
[8442d10] | 147 | uintptr_t *rx_frame_phys;
|
---|
[1bc35b5] | 148 | /** Ring of RX frames, virtual address */
|
---|
| 149 | void **rx_frame_virt;
|
---|
[a35b458] | 150 |
|
---|
[bf84871] | 151 | /** VLAN tag */
|
---|
| 152 | uint16_t vlan_tag;
|
---|
[a35b458] | 153 |
|
---|
[1bc35b5] | 154 | /** Add VLAN tag to frame */
|
---|
[1df224c] | 155 | bool vlan_tag_add;
|
---|
[a35b458] | 156 |
|
---|
[bf84871] | 157 | /** Used unicast Receive Address count */
|
---|
| 158 | unsigned int unicast_ra_count;
|
---|
[a35b458] | 159 |
|
---|
[1df224c] | 160 | /** Used milticast Receive addrress count */
|
---|
[bf84871] | 161 | unsigned int multicast_ra_count;
|
---|
[a35b458] | 162 |
|
---|
[bf84871] | 163 | /** The irq assigned */
|
---|
| 164 | int irq;
|
---|
[a35b458] | 165 |
|
---|
[bf84871] | 166 | /** Lock for CTRL register */
|
---|
| 167 | fibril_mutex_t ctrl_lock;
|
---|
[a35b458] | 168 |
|
---|
[bf84871] | 169 | /** Lock for receiver */
|
---|
| 170 | fibril_mutex_t rx_lock;
|
---|
[a35b458] | 171 |
|
---|
[bf84871] | 172 | /** Lock for transmitter */
|
---|
| 173 | fibril_mutex_t tx_lock;
|
---|
[a35b458] | 174 |
|
---|
[bf84871] | 175 | /** Lock for EEPROM access */
|
---|
| 176 | fibril_mutex_t eeprom_lock;
|
---|
| 177 | } e1000_t;
|
---|
| 178 |
|
---|
| 179 | /** Global mutex for work with shared irq structure */
|
---|
| 180 | FIBRIL_MUTEX_INITIALIZE(irq_reg_mutex);
|
---|
| 181 |
|
---|
[b7fd2a0] | 182 | static errno_t e1000_get_address(e1000_t *, nic_address_t *);
|
---|
[1df224c] | 183 | static void e1000_eeprom_get_address(e1000_t *, nic_address_t *);
|
---|
[b7fd2a0] | 184 | static errno_t e1000_set_addr(ddf_fun_t *, const nic_address_t *);
|
---|
[bf84871] | 185 |
|
---|
[b7fd2a0] | 186 | static errno_t e1000_defective_get_mode(ddf_fun_t *, uint32_t *);
|
---|
| 187 | static errno_t e1000_defective_set_mode(ddf_fun_t *, uint32_t);
|
---|
[bf84871] | 188 |
|
---|
[b7fd2a0] | 189 | static errno_t e1000_get_cable_state(ddf_fun_t *, nic_cable_state_t *);
|
---|
| 190 | static errno_t e1000_get_device_info(ddf_fun_t *, nic_device_info_t *);
|
---|
| 191 | static errno_t e1000_get_operation_mode(ddf_fun_t *, int *,
|
---|
[1df224c] | 192 | nic_channel_mode_t *, nic_role_t *);
|
---|
[b7fd2a0] | 193 | static errno_t e1000_set_operation_mode(ddf_fun_t *, int,
|
---|
[1df224c] | 194 | nic_channel_mode_t, nic_role_t);
|
---|
[b7fd2a0] | 195 | static errno_t e1000_autoneg_enable(ddf_fun_t *, uint32_t);
|
---|
| 196 | static errno_t e1000_autoneg_disable(ddf_fun_t *);
|
---|
| 197 | static errno_t e1000_autoneg_restart(ddf_fun_t *);
|
---|
[bf84871] | 198 |
|
---|
[b7fd2a0] | 199 | static errno_t e1000_vlan_set_tag(ddf_fun_t *, uint16_t, bool, bool);
|
---|
[bf84871] | 200 |
|
---|
| 201 | /** Network interface options for E1000 card driver */
|
---|
| 202 | static nic_iface_t e1000_nic_iface;
|
---|
| 203 |
|
---|
| 204 | /** Network interface options for E1000 card driver */
|
---|
| 205 | static nic_iface_t e1000_nic_iface = {
|
---|
| 206 | .set_address = &e1000_set_addr,
|
---|
| 207 | .get_device_info = &e1000_get_device_info,
|
---|
| 208 | .get_cable_state = &e1000_get_cable_state,
|
---|
| 209 | .get_operation_mode = &e1000_get_operation_mode,
|
---|
| 210 | .set_operation_mode = &e1000_set_operation_mode,
|
---|
| 211 | .autoneg_enable = &e1000_autoneg_enable,
|
---|
| 212 | .autoneg_disable = &e1000_autoneg_disable,
|
---|
| 213 | .autoneg_restart = &e1000_autoneg_restart,
|
---|
| 214 | .vlan_set_tag = &e1000_vlan_set_tag,
|
---|
| 215 | .defective_get_mode = &e1000_defective_get_mode,
|
---|
| 216 | .defective_set_mode = &e1000_defective_set_mode,
|
---|
| 217 | };
|
---|
| 218 |
|
---|
| 219 | /** Basic device operations for E1000 driver */
|
---|
| 220 | static ddf_dev_ops_t e1000_dev_ops;
|
---|
| 221 |
|
---|
[b7fd2a0] | 222 | static errno_t e1000_dev_add(ddf_dev_t *);
|
---|
[8300c72] | 223 | static errno_t e1000_dev_quiesce(ddf_dev_t *);
|
---|
[bf84871] | 224 |
|
---|
| 225 | /** Basic driver operations for E1000 driver */
|
---|
| 226 | static driver_ops_t e1000_driver_ops = {
|
---|
[8300c72] | 227 | .dev_add = e1000_dev_add,
|
---|
| 228 | .dev_quiesce = e1000_dev_quiesce
|
---|
[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);
|
---|
| 1194 | nic_received_frame(nic, frame);
|
---|
| 1195 | } else {
|
---|
| 1196 | ddf_msg(LVL_ERROR, "Memory allocation failed. Frame dropped.");
|
---|
| 1197 | }
|
---|
[a35b458] | 1198 |
|
---|
[c4be33a] | 1199 | e1000_fill_new_rx_descriptor(nic, next_tail);
|
---|
[a35b458] | 1200 |
|
---|
[1bc35b5] | 1201 | *tail_addr = e1000_inc_tail(*tail_addr, E1000_RX_FRAME_COUNT);
|
---|
| 1202 | next_tail = e1000_inc_tail(*tail_addr, E1000_RX_FRAME_COUNT);
|
---|
[a35b458] | 1203 |
|
---|
[1df224c] | 1204 | rx_descriptor = (e1000_rx_descriptor_t *)
|
---|
[c4be33a] | 1205 | (e1000->rx_ring_virt + next_tail * sizeof(e1000_rx_descriptor_t));
|
---|
[bf84871] | 1206 | }
|
---|
[a35b458] | 1207 |
|
---|
[c4be33a] | 1208 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
[bf84871] | 1209 | }
|
---|
| 1210 |
|
---|
[1df224c] | 1211 | /** Enable E1000 interupts
|
---|
| 1212 | *
|
---|
[c4be33a] | 1213 | * @param e1000 E1000 data structure
|
---|
[bf84871] | 1214 | *
|
---|
| 1215 | */
|
---|
[c4be33a] | 1216 | static void e1000_enable_interrupts(e1000_t *e1000)
|
---|
[bf84871] | 1217 | {
|
---|
[c4be33a] | 1218 | E1000_REG_WRITE(e1000, E1000_IMS, ICR_RXT0);
|
---|
[bf84871] | 1219 | }
|
---|
| 1220 |
|
---|
[1df224c] | 1221 | /** Disable E1000 interupts
|
---|
| 1222 | *
|
---|
[c4be33a] | 1223 | * @param e1000 E1000 data structure
|
---|
[bf84871] | 1224 | *
|
---|
| 1225 | */
|
---|
[c4be33a] | 1226 | static void e1000_disable_interrupts(e1000_t *e1000)
|
---|
[bf84871] | 1227 | {
|
---|
[c4be33a] | 1228 | E1000_REG_WRITE(e1000, E1000_IMS, 0);
|
---|
[bf84871] | 1229 | }
|
---|
| 1230 |
|
---|
| 1231 | /** Interrupt handler implementation
|
---|
| 1232 | *
|
---|
[1df224c] | 1233 | * This function is called from e1000_interrupt_handler()
|
---|
| 1234 | * and e1000_poll()
|
---|
| 1235 | *
|
---|
[c4be33a] | 1236 | * @param nic NIC data
|
---|
| 1237 | * @param icr ICR register value
|
---|
[1df224c] | 1238 | *
|
---|
[bf84871] | 1239 | */
|
---|
[c4be33a] | 1240 | static void e1000_interrupt_handler_impl(nic_t *nic, uint32_t icr)
|
---|
[1df224c] | 1241 | {
|
---|
| 1242 | if (icr & ICR_RXT0)
|
---|
[1bc35b5] | 1243 | e1000_receive_frames(nic);
|
---|
[bf84871] | 1244 | }
|
---|
| 1245 |
|
---|
| 1246 | /** Handle device interrupt
|
---|
| 1247 | *
|
---|
[1df224c] | 1248 | * @param icall IPC call structure
|
---|
[60744cb] | 1249 | * @param arg Argument (nic_t *)
|
---|
[1df224c] | 1250 | *
|
---|
[bf84871] | 1251 | */
|
---|
[60744cb] | 1252 | static void e1000_interrupt_handler(ipc_call_t *icall, void *arg)
|
---|
[bf84871] | 1253 | {
|
---|
[fafb8e5] | 1254 | uint32_t icr = (uint32_t) ipc_get_arg2(icall);
|
---|
[60744cb] | 1255 | nic_t *nic = (nic_t *)arg;
|
---|
[c4be33a] | 1256 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
[a35b458] | 1257 |
|
---|
[c4be33a] | 1258 | e1000_interrupt_handler_impl(nic, icr);
|
---|
| 1259 | e1000_enable_interrupts(e1000);
|
---|
[1df224c] | 1260 | }
|
---|
[bf84871] | 1261 |
|
---|
| 1262 | /** Register interrupt handler for the card in the system
|
---|
| 1263 | *
|
---|
[1df224c] | 1264 | * Note: The global irq_reg_mutex is locked because of work with global
|
---|
| 1265 | * structure.
|
---|
| 1266 | *
|
---|
[c4be33a] | 1267 | * @param nic Driver data
|
---|
[1df224c] | 1268 | *
|
---|
[071a1ddb] | 1269 | * @param[out] handle IRQ capability handle if the handler was registered
|
---|
| 1270 | *
|
---|
[cde999a] | 1271 | * @return An error code otherwise
|
---|
[bf84871] | 1272 | *
|
---|
| 1273 | */
|
---|
[870841cf] | 1274 | static errno_t e1000_register_int_handler(nic_t *nic,
|
---|
[eadaeae8] | 1275 | cap_irq_handle_t *handle)
|
---|
[bf84871] | 1276 | {
|
---|
[c4be33a] | 1277 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
[a35b458] | 1278 |
|
---|
[bf84871] | 1279 | /* Lock the mutex in whole driver while working with global structure */
|
---|
| 1280 | fibril_mutex_lock(&irq_reg_mutex);
|
---|
[a35b458] | 1281 |
|
---|
[ec52752] | 1282 | e1000_irq_code.ranges[0].base = (uintptr_t) e1000->reg_base_phys;
|
---|
| 1283 | e1000_irq_code.cmds[0].addr = e1000->reg_base_phys + E1000_ICR;
|
---|
[807be7e] | 1284 | e1000_irq_code.cmds[3].addr = e1000->reg_base_phys + E1000_IMC;
|
---|
[a35b458] | 1285 |
|
---|
[b7fd2a0] | 1286 | errno_t rc = register_interrupt_handler(nic_get_ddf_dev(nic), e1000->irq,
|
---|
[60744cb] | 1287 | e1000_interrupt_handler, (void *)nic, &e1000_irq_code, handle);
|
---|
[a35b458] | 1288 |
|
---|
[bf84871] | 1289 | fibril_mutex_unlock(&irq_reg_mutex);
|
---|
[071a1ddb] | 1290 | return rc;
|
---|
[bf84871] | 1291 | }
|
---|
| 1292 |
|
---|
[1bc35b5] | 1293 | /** Force receiving all frames in the receive buffer
|
---|
[bf84871] | 1294 | *
|
---|
[c4be33a] | 1295 | * @param nic NIC data
|
---|
[1df224c] | 1296 | *
|
---|
[bf84871] | 1297 | */
|
---|
[c4be33a] | 1298 | static void e1000_poll(nic_t *nic)
|
---|
[bf84871] | 1299 | {
|
---|
[c4be33a] | 1300 | assert(nic);
|
---|
[a35b458] | 1301 |
|
---|
[c4be33a] | 1302 | e1000_t *e1000 = nic_get_specific(nic);
|
---|
| 1303 | assert(e1000);
|
---|
[a35b458] | 1304 |
|
---|
[c4be33a] | 1305 | uint32_t icr = E1000_REG_READ(e1000, E1000_ICR);
|
---|
| 1306 | e1000_interrupt_handler_impl(nic, icr);
|
---|
[bf84871] | 1307 | }
|
---|
| 1308 |
|
---|
[bd41ac52] | 1309 | /** Calculates ITR register interrupt from timespec structure
|
---|
[bf84871] | 1310 | *
|
---|
[1df224c] | 1311 | * @param period Period
|
---|
| 1312 | *
|
---|
[bf84871] | 1313 | */
|
---|
[bd41ac52] | 1314 | static uint16_t e1000_calculate_itr_interval(const struct timespec *period)
|
---|
[1df224c] | 1315 | {
|
---|
| 1316 | // TODO: use also tv_sec
|
---|
[bd41ac52] | 1317 | return e1000_calculate_itr_interval_from_usecs(NSEC2USEC(period->tv_nsec));
|
---|
[bf84871] | 1318 | }
|
---|
| 1319 |
|
---|
| 1320 | /** Set polling mode
|
---|
| 1321 | *
|
---|
[1df224c] | 1322 | * @param device Device to set
|
---|
| 1323 | * @param mode Mode to set
|
---|
| 1324 | * @param period Period for NIC_POLL_PERIODIC
|
---|
| 1325 | *
|
---|
| 1326 | * @return EOK if succeed
|
---|
| 1327 | * @return ENOTSUP if the mode is not supported
|
---|
[bf84871] | 1328 | *
|
---|
| 1329 | */
|
---|
[b7fd2a0] | 1330 | static errno_t e1000_poll_mode_change(nic_t *nic, nic_poll_mode_t mode,
|
---|
[bd41ac52] | 1331 | const struct timespec *period)
|
---|
[bf84871] | 1332 | {
|
---|
[c4be33a] | 1333 | assert(nic);
|
---|
[a35b458] | 1334 |
|
---|
[c4be33a] | 1335 | e1000_t *e1000 = nic_get_specific(nic);
|
---|
| 1336 | assert(e1000);
|
---|
[a35b458] | 1337 |
|
---|
[1df224c] | 1338 | switch (mode) {
|
---|
[bf84871] | 1339 | case NIC_POLL_IMMEDIATE:
|
---|
[c4be33a] | 1340 | E1000_REG_WRITE(e1000, E1000_ITR, 0);
|
---|
| 1341 | e1000_enable_interrupts(e1000);
|
---|
[bf84871] | 1342 | break;
|
---|
| 1343 | case NIC_POLL_ON_DEMAND:
|
---|
[c4be33a] | 1344 | e1000_disable_interrupts(e1000);
|
---|
[bf84871] | 1345 | break;
|
---|
| 1346 | case NIC_POLL_PERIODIC:
|
---|
| 1347 | assert(period);
|
---|
| 1348 | uint16_t itr_interval = e1000_calculate_itr_interval(period);
|
---|
[c4be33a] | 1349 | E1000_REG_WRITE(e1000, E1000_ITR, (uint32_t) itr_interval);
|
---|
| 1350 | e1000_enable_interrupts(e1000);
|
---|
[bf84871] | 1351 | break;
|
---|
| 1352 | default:
|
---|
| 1353 | return ENOTSUP;
|
---|
| 1354 | }
|
---|
[a35b458] | 1355 |
|
---|
[bf84871] | 1356 | return EOK;
|
---|
| 1357 | }
|
---|
| 1358 |
|
---|
[1df224c] | 1359 | /** Initialize receive registers
|
---|
| 1360 | *
|
---|
[c4be33a] | 1361 | * @param e1000 E1000 data structure
|
---|
[bf84871] | 1362 | *
|
---|
| 1363 | */
|
---|
[c4be33a] | 1364 | static void e1000_initialize_rx_registers(e1000_t *e1000)
|
---|
[bf84871] | 1365 | {
|
---|
[1bc35b5] | 1366 | E1000_REG_WRITE(e1000, E1000_RDLEN, E1000_RX_FRAME_COUNT * 16);
|
---|
[c4be33a] | 1367 | E1000_REG_WRITE(e1000, E1000_RDH, 0);
|
---|
[a35b458] | 1368 |
|
---|
[1df224c] | 1369 | /* It is not posible to let HW use all descriptors */
|
---|
[1bc35b5] | 1370 | E1000_REG_WRITE(e1000, E1000_RDT, E1000_RX_FRAME_COUNT - 1);
|
---|
[a35b458] | 1371 |
|
---|
[1df224c] | 1372 | /* Set Broadcast Enable Bit */
|
---|
[c4be33a] | 1373 | E1000_REG_WRITE(e1000, E1000_RCTL, RCTL_BAM);
|
---|
[bf84871] | 1374 | }
|
---|
| 1375 |
|
---|
[1df224c] | 1376 | /** Initialize receive structure
|
---|
| 1377 | *
|
---|
[c4be33a] | 1378 | * @param nic NIC data
|
---|
[1df224c] | 1379 | *
|
---|
| 1380 | * @return EOK if succeed
|
---|
[cde999a] | 1381 | * @return An error code otherwise
|
---|
[bf84871] | 1382 | *
|
---|
| 1383 | */
|
---|
[b7fd2a0] | 1384 | static errno_t e1000_initialize_rx_structure(nic_t *nic)
|
---|
[bf84871] | 1385 | {
|
---|
[c4be33a] | 1386 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
| 1387 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
[a35b458] | 1388 |
|
---|
[bf9cb2f] | 1389 | e1000->rx_ring_virt = AS_AREA_ANY;
|
---|
[b7fd2a0] | 1390 | errno_t rc = dmamem_map_anonymous(
|
---|
[1bc35b5] | 1391 | E1000_RX_FRAME_COUNT * sizeof(e1000_rx_descriptor_t),
|
---|
[8442d10] | 1392 | DMAMEM_4GiB, AS_AREA_READ | AS_AREA_WRITE, 0,
|
---|
| 1393 | &e1000->rx_ring_phys, &e1000->rx_ring_virt);
|
---|
[1df224c] | 1394 | if (rc != EOK)
|
---|
[bf84871] | 1395 | return rc;
|
---|
[a35b458] | 1396 |
|
---|
[c4be33a] | 1397 | E1000_REG_WRITE(e1000, E1000_RDBAH,
|
---|
| 1398 | (uint32_t) (PTR_TO_U64(e1000->rx_ring_phys) >> 32));
|
---|
| 1399 | E1000_REG_WRITE(e1000, E1000_RDBAL,
|
---|
| 1400 | (uint32_t) PTR_TO_U64(e1000->rx_ring_phys));
|
---|
[a35b458] | 1401 |
|
---|
[8442d10] | 1402 | e1000->rx_frame_phys = (uintptr_t *)
|
---|
| 1403 | calloc(E1000_RX_FRAME_COUNT, sizeof(uintptr_t));
|
---|
[1bc35b5] | 1404 | e1000->rx_frame_virt =
|
---|
| 1405 | calloc(E1000_RX_FRAME_COUNT, sizeof(void *));
|
---|
[8442d10] | 1406 | if ((e1000->rx_frame_phys == NULL) || (e1000->rx_frame_virt == NULL)) {
|
---|
[1bc35b5] | 1407 | rc = ENOMEM;
|
---|
| 1408 | goto error;
|
---|
| 1409 | }
|
---|
[a35b458] | 1410 |
|
---|
[bf9cb2f] | 1411 | for (size_t i = 0; i < E1000_RX_FRAME_COUNT; i++) {
|
---|
| 1412 | uintptr_t frame_phys;
|
---|
| 1413 | void *frame_virt = AS_AREA_ANY;
|
---|
[a35b458] | 1414 |
|
---|
[8442d10] | 1415 | rc = dmamem_map_anonymous(E1000_MAX_SEND_FRAME_SIZE,
|
---|
| 1416 | DMAMEM_4GiB, AS_AREA_READ | AS_AREA_WRITE, 0,
|
---|
| 1417 | &frame_phys, &frame_virt);
|
---|
[1bc35b5] | 1418 | if (rc != EOK)
|
---|
| 1419 | goto error;
|
---|
[a35b458] | 1420 |
|
---|
[1bc35b5] | 1421 | e1000->rx_frame_phys[i] = frame_phys;
|
---|
[8442d10] | 1422 | e1000->rx_frame_virt[i] = frame_virt;
|
---|
[1bc35b5] | 1423 | }
|
---|
[a35b458] | 1424 |
|
---|
[1df224c] | 1425 | /* Write descriptor */
|
---|
[bf9cb2f] | 1426 | for (size_t i = 0; i < E1000_RX_FRAME_COUNT; i++)
|
---|
[1bc35b5] | 1427 | e1000_fill_new_rx_descriptor(nic, i);
|
---|
[a35b458] | 1428 |
|
---|
[c4be33a] | 1429 | e1000_initialize_rx_registers(e1000);
|
---|
[a35b458] | 1430 |
|
---|
[c4be33a] | 1431 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
[bf84871] | 1432 | return EOK;
|
---|
[a35b458] | 1433 |
|
---|
[1bc35b5] | 1434 | error:
|
---|
[bf9cb2f] | 1435 | for (size_t i = 0; i < E1000_RX_FRAME_COUNT; i++) {
|
---|
[1bc35b5] | 1436 | if (e1000->rx_frame_virt[i] != NULL) {
|
---|
| 1437 | dmamem_unmap_anonymous(e1000->rx_frame_virt[i]);
|
---|
[8442d10] | 1438 | e1000->rx_frame_phys[i] = 0;
|
---|
[1bc35b5] | 1439 | e1000->rx_frame_virt[i] = NULL;
|
---|
| 1440 | }
|
---|
| 1441 | }
|
---|
[a35b458] | 1442 |
|
---|
[1bc35b5] | 1443 | if (e1000->rx_frame_phys != NULL) {
|
---|
| 1444 | free(e1000->rx_frame_phys);
|
---|
| 1445 | e1000->rx_frame_phys = NULL;
|
---|
| 1446 | }
|
---|
[a35b458] | 1447 |
|
---|
[1bc35b5] | 1448 | if (e1000->rx_frame_virt != NULL) {
|
---|
| 1449 | free(e1000->rx_frame_virt);
|
---|
[8442d10] | 1450 | e1000->rx_frame_virt = NULL;
|
---|
[1bc35b5] | 1451 | }
|
---|
[a35b458] | 1452 |
|
---|
[1bc35b5] | 1453 | return rc;
|
---|
[bf84871] | 1454 | }
|
---|
| 1455 |
|
---|
[1df224c] | 1456 | /** Uninitialize receive structure
|
---|
| 1457 | *
|
---|
[c4be33a] | 1458 | * @param nic NIC data
|
---|
[bf84871] | 1459 | *
|
---|
| 1460 | */
|
---|
[c4be33a] | 1461 | static void e1000_uninitialize_rx_structure(nic_t *nic)
|
---|
[bf84871] | 1462 | {
|
---|
[c4be33a] | 1463 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
[a35b458] | 1464 |
|
---|
[1df224c] | 1465 | /* Write descriptor */
|
---|
[1bc35b5] | 1466 | for (unsigned int offset = 0; offset < E1000_RX_FRAME_COUNT; offset++) {
|
---|
| 1467 | dmamem_unmap_anonymous(e1000->rx_frame_virt[offset]);
|
---|
[8442d10] | 1468 | e1000->rx_frame_phys[offset] = 0;
|
---|
[1bc35b5] | 1469 | e1000->rx_frame_virt[offset] = NULL;
|
---|
[bf84871] | 1470 | }
|
---|
[a35b458] | 1471 |
|
---|
[1bc35b5] | 1472 | free(e1000->rx_frame_virt);
|
---|
[a35b458] | 1473 |
|
---|
[1bc35b5] | 1474 | e1000->rx_frame_phys = NULL;
|
---|
[8442d10] | 1475 | e1000->rx_frame_virt = NULL;
|
---|
[a35b458] | 1476 |
|
---|
[c4be33a] | 1477 | dmamem_unmap_anonymous(e1000->rx_ring_virt);
|
---|
[bf84871] | 1478 | }
|
---|
| 1479 |
|
---|
| 1480 | /** Clear receive descriptor ring
|
---|
| 1481 | *
|
---|
[c4be33a] | 1482 | * @param e1000 E1000 data
|
---|
[1df224c] | 1483 | *
|
---|
[bf84871] | 1484 | */
|
---|
[c4be33a] | 1485 | static void e1000_clear_rx_ring(e1000_t *e1000)
|
---|
[bf84871] | 1486 | {
|
---|
[1df224c] | 1487 | /* Write descriptor */
|
---|
| 1488 | for (unsigned int offset = 0;
|
---|
[1bc35b5] | 1489 | offset < E1000_RX_FRAME_COUNT;
|
---|
[1df224c] | 1490 | offset++)
|
---|
[c4be33a] | 1491 | e1000_clear_rx_descriptor(e1000, offset);
|
---|
[bf84871] | 1492 | }
|
---|
| 1493 |
|
---|
| 1494 | /** Initialize filters
|
---|
| 1495 | *
|
---|
[c4be33a] | 1496 | * @param e1000 E1000 data
|
---|
[1df224c] | 1497 | *
|
---|
[bf84871] | 1498 | */
|
---|
[c4be33a] | 1499 | static void e1000_initialize_filters(e1000_t *e1000)
|
---|
[bf84871] | 1500 | {
|
---|
[1df224c] | 1501 | /* Initialize address filter */
|
---|
[c4be33a] | 1502 | e1000->unicast_ra_count = 0;
|
---|
| 1503 | e1000->multicast_ra_count = 0;
|
---|
| 1504 | e1000_clear_unicast_receive_addresses(e1000);
|
---|
[bf84871] | 1505 | }
|
---|
| 1506 |
|
---|
| 1507 | /** Initialize VLAN
|
---|
| 1508 | *
|
---|
[c4be33a] | 1509 | * @param e1000 E1000 data
|
---|
[1df224c] | 1510 | *
|
---|
[bf84871] | 1511 | */
|
---|
[c4be33a] | 1512 | static void e1000_initialize_vlan(e1000_t *e1000)
|
---|
[bf84871] | 1513 | {
|
---|
[c4be33a] | 1514 | e1000->vlan_tag_add = false;
|
---|
[bf84871] | 1515 | }
|
---|
| 1516 |
|
---|
[1df224c] | 1517 | /** Fill MAC address from EEPROM to RA[0] register
|
---|
| 1518 | *
|
---|
[c4be33a] | 1519 | * @param e1000 E1000 data
|
---|
[bf84871] | 1520 | *
|
---|
| 1521 | */
|
---|
[c4be33a] | 1522 | static void e1000_fill_mac_from_eeprom(e1000_t *e1000)
|
---|
[bf84871] | 1523 | {
|
---|
[1df224c] | 1524 | /* MAC address from eeprom to RA[0] */
|
---|
[bf84871] | 1525 | nic_address_t address;
|
---|
[c4be33a] | 1526 | e1000_eeprom_get_address(e1000, &address);
|
---|
| 1527 | e1000_write_receive_address(e1000, 0, &address, true);
|
---|
[bf84871] | 1528 | }
|
---|
| 1529 |
|
---|
[1df224c] | 1530 | /** Initialize other registers
|
---|
| 1531 | *
|
---|
| 1532 | * @param dev E1000 data.
|
---|
| 1533 | *
|
---|
| 1534 | * @return EOK if succeed
|
---|
[cde999a] | 1535 | * @return An error code otherwise
|
---|
[bf84871] | 1536 | *
|
---|
| 1537 | */
|
---|
[c4be33a] | 1538 | static void e1000_initialize_registers(e1000_t *e1000)
|
---|
[bf84871] | 1539 | {
|
---|
[c4be33a] | 1540 | E1000_REG_WRITE(e1000, E1000_ITR,
|
---|
[1df224c] | 1541 | e1000_calculate_itr_interval_from_usecs(
|
---|
[9916841] | 1542 | E1000_DEFAULT_INTERRUPT_INTERVAL_USEC));
|
---|
[c4be33a] | 1543 | E1000_REG_WRITE(e1000, E1000_FCAH, 0);
|
---|
| 1544 | E1000_REG_WRITE(e1000, E1000_FCAL, 0);
|
---|
| 1545 | E1000_REG_WRITE(e1000, E1000_FCT, 0);
|
---|
| 1546 | E1000_REG_WRITE(e1000, E1000_FCTTV, 0);
|
---|
| 1547 | E1000_REG_WRITE(e1000, E1000_VET, VET_VALUE);
|
---|
| 1548 | E1000_REG_WRITE(e1000, E1000_CTRL, CTRL_ASDE);
|
---|
[bf84871] | 1549 | }
|
---|
| 1550 |
|
---|
[1df224c] | 1551 | /** Initialize transmit registers
|
---|
| 1552 | *
|
---|
[c4be33a] | 1553 | * @param e1000 E1000 data.
|
---|
[bf84871] | 1554 | *
|
---|
| 1555 | */
|
---|
[c4be33a] | 1556 | static void e1000_initialize_tx_registers(e1000_t *e1000)
|
---|
[bf84871] | 1557 | {
|
---|
[1bc35b5] | 1558 | E1000_REG_WRITE(e1000, E1000_TDLEN, E1000_TX_FRAME_COUNT * 16);
|
---|
[c4be33a] | 1559 | E1000_REG_WRITE(e1000, E1000_TDH, 0);
|
---|
| 1560 | E1000_REG_WRITE(e1000, E1000_TDT, 0);
|
---|
[a35b458] | 1561 |
|
---|
[c4be33a] | 1562 | E1000_REG_WRITE(e1000, E1000_TIPG,
|
---|
[1df224c] | 1563 | 10 << TIPG_IPGT_SHIFT |
|
---|
| 1564 | 8 << TIPG_IPGR1_SHIFT |
|
---|
| 1565 | 6 << TIPG_IPGR2_SHIFT);
|
---|
[a35b458] | 1566 |
|
---|
[c4be33a] | 1567 | E1000_REG_WRITE(e1000, E1000_TCTL,
|
---|
[1df224c] | 1568 | 0x0F << TCTL_CT_SHIFT /* Collision Threshold */ |
|
---|
| 1569 | 0x40 << TCTL_COLD_SHIFT /* Collision Distance */ |
|
---|
| 1570 | TCTL_PSP /* Pad Short Packets */);
|
---|
[bf84871] | 1571 | }
|
---|
| 1572 |
|
---|
| 1573 | /** Initialize transmit structure
|
---|
| 1574 | *
|
---|
[c4be33a] | 1575 | * @param e1000 E1000 data.
|
---|
[1df224c] | 1576 | *
|
---|
[bf84871] | 1577 | */
|
---|
[b7fd2a0] | 1578 | static errno_t e1000_initialize_tx_structure(e1000_t *e1000)
|
---|
[bf84871] | 1579 | {
|
---|
[6d8455d] | 1580 | size_t i;
|
---|
[a35b458] | 1581 |
|
---|
[c4be33a] | 1582 | fibril_mutex_lock(&e1000->tx_lock);
|
---|
[a35b458] | 1583 |
|
---|
[8442d10] | 1584 | e1000->tx_ring_phys = 0;
|
---|
[bf9cb2f] | 1585 | e1000->tx_ring_virt = AS_AREA_ANY;
|
---|
[a35b458] | 1586 |
|
---|
[6d8455d] | 1587 | e1000->tx_frame_phys = NULL;
|
---|
| 1588 | e1000->tx_frame_virt = NULL;
|
---|
[a35b458] | 1589 |
|
---|
[b7fd2a0] | 1590 | errno_t rc = dmamem_map_anonymous(
|
---|
[1bc35b5] | 1591 | E1000_TX_FRAME_COUNT * sizeof(e1000_tx_descriptor_t),
|
---|
[8442d10] | 1592 | DMAMEM_4GiB, AS_AREA_READ | AS_AREA_WRITE, 0,
|
---|
| 1593 | &e1000->tx_ring_phys, &e1000->tx_ring_virt);
|
---|
[1df224c] | 1594 | if (rc != EOK)
|
---|
[6d8455d] | 1595 | goto error;
|
---|
[a35b458] | 1596 |
|
---|
[acdb5bac] | 1597 | memset(e1000->tx_ring_virt, 0,
|
---|
[1bc35b5] | 1598 | E1000_TX_FRAME_COUNT * sizeof(e1000_tx_descriptor_t));
|
---|
[a35b458] | 1599 |
|
---|
[8442d10] | 1600 | e1000->tx_frame_phys = (uintptr_t *)
|
---|
| 1601 | calloc(E1000_TX_FRAME_COUNT, sizeof(uintptr_t));
|
---|
| 1602 | e1000->tx_frame_virt =
|
---|
| 1603 | calloc(E1000_TX_FRAME_COUNT, sizeof(void *));
|
---|
[6d8455d] | 1604 |
|
---|
[8442d10] | 1605 | if ((e1000->tx_frame_phys == NULL) || (e1000->tx_frame_virt == NULL)) {
|
---|
[6d8455d] | 1606 | rc = ENOMEM;
|
---|
| 1607 | goto error;
|
---|
| 1608 | }
|
---|
[a35b458] | 1609 |
|
---|
[1bc35b5] | 1610 | for (i = 0; i < E1000_TX_FRAME_COUNT; i++) {
|
---|
[bf9cb2f] | 1611 | e1000->tx_frame_virt[i] = AS_AREA_ANY;
|
---|
[8442d10] | 1612 | rc = dmamem_map_anonymous(E1000_MAX_SEND_FRAME_SIZE,
|
---|
| 1613 | DMAMEM_4GiB, AS_AREA_READ | AS_AREA_WRITE,
|
---|
[6d8455d] | 1614 | 0, &e1000->tx_frame_phys[i], &e1000->tx_frame_virt[i]);
|
---|
| 1615 | if (rc != EOK)
|
---|
| 1616 | goto error;
|
---|
| 1617 | }
|
---|
[a35b458] | 1618 |
|
---|
[c4be33a] | 1619 | E1000_REG_WRITE(e1000, E1000_TDBAH,
|
---|
| 1620 | (uint32_t) (PTR_TO_U64(e1000->tx_ring_phys) >> 32));
|
---|
| 1621 | E1000_REG_WRITE(e1000, E1000_TDBAL,
|
---|
| 1622 | (uint32_t) PTR_TO_U64(e1000->tx_ring_phys));
|
---|
[a35b458] | 1623 |
|
---|
[c4be33a] | 1624 | e1000_initialize_tx_registers(e1000);
|
---|
[a35b458] | 1625 |
|
---|
[c4be33a] | 1626 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
[bf84871] | 1627 | return EOK;
|
---|
[a35b458] | 1628 |
|
---|
[6d8455d] | 1629 | error:
|
---|
| 1630 | if (e1000->tx_ring_virt != NULL) {
|
---|
| 1631 | dmamem_unmap_anonymous(e1000->tx_ring_virt);
|
---|
| 1632 | e1000->tx_ring_virt = NULL;
|
---|
| 1633 | }
|
---|
[a35b458] | 1634 |
|
---|
[8442d10] | 1635 | if ((e1000->tx_frame_phys != NULL) && (e1000->tx_frame_virt != NULL)) {
|
---|
[1bc35b5] | 1636 | for (i = 0; i < E1000_TX_FRAME_COUNT; i++) {
|
---|
[6d8455d] | 1637 | if (e1000->tx_frame_virt[i] != NULL) {
|
---|
| 1638 | dmamem_unmap_anonymous(e1000->tx_frame_virt[i]);
|
---|
[8442d10] | 1639 | e1000->tx_frame_phys[i] = 0;
|
---|
[6d8455d] | 1640 | e1000->tx_frame_virt[i] = NULL;
|
---|
| 1641 | }
|
---|
| 1642 | }
|
---|
| 1643 | }
|
---|
[a35b458] | 1644 |
|
---|
[6d8455d] | 1645 | if (e1000->tx_frame_phys != NULL) {
|
---|
| 1646 | free(e1000->tx_frame_phys);
|
---|
| 1647 | e1000->tx_frame_phys = NULL;
|
---|
| 1648 | }
|
---|
[a35b458] | 1649 |
|
---|
[6d8455d] | 1650 | if (e1000->tx_frame_virt != NULL) {
|
---|
| 1651 | free(e1000->tx_frame_virt);
|
---|
[8442d10] | 1652 | e1000->tx_frame_virt = NULL;
|
---|
[6d8455d] | 1653 | }
|
---|
[a35b458] | 1654 |
|
---|
[6d8455d] | 1655 | return rc;
|
---|
[bf84871] | 1656 | }
|
---|
| 1657 |
|
---|
[1df224c] | 1658 | /** Uninitialize transmit structure
|
---|
| 1659 | *
|
---|
[c4be33a] | 1660 | * @param nic NIC data
|
---|
[bf84871] | 1661 | *
|
---|
| 1662 | */
|
---|
[c4be33a] | 1663 | static void e1000_uninitialize_tx_structure(e1000_t *e1000)
|
---|
[bf84871] | 1664 | {
|
---|
[6d8455d] | 1665 | size_t i;
|
---|
[a35b458] | 1666 |
|
---|
[1bc35b5] | 1667 | for (i = 0; i < E1000_TX_FRAME_COUNT; i++) {
|
---|
[6d8455d] | 1668 | dmamem_unmap_anonymous(e1000->tx_frame_virt[i]);
|
---|
[8442d10] | 1669 | e1000->tx_frame_phys[i] = 0;
|
---|
[6d8455d] | 1670 | e1000->tx_frame_virt[i] = NULL;
|
---|
| 1671 | }
|
---|
[a35b458] | 1672 |
|
---|
[6d8455d] | 1673 | if (e1000->tx_frame_phys != NULL) {
|
---|
| 1674 | free(e1000->tx_frame_phys);
|
---|
| 1675 | e1000->tx_frame_phys = NULL;
|
---|
| 1676 | }
|
---|
[a35b458] | 1677 |
|
---|
[6d8455d] | 1678 | if (e1000->tx_frame_virt != NULL) {
|
---|
| 1679 | free(e1000->tx_frame_virt);
|
---|
[8442d10] | 1680 | e1000->tx_frame_virt = NULL;
|
---|
[6d8455d] | 1681 | }
|
---|
[a35b458] | 1682 |
|
---|
[c4be33a] | 1683 | dmamem_unmap_anonymous(e1000->tx_ring_virt);
|
---|
[bf84871] | 1684 | }
|
---|
| 1685 |
|
---|
| 1686 | /** Clear transmit descriptor ring
|
---|
| 1687 | *
|
---|
[c4be33a] | 1688 | * @param nic NIC data
|
---|
[1df224c] | 1689 | *
|
---|
[bf84871] | 1690 | */
|
---|
[c4be33a] | 1691 | static void e1000_clear_tx_ring(nic_t *nic)
|
---|
[bf84871] | 1692 | {
|
---|
[1df224c] | 1693 | /* Write descriptor */
|
---|
| 1694 | for (unsigned int offset = 0;
|
---|
[1bc35b5] | 1695 | offset < E1000_TX_FRAME_COUNT;
|
---|
[1df224c] | 1696 | offset++)
|
---|
[c4be33a] | 1697 | e1000_clear_tx_descriptor(nic, offset);
|
---|
[bf84871] | 1698 | }
|
---|
| 1699 |
|
---|
| 1700 | /** Enable transmit
|
---|
| 1701 | *
|
---|
[c4be33a] | 1702 | * @param e1000 E1000 data
|
---|
[1df224c] | 1703 | *
|
---|
[bf84871] | 1704 | */
|
---|
[c4be33a] | 1705 | static void e1000_enable_tx(e1000_t *e1000)
|
---|
[bf84871] | 1706 | {
|
---|
[1df224c] | 1707 | /* Set Transmit Enable Bit */
|
---|
[c4be33a] | 1708 | E1000_REG_WRITE(e1000, E1000_TCTL,
|
---|
| 1709 | E1000_REG_READ(e1000, E1000_TCTL) | (TCTL_EN));
|
---|
[bf84871] | 1710 | }
|
---|
| 1711 |
|
---|
| 1712 | /** Disable transmit
|
---|
| 1713 | *
|
---|
[c4be33a] | 1714 | * @param e1000 E1000 data
|
---|
[1df224c] | 1715 | *
|
---|
[bf84871] | 1716 | */
|
---|
[c4be33a] | 1717 | static void e1000_disable_tx(e1000_t *e1000)
|
---|
[bf84871] | 1718 | {
|
---|
[1df224c] | 1719 | /* Clear Transmit Enable Bit */
|
---|
[c4be33a] | 1720 | E1000_REG_WRITE(e1000, E1000_TCTL,
|
---|
| 1721 | E1000_REG_READ(e1000, E1000_TCTL) & ~(TCTL_EN));
|
---|
[bf84871] | 1722 | }
|
---|
| 1723 |
|
---|
| 1724 | /** Reset E1000 device
|
---|
| 1725 | *
|
---|
[c4be33a] | 1726 | * @param e1000 The E1000 data
|
---|
[1df224c] | 1727 | *
|
---|
[bf84871] | 1728 | */
|
---|
[b7fd2a0] | 1729 | static errno_t e1000_reset(nic_t *nic)
|
---|
[bf84871] | 1730 | {
|
---|
[c4be33a] | 1731 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
[a35b458] | 1732 |
|
---|
[c4be33a] | 1733 | E1000_REG_WRITE(e1000, E1000_CTRL, CTRL_RST);
|
---|
[a35b458] | 1734 |
|
---|
[1df224c] | 1735 | /* Wait for the reset */
|
---|
[5f97ef44] | 1736 | fibril_usleep(20);
|
---|
[a35b458] | 1737 |
|
---|
[1df224c] | 1738 | /* check if RST_BIT cleared */
|
---|
[c4be33a] | 1739 | if (E1000_REG_READ(e1000, E1000_CTRL) & (CTRL_RST))
|
---|
[1df224c] | 1740 | return EINVAL;
|
---|
[a35b458] | 1741 |
|
---|
[c4be33a] | 1742 | e1000_initialize_registers(e1000);
|
---|
| 1743 | e1000_initialize_rx_registers(e1000);
|
---|
| 1744 | e1000_initialize_tx_registers(e1000);
|
---|
| 1745 | e1000_fill_mac_from_eeprom(e1000);
|
---|
| 1746 | e1000_initialize_filters(e1000);
|
---|
| 1747 | e1000_initialize_vlan(e1000);
|
---|
[a35b458] | 1748 |
|
---|
[bf84871] | 1749 | return EOK;
|
---|
| 1750 | }
|
---|
| 1751 |
|
---|
[1bc35b5] | 1752 | /** Activate the device to receive and transmit frames
|
---|
[bf84871] | 1753 | *
|
---|
[c4be33a] | 1754 | * @param nic NIC driver data
|
---|
[1df224c] | 1755 | *
|
---|
| 1756 | * @return EOK if activated successfully
|
---|
| 1757 | * @return Error code otherwise
|
---|
| 1758 | *
|
---|
[bf84871] | 1759 | */
|
---|
[b7fd2a0] | 1760 | static errno_t e1000_on_activating(nic_t *nic)
|
---|
[bf84871] | 1761 | {
|
---|
[c4be33a] | 1762 | assert(nic);
|
---|
[a35b458] | 1763 |
|
---|
[c4be33a] | 1764 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
[a35b458] | 1765 |
|
---|
[c4be33a] | 1766 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
| 1767 | fibril_mutex_lock(&e1000->tx_lock);
|
---|
| 1768 | fibril_mutex_lock(&e1000->ctrl_lock);
|
---|
[a35b458] | 1769 |
|
---|
[c4be33a] | 1770 | e1000_enable_interrupts(e1000);
|
---|
[a35b458] | 1771 |
|
---|
[b7fd2a0] | 1772 | errno_t rc = hw_res_enable_interrupt(e1000->parent_sess, e1000->irq);
|
---|
[e5424e9] | 1773 | if (rc != EOK) {
|
---|
| 1774 | e1000_disable_interrupts(e1000);
|
---|
| 1775 | fibril_mutex_unlock(&e1000->ctrl_lock);
|
---|
| 1776 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
| 1777 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
| 1778 | return rc;
|
---|
| 1779 | }
|
---|
[a35b458] | 1780 |
|
---|
[c4be33a] | 1781 | e1000_clear_rx_ring(e1000);
|
---|
| 1782 | e1000_enable_rx(e1000);
|
---|
[a35b458] | 1783 |
|
---|
[c4be33a] | 1784 | e1000_clear_tx_ring(nic);
|
---|
| 1785 | e1000_enable_tx(e1000);
|
---|
[a35b458] | 1786 |
|
---|
[c4be33a] | 1787 | uint32_t ctrl = E1000_REG_READ(e1000, E1000_CTRL);
|
---|
[bf84871] | 1788 | ctrl |= CTRL_SLU;
|
---|
[c4be33a] | 1789 | E1000_REG_WRITE(e1000, E1000_CTRL, ctrl);
|
---|
[a35b458] | 1790 |
|
---|
[c4be33a] | 1791 | fibril_mutex_unlock(&e1000->ctrl_lock);
|
---|
| 1792 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
| 1793 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
[a35b458] | 1794 |
|
---|
[bf84871] | 1795 | return EOK;
|
---|
| 1796 | }
|
---|
| 1797 |
|
---|
| 1798 | /** Callback for NIC_STATE_DOWN change
|
---|
| 1799 | *
|
---|
[c4be33a] | 1800 | * @param nic NIC driver data
|
---|
[1df224c] | 1801 | *
|
---|
| 1802 | * @return EOK if succeed
|
---|
| 1803 | * @return Error code otherwise
|
---|
| 1804 | *
|
---|
[bf84871] | 1805 | */
|
---|
[b7fd2a0] | 1806 | static errno_t e1000_on_down_unlocked(nic_t *nic)
|
---|
[bf84871] | 1807 | {
|
---|
[c4be33a] | 1808 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
[a35b458] | 1809 |
|
---|
[c4be33a] | 1810 | uint32_t ctrl = E1000_REG_READ(e1000, E1000_CTRL);
|
---|
[bf84871] | 1811 | ctrl &= ~CTRL_SLU;
|
---|
[c4be33a] | 1812 | E1000_REG_WRITE(e1000, E1000_CTRL, ctrl);
|
---|
[a35b458] | 1813 |
|
---|
[c4be33a] | 1814 | e1000_disable_tx(e1000);
|
---|
| 1815 | e1000_disable_rx(e1000);
|
---|
[a35b458] | 1816 |
|
---|
[d51838f] | 1817 | hw_res_disable_interrupt(e1000->parent_sess, e1000->irq);
|
---|
[c4be33a] | 1818 | e1000_disable_interrupts(e1000);
|
---|
[a35b458] | 1819 |
|
---|
[1df224c] | 1820 | /*
|
---|
| 1821 | * Wait for the for the end of all data
|
---|
| 1822 | * transfers to descriptors.
|
---|
| 1823 | */
|
---|
[5f97ef44] | 1824 | fibril_usleep(100);
|
---|
[a35b458] | 1825 |
|
---|
[bf84871] | 1826 | return EOK;
|
---|
| 1827 | }
|
---|
| 1828 |
|
---|
| 1829 | /** Callback for NIC_STATE_DOWN change
|
---|
| 1830 | *
|
---|
[c4be33a] | 1831 | * @param nic NIC driver data
|
---|
[1df224c] | 1832 | *
|
---|
| 1833 | * @return EOK if succeed
|
---|
| 1834 | * @return Error code otherwise
|
---|
| 1835 | *
|
---|
[bf84871] | 1836 | */
|
---|
[b7fd2a0] | 1837 | static errno_t e1000_on_down(nic_t *nic)
|
---|
[bf84871] | 1838 | {
|
---|
[c4be33a] | 1839 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
[a35b458] | 1840 |
|
---|
[c4be33a] | 1841 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
| 1842 | fibril_mutex_lock(&e1000->tx_lock);
|
---|
| 1843 | fibril_mutex_lock(&e1000->ctrl_lock);
|
---|
[a35b458] | 1844 |
|
---|
[b7fd2a0] | 1845 | errno_t rc = e1000_on_down_unlocked(nic);
|
---|
[a35b458] | 1846 |
|
---|
[c4be33a] | 1847 | fibril_mutex_unlock(&e1000->ctrl_lock);
|
---|
| 1848 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
| 1849 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
[a35b458] | 1850 |
|
---|
[bf84871] | 1851 | return rc;
|
---|
| 1852 | }
|
---|
| 1853 |
|
---|
| 1854 | /** Callback for NIC_STATE_STOPPED change
|
---|
| 1855 | *
|
---|
[c4be33a] | 1856 | * @param nic NIC driver data
|
---|
[1df224c] | 1857 | *
|
---|
| 1858 | * @return EOK if succeed
|
---|
| 1859 | * @return Error code otherwise
|
---|
| 1860 | *
|
---|
[bf84871] | 1861 | */
|
---|
[b7fd2a0] | 1862 | static errno_t e1000_on_stopping(nic_t *nic)
|
---|
[bf84871] | 1863 | {
|
---|
[c4be33a] | 1864 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
[a35b458] | 1865 |
|
---|
[c4be33a] | 1866 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
| 1867 | fibril_mutex_lock(&e1000->tx_lock);
|
---|
| 1868 | fibril_mutex_lock(&e1000->ctrl_lock);
|
---|
[a35b458] | 1869 |
|
---|
[b7fd2a0] | 1870 | errno_t rc = e1000_on_down_unlocked(nic);
|
---|
[1df224c] | 1871 | if (rc == EOK)
|
---|
[c4be33a] | 1872 | rc = e1000_reset(nic);
|
---|
[a35b458] | 1873 |
|
---|
[c4be33a] | 1874 | fibril_mutex_unlock(&e1000->ctrl_lock);
|
---|
| 1875 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
| 1876 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
[a35b458] | 1877 |
|
---|
[bf84871] | 1878 | return rc;
|
---|
| 1879 | }
|
---|
| 1880 |
|
---|
| 1881 | /** Create driver data structure
|
---|
| 1882 | *
|
---|
[1df224c] | 1883 | * @return Intialized device data structure or NULL
|
---|
| 1884 | *
|
---|
[bf84871] | 1885 | */
|
---|
| 1886 | static e1000_t *e1000_create_dev_data(ddf_dev_t *dev)
|
---|
| 1887 | {
|
---|
[c4be33a] | 1888 | nic_t *nic = nic_create_and_bind(dev);
|
---|
| 1889 | if (!nic)
|
---|
[bf84871] | 1890 | return NULL;
|
---|
[a35b458] | 1891 |
|
---|
[c4be33a] | 1892 | e1000_t *e1000 = malloc(sizeof(e1000_t));
|
---|
| 1893 | if (!e1000) {
|
---|
[bf84871] | 1894 | nic_unbind_and_destroy(dev);
|
---|
| 1895 | return NULL;
|
---|
| 1896 | }
|
---|
[a35b458] | 1897 |
|
---|
[acdb5bac] | 1898 | memset(e1000, 0, sizeof(e1000_t));
|
---|
[d51838f] | 1899 | e1000->dev = dev;
|
---|
[a35b458] | 1900 |
|
---|
[c4be33a] | 1901 | nic_set_specific(nic, e1000);
|
---|
[6d8455d] | 1902 | nic_set_send_frame_handler(nic, e1000_send_frame);
|
---|
[c4be33a] | 1903 | nic_set_state_change_handlers(nic, e1000_on_activating,
|
---|
[1df224c] | 1904 | e1000_on_down, e1000_on_stopping);
|
---|
[c4be33a] | 1905 | nic_set_filtering_change_handlers(nic,
|
---|
[1df224c] | 1906 | e1000_on_unicast_mode_change, e1000_on_multicast_mode_change,
|
---|
| 1907 | e1000_on_broadcast_mode_change, NULL, e1000_on_vlan_mask_change);
|
---|
[c4be33a] | 1908 | nic_set_poll_handlers(nic, e1000_poll_mode_change, e1000_poll);
|
---|
[a35b458] | 1909 |
|
---|
[c4be33a] | 1910 | fibril_mutex_initialize(&e1000->ctrl_lock);
|
---|
| 1911 | fibril_mutex_initialize(&e1000->rx_lock);
|
---|
| 1912 | fibril_mutex_initialize(&e1000->tx_lock);
|
---|
| 1913 | fibril_mutex_initialize(&e1000->eeprom_lock);
|
---|
[a35b458] | 1914 |
|
---|
[c4be33a] | 1915 | return e1000;
|
---|
[bf84871] | 1916 | }
|
---|
| 1917 |
|
---|
[1df224c] | 1918 | /** Delete driver data structure
|
---|
| 1919 | *
|
---|
| 1920 | * @param data E1000 device data structure
|
---|
[bf84871] | 1921 | *
|
---|
| 1922 | */
|
---|
[870841cf] | 1923 | static void e1000_delete_dev_data(ddf_dev_t *dev)
|
---|
[bf84871] | 1924 | {
|
---|
| 1925 | assert(dev);
|
---|
[a35b458] | 1926 |
|
---|
[56fd7cf] | 1927 | if (ddf_dev_data_get(dev) != NULL)
|
---|
[bf84871] | 1928 | nic_unbind_and_destroy(dev);
|
---|
| 1929 | }
|
---|
| 1930 |
|
---|
[1df224c] | 1931 | /** Clean up the E1000 device structure.
|
---|
| 1932 | *
|
---|
| 1933 | * @param dev Device structure.
|
---|
[bf84871] | 1934 | *
|
---|
| 1935 | */
|
---|
| 1936 | static void e1000_dev_cleanup(ddf_dev_t *dev)
|
---|
| 1937 | {
|
---|
| 1938 | assert(dev);
|
---|
[a35b458] | 1939 |
|
---|
[bf84871] | 1940 | e1000_delete_dev_data(dev);
|
---|
| 1941 | }
|
---|
| 1942 |
|
---|
| 1943 | /** Fill the irq and io_addr part of device data structure
|
---|
| 1944 | *
|
---|
[1df224c] | 1945 | * The hw_resources must be obtained before calling this function
|
---|
| 1946 | *
|
---|
| 1947 | * @param dev Device structure
|
---|
| 1948 | * @param hw_resources Hardware resources obtained from the parent device
|
---|
| 1949 | *
|
---|
| 1950 | * @return EOK if succeed
|
---|
[cde999a] | 1951 | * @return An error code otherwise
|
---|
[bf84871] | 1952 | *
|
---|
| 1953 | */
|
---|
[b7fd2a0] | 1954 | static errno_t e1000_fill_resource_info(ddf_dev_t *dev,
|
---|
[1df224c] | 1955 | const hw_res_list_parsed_t *hw_resources)
|
---|
[bf84871] | 1956 | {
|
---|
[c4be33a] | 1957 | e1000_t *e1000 = DRIVER_DATA_DEV(dev);
|
---|
[a35b458] | 1958 |
|
---|
[1df224c] | 1959 | if (hw_resources->irqs.count != 1)
|
---|
[bf84871] | 1960 | return EINVAL;
|
---|
[a35b458] | 1961 |
|
---|
[c4be33a] | 1962 | e1000->irq = hw_resources->irqs.irqs[0];
|
---|
| 1963 | e1000->reg_base_phys =
|
---|
[7de1988c] | 1964 | MEMADDR_TO_PTR(RNGABS(hw_resources->mem_ranges.ranges[0]));
|
---|
[a35b458] | 1965 |
|
---|
[bf84871] | 1966 | return EOK;
|
---|
| 1967 | }
|
---|
| 1968 |
|
---|
| 1969 | /** Obtain information about hardware resources of the device
|
---|
| 1970 | *
|
---|
[1df224c] | 1971 | * The device must be connected to the parent
|
---|
| 1972 | *
|
---|
| 1973 | * @param dev Device structure
|
---|
| 1974 | *
|
---|
| 1975 | * @return EOK if succeed
|
---|
[cde999a] | 1976 | * @return An error code otherwise
|
---|
[bf84871] | 1977 | *
|
---|
| 1978 | */
|
---|
[b7fd2a0] | 1979 | static errno_t e1000_get_resource_info(ddf_dev_t *dev)
|
---|
[bf84871] | 1980 | {
|
---|
| 1981 | assert(dev != NULL);
|
---|
| 1982 | assert(NIC_DATA_DEV(dev) != NULL);
|
---|
[a35b458] | 1983 |
|
---|
[bf84871] | 1984 | hw_res_list_parsed_t hw_res_parsed;
|
---|
| 1985 | hw_res_list_parsed_init(&hw_res_parsed);
|
---|
[a35b458] | 1986 |
|
---|
[bf84871] | 1987 | /* Get hw resources form parent driver */
|
---|
[b7fd2a0] | 1988 | errno_t rc = nic_get_resources(NIC_DATA_DEV(dev), &hw_res_parsed);
|
---|
[bf84871] | 1989 | if (rc != EOK)
|
---|
| 1990 | return rc;
|
---|
[a35b458] | 1991 |
|
---|
[bf84871] | 1992 | /* Fill resources information to the device */
|
---|
| 1993 | rc = e1000_fill_resource_info(dev, &hw_res_parsed);
|
---|
| 1994 | hw_res_list_parsed_clean(&hw_res_parsed);
|
---|
[a35b458] | 1995 |
|
---|
[bf84871] | 1996 | return rc;
|
---|
| 1997 | }
|
---|
| 1998 |
|
---|
[1df224c] | 1999 | /** Initialize the E1000 device structure
|
---|
| 2000 | *
|
---|
| 2001 | * @param dev Device information
|
---|
| 2002 | *
|
---|
| 2003 | * @return EOK if succeed
|
---|
[cde999a] | 2004 | * @return An error code otherwise
|
---|
[bf84871] | 2005 | *
|
---|
| 2006 | */
|
---|
[b7fd2a0] | 2007 | static errno_t e1000_device_initialize(ddf_dev_t *dev)
|
---|
[bf84871] | 2008 | {
|
---|
| 2009 | /* Allocate driver data for the device. */
|
---|
[c4be33a] | 2010 | e1000_t *e1000 = e1000_create_dev_data(dev);
|
---|
[77c2b02] | 2011 | if (e1000 == NULL) {
|
---|
| 2012 | ddf_msg(LVL_ERROR, "Unable to allocate device softstate");
|
---|
[bf84871] | 2013 | return ENOMEM;
|
---|
[77c2b02] | 2014 | }
|
---|
[a35b458] | 2015 |
|
---|
[d51838f] | 2016 | e1000->parent_sess = ddf_dev_parent_sess_get(dev);
|
---|
| 2017 | if (e1000->parent_sess == NULL) {
|
---|
| 2018 | ddf_msg(LVL_ERROR, "Failed connecting parent device.");
|
---|
| 2019 | return EIO;
|
---|
| 2020 | }
|
---|
[a35b458] | 2021 |
|
---|
[bf84871] | 2022 | /* Obtain and fill hardware resources info */
|
---|
[b7fd2a0] | 2023 | errno_t rc = e1000_get_resource_info(dev);
|
---|
[bf84871] | 2024 | if (rc != EOK) {
|
---|
[77c2b02] | 2025 | ddf_msg(LVL_ERROR, "Cannot obtain hardware resources");
|
---|
[1df224c] | 2026 | e1000_dev_cleanup(dev);
|
---|
| 2027 | return rc;
|
---|
[bf84871] | 2028 | }
|
---|
[a35b458] | 2029 |
|
---|
[77c2b02] | 2030 | uint16_t device_id;
|
---|
[56fd7cf] | 2031 | rc = pci_config_space_read_16(ddf_dev_parent_sess_get(dev), PCI_DEVICE_ID,
|
---|
[77c2b02] | 2032 | &device_id);
|
---|
[bf84871] | 2033 | if (rc != EOK) {
|
---|
[77c2b02] | 2034 | ddf_msg(LVL_ERROR, "Cannot access PCI configuration space");
|
---|
[1df224c] | 2035 | e1000_dev_cleanup(dev);
|
---|
| 2036 | return rc;
|
---|
[bf84871] | 2037 | }
|
---|
[a35b458] | 2038 |
|
---|
[77c2b02] | 2039 | e1000_board_t board;
|
---|
| 2040 | switch (device_id) {
|
---|
[9f0fb84] | 2041 | case 0x100e:
|
---|
| 2042 | case 0x1015:
|
---|
| 2043 | case 0x1016:
|
---|
| 2044 | case 0x1017:
|
---|
| 2045 | board = E1000_82540;
|
---|
| 2046 | break;
|
---|
[77c2b02] | 2047 | case 0x1013:
|
---|
| 2048 | case 0x1018:
|
---|
| 2049 | case 0x1078:
|
---|
| 2050 | board = E1000_82541;
|
---|
| 2051 | break;
|
---|
| 2052 | case 0x1076:
|
---|
| 2053 | case 0x1077:
|
---|
| 2054 | case 0x107c:
|
---|
| 2055 | board = E1000_82541REV2;
|
---|
| 2056 | break;
|
---|
[9f0fb84] | 2057 | case 0x100f:
|
---|
| 2058 | case 0x1011:
|
---|
| 2059 | case 0x1026:
|
---|
| 2060 | case 0x1027:
|
---|
| 2061 | case 0x1028:
|
---|
| 2062 | board = E1000_82545;
|
---|
| 2063 | break;
|
---|
| 2064 | case 0x1010:
|
---|
| 2065 | case 0x1012:
|
---|
| 2066 | case 0x101d:
|
---|
| 2067 | case 0x1079:
|
---|
| 2068 | case 0x107a:
|
---|
| 2069 | case 0x107b:
|
---|
| 2070 | board = E1000_82546;
|
---|
| 2071 | break;
|
---|
[77c2b02] | 2072 | case 0x1019:
|
---|
| 2073 | case 0x101a:
|
---|
| 2074 | board = E1000_82547;
|
---|
| 2075 | break;
|
---|
| 2076 | case 0x10b9:
|
---|
| 2077 | board = E1000_82572;
|
---|
| 2078 | break;
|
---|
| 2079 | case 0x1096:
|
---|
| 2080 | board = E1000_80003ES2;
|
---|
| 2081 | break;
|
---|
| 2082 | default:
|
---|
| 2083 | ddf_msg(LVL_ERROR, "Device not supported (%#" PRIx16 ")",
|
---|
| 2084 | device_id);
|
---|
| 2085 | e1000_dev_cleanup(dev);
|
---|
| 2086 | return ENOTSUP;
|
---|
| 2087 | }
|
---|
[a35b458] | 2088 |
|
---|
[77c2b02] | 2089 | switch (board) {
|
---|
[9f0fb84] | 2090 | case E1000_82540:
|
---|
[77c2b02] | 2091 | case E1000_82541:
|
---|
| 2092 | case E1000_82541REV2:
|
---|
[9f0fb84] | 2093 | case E1000_82545:
|
---|
| 2094 | case E1000_82546:
|
---|
[77c2b02] | 2095 | e1000->info.eerd_start = 0x01;
|
---|
| 2096 | e1000->info.eerd_done = 0x10;
|
---|
| 2097 | e1000->info.eerd_address_offset = 8;
|
---|
| 2098 | e1000->info.eerd_data_offset = 16;
|
---|
| 2099 | break;
|
---|
| 2100 | case E1000_82547:
|
---|
[d81eaf94] | 2101 | case E1000_82572:
|
---|
[77c2b02] | 2102 | case E1000_80003ES2:
|
---|
| 2103 | e1000->info.eerd_start = 0x01;
|
---|
| 2104 | e1000->info.eerd_done = 0x02;
|
---|
| 2105 | e1000->info.eerd_address_offset = 2;
|
---|
| 2106 | e1000->info.eerd_data_offset = 16;
|
---|
| 2107 | break;
|
---|
| 2108 | }
|
---|
[a35b458] | 2109 |
|
---|
[1df224c] | 2110 | return EOK;
|
---|
[bf84871] | 2111 | }
|
---|
| 2112 |
|
---|
[1df224c] | 2113 | /** Enable the I/O ports of the device.
|
---|
| 2114 | *
|
---|
| 2115 | * @param dev E1000 device.
|
---|
| 2116 | *
|
---|
| 2117 | * @return EOK if successed
|
---|
[cde999a] | 2118 | * @return An error code otherwise
|
---|
[bf84871] | 2119 | *
|
---|
| 2120 | */
|
---|
[b7fd2a0] | 2121 | static errno_t e1000_pio_enable(ddf_dev_t *dev)
|
---|
[bf84871] | 2122 | {
|
---|
[c4be33a] | 2123 | e1000_t *e1000 = DRIVER_DATA_DEV(dev);
|
---|
[a35b458] | 2124 |
|
---|
[b7fd2a0] | 2125 | errno_t rc = pio_enable(e1000->reg_base_phys, 8 * PAGE_SIZE,
|
---|
[c4be33a] | 2126 | &e1000->reg_base_virt);
|
---|
[1df224c] | 2127 | if (rc != EOK)
|
---|
[bf84871] | 2128 | return EADDRNOTAVAIL;
|
---|
[a35b458] | 2129 |
|
---|
[bf84871] | 2130 | return EOK;
|
---|
| 2131 | }
|
---|
| 2132 |
|
---|
[9916841] | 2133 | /** Probe and initialize the newly added device.
|
---|
[bf84871] | 2134 | *
|
---|
[1df224c] | 2135 | * @param dev E1000 device.
|
---|
| 2136 | *
|
---|
[bf84871] | 2137 | */
|
---|
[b7fd2a0] | 2138 | errno_t e1000_dev_add(ddf_dev_t *dev)
|
---|
[bf84871] | 2139 | {
|
---|
[e86b8f0] | 2140 | ddf_fun_t *fun;
|
---|
[a35b458] | 2141 |
|
---|
[1df224c] | 2142 | /* Initialize device structure for E1000 */
|
---|
[b7fd2a0] | 2143 | errno_t rc = e1000_device_initialize(dev);
|
---|
[bf84871] | 2144 | if (rc != EOK)
|
---|
| 2145 | return rc;
|
---|
[a35b458] | 2146 |
|
---|
[1df224c] | 2147 | /* Device initialization */
|
---|
[56fd7cf] | 2148 | nic_t *nic = ddf_dev_data_get(dev);
|
---|
[c4be33a] | 2149 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
[a35b458] | 2150 |
|
---|
[bf84871] | 2151 | /* Map registers */
|
---|
| 2152 | rc = e1000_pio_enable(dev);
|
---|
| 2153 | if (rc != EOK)
|
---|
| 2154 | goto err_destroy;
|
---|
[a35b458] | 2155 |
|
---|
[c4be33a] | 2156 | e1000_initialize_registers(e1000);
|
---|
| 2157 | rc = e1000_initialize_tx_structure(e1000);
|
---|
[1df224c] | 2158 | if (rc != EOK)
|
---|
[bf84871] | 2159 | goto err_pio;
|
---|
[a35b458] | 2160 |
|
---|
[c4be33a] | 2161 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
[a35b458] | 2162 |
|
---|
[c4be33a] | 2163 | e1000_fill_mac_from_eeprom(e1000);
|
---|
| 2164 | e1000_initialize_filters(e1000);
|
---|
[a35b458] | 2165 |
|
---|
[c4be33a] | 2166 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
[a35b458] | 2167 |
|
---|
[c4be33a] | 2168 | e1000_initialize_vlan(e1000);
|
---|
[a35b458] | 2169 |
|
---|
[e86b8f0] | 2170 | fun = ddf_fun_create(nic_get_ddf_dev(nic), fun_exposed, "port0");
|
---|
| 2171 | if (fun == NULL)
|
---|
[bf84871] | 2172 | goto err_tx_structure;
|
---|
[e86b8f0] | 2173 | nic_set_ddf_fun(nic, fun);
|
---|
[56fd7cf] | 2174 | ddf_fun_set_ops(fun, &e1000_dev_ops);
|
---|
[a35b458] | 2175 |
|
---|
[eadaeae8] | 2176 | cap_irq_handle_t irq_handle;
|
---|
| 2177 | rc = e1000_register_int_handler(nic, &irq_handle);
|
---|
[071a1ddb] | 2178 | if (rc != EOK) {
|
---|
[e86b8f0] | 2179 | goto err_fun_create;
|
---|
[e9d15d9] | 2180 | }
|
---|
[a35b458] | 2181 |
|
---|
[c4be33a] | 2182 | rc = e1000_initialize_rx_structure(nic);
|
---|
[1df224c] | 2183 | if (rc != EOK)
|
---|
[bf84871] | 2184 | goto err_irq;
|
---|
[a35b458] | 2185 |
|
---|
[bf84871] | 2186 | nic_address_t e1000_address;
|
---|
[c4be33a] | 2187 | e1000_get_address(e1000, &e1000_address);
|
---|
| 2188 | rc = nic_report_address(nic, &e1000_address);
|
---|
[1df224c] | 2189 | if (rc != EOK)
|
---|
[bf84871] | 2190 | goto err_rx_structure;
|
---|
[a35b458] | 2191 |
|
---|
[bd41ac52] | 2192 | struct timespec period;
|
---|
[bf84871] | 2193 | period.tv_sec = 0;
|
---|
[bd41ac52] | 2194 | period.tv_nsec = USEC2NSEC(E1000_DEFAULT_INTERRUPT_INTERVAL_USEC);
|
---|
[c4be33a] | 2195 | rc = nic_report_poll_mode(nic, NIC_POLL_PERIODIC, &period);
|
---|
[1df224c] | 2196 | if (rc != EOK)
|
---|
[bf84871] | 2197 | goto err_rx_structure;
|
---|
[a35b458] | 2198 |
|
---|
[e86b8f0] | 2199 | rc = ddf_fun_bind(fun);
|
---|
| 2200 | if (rc != EOK)
|
---|
| 2201 | goto err_fun_bind;
|
---|
[a35b458] | 2202 |
|
---|
[e86b8f0] | 2203 | rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
|
---|
| 2204 | if (rc != EOK)
|
---|
| 2205 | goto err_add_to_cat;
|
---|
[a35b458] | 2206 |
|
---|
[bf84871] | 2207 | return EOK;
|
---|
[a35b458] | 2208 |
|
---|
[e86b8f0] | 2209 | err_add_to_cat:
|
---|
| 2210 | ddf_fun_unbind(fun);
|
---|
| 2211 | err_fun_bind:
|
---|
[bf84871] | 2212 | err_rx_structure:
|
---|
[c4be33a] | 2213 | e1000_uninitialize_rx_structure(nic);
|
---|
[bf84871] | 2214 | err_irq:
|
---|
[eadaeae8] | 2215 | unregister_interrupt_handler(dev, irq_handle);
|
---|
[e86b8f0] | 2216 | err_fun_create:
|
---|
| 2217 | ddf_fun_destroy(fun);
|
---|
| 2218 | nic_set_ddf_fun(nic, NULL);
|
---|
[bf84871] | 2219 | err_tx_structure:
|
---|
[c4be33a] | 2220 | e1000_uninitialize_tx_structure(e1000);
|
---|
[bf84871] | 2221 | err_pio:
|
---|
[1df224c] | 2222 | // TODO: e1000_pio_disable(dev);
|
---|
[bf84871] | 2223 | err_destroy:
|
---|
| 2224 | e1000_dev_cleanup(dev);
|
---|
| 2225 | return rc;
|
---|
[1df224c] | 2226 | }
|
---|
[bf84871] | 2227 |
|
---|
[8300c72] | 2228 | /** Quiesce E1000 device.
|
---|
| 2229 | *
|
---|
| 2230 | * @param dev E1000 device.
|
---|
| 2231 | *
|
---|
| 2232 | */
|
---|
| 2233 | errno_t e1000_dev_quiesce(ddf_dev_t *dev)
|
---|
| 2234 | {
|
---|
| 2235 | nic_t *nic = ddf_dev_data_get(dev);
|
---|
| 2236 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
| 2237 | errno_t rc;
|
---|
| 2238 |
|
---|
| 2239 | ddf_msg(LVL_DEBUG, "e1000_dev_quiesce()");
|
---|
| 2240 |
|
---|
| 2241 | e1000_disable_interrupts(e1000);
|
---|
| 2242 | rc = e1000_reset(nic);
|
---|
| 2243 | if (rc != EOK)
|
---|
| 2244 | ddf_msg(LVL_ERROR, "e1000_dev_quiesce failed");
|
---|
| 2245 | return rc;
|
---|
| 2246 | }
|
---|
| 2247 |
|
---|
[bf84871] | 2248 | /** Read 16-bit value from EEPROM of E1000 adapter
|
---|
[1df224c] | 2249 | *
|
---|
| 2250 | * Read using the EERD register.
|
---|
| 2251 | *
|
---|
| 2252 | * @param device E1000 device
|
---|
| 2253 | * @param eeprom_address 8-bit EEPROM address
|
---|
| 2254 | *
|
---|
| 2255 | * @return 16-bit value from EEPROM
|
---|
| 2256 | *
|
---|
[bf84871] | 2257 | */
|
---|
[c4be33a] | 2258 | static uint16_t e1000_eeprom_read(e1000_t *e1000, uint8_t eeprom_address)
|
---|
[bf84871] | 2259 | {
|
---|
[c4be33a] | 2260 | fibril_mutex_lock(&e1000->eeprom_lock);
|
---|
[a35b458] | 2261 |
|
---|
[1df224c] | 2262 | /* Write address and START bit to EERD register */
|
---|
[77c2b02] | 2263 | uint32_t write_data = e1000->info.eerd_start |
|
---|
| 2264 | (((uint32_t) eeprom_address) <<
|
---|
| 2265 | e1000->info.eerd_address_offset);
|
---|
[c4be33a] | 2266 | E1000_REG_WRITE(e1000, E1000_EERD, write_data);
|
---|
[a35b458] | 2267 |
|
---|
[c4be33a] | 2268 | uint32_t eerd = E1000_REG_READ(e1000, E1000_EERD);
|
---|
[77c2b02] | 2269 | while ((eerd & e1000->info.eerd_done) == 0) {
|
---|
[5f97ef44] | 2270 | fibril_usleep(1);
|
---|
[c4be33a] | 2271 | eerd = E1000_REG_READ(e1000, E1000_EERD);
|
---|
[bf84871] | 2272 | }
|
---|
[a35b458] | 2273 |
|
---|
[c4be33a] | 2274 | fibril_mutex_unlock(&e1000->eeprom_lock);
|
---|
[a35b458] | 2275 |
|
---|
[77c2b02] | 2276 | return (uint16_t) (eerd >> e1000->info.eerd_data_offset);
|
---|
[bf84871] | 2277 | }
|
---|
| 2278 |
|
---|
| 2279 | /** Get MAC address of the E1000 adapter
|
---|
| 2280 | *
|
---|
[1df224c] | 2281 | * @param device E1000 device
|
---|
| 2282 | * @param address Place to store the address
|
---|
| 2283 | * @param max_len Maximal addresss length to store
|
---|
| 2284 | *
|
---|
| 2285 | * @return EOK if succeed
|
---|
[cde999a] | 2286 | * @return An error code otherwise
|
---|
[1df224c] | 2287 | *
|
---|
[bf84871] | 2288 | */
|
---|
[b7fd2a0] | 2289 | static errno_t e1000_get_address(e1000_t *e1000, nic_address_t *address)
|
---|
[bf84871] | 2290 | {
|
---|
[c4be33a] | 2291 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
[a35b458] | 2292 |
|
---|
[1df224c] | 2293 | uint8_t *mac0_dest = (uint8_t *) address->address;
|
---|
| 2294 | uint8_t *mac1_dest = (uint8_t *) address->address + 1;
|
---|
| 2295 | uint8_t *mac2_dest = (uint8_t *) address->address + 2;
|
---|
| 2296 | uint8_t *mac3_dest = (uint8_t *) address->address + 3;
|
---|
| 2297 | uint8_t *mac4_dest = (uint8_t *) address->address + 4;
|
---|
| 2298 | uint8_t *mac5_dest = (uint8_t *) address->address + 5;
|
---|
[a35b458] | 2299 |
|
---|
[c4be33a] | 2300 | uint32_t rah = E1000_REG_READ(e1000, E1000_RAH_ARRAY(0));
|
---|
| 2301 | uint32_t ral = E1000_REG_READ(e1000, E1000_RAL_ARRAY(0));
|
---|
[a35b458] | 2302 |
|
---|
[bf84871] | 2303 | *mac0_dest = (uint8_t) ral;
|
---|
| 2304 | *mac1_dest = (uint8_t) (ral >> 8);
|
---|
| 2305 | *mac2_dest = (uint8_t) (ral >> 16);
|
---|
| 2306 | *mac3_dest = (uint8_t) (ral >> 24);
|
---|
| 2307 | *mac4_dest = (uint8_t) rah;
|
---|
| 2308 | *mac5_dest = (uint8_t) (rah >> 8);
|
---|
[a35b458] | 2309 |
|
---|
[c4be33a] | 2310 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
[bf84871] | 2311 | return EOK;
|
---|
[84239b1] | 2312 | }
|
---|
[bf84871] | 2313 |
|
---|
| 2314 | /** Set card MAC address
|
---|
| 2315 | *
|
---|
[1df224c] | 2316 | * @param device E1000 device
|
---|
| 2317 | * @param address Address
|
---|
| 2318 | *
|
---|
| 2319 | * @return EOK if succeed
|
---|
[cde999a] | 2320 | * @return An error code otherwise
|
---|
[bf84871] | 2321 | */
|
---|
[b7fd2a0] | 2322 | static errno_t e1000_set_addr(ddf_fun_t *fun, const nic_address_t *addr)
|
---|
[bf84871] | 2323 | {
|
---|
[56fd7cf] | 2324 | nic_t *nic = NIC_DATA_FUN(fun);
|
---|
[c4be33a] | 2325 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
[a35b458] | 2326 |
|
---|
[c4be33a] | 2327 | fibril_mutex_lock(&e1000->rx_lock);
|
---|
| 2328 | fibril_mutex_lock(&e1000->tx_lock);
|
---|
[a35b458] | 2329 |
|
---|
[b7fd2a0] | 2330 | errno_t rc = nic_report_address(nic, addr);
|
---|
[1df224c] | 2331 | if (rc == EOK)
|
---|
[c4be33a] | 2332 | e1000_write_receive_address(e1000, 0, addr, false);
|
---|
[a35b458] | 2333 |
|
---|
[c4be33a] | 2334 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
| 2335 | fibril_mutex_unlock(&e1000->rx_lock);
|
---|
[a35b458] | 2336 |
|
---|
[bf84871] | 2337 | return rc;
|
---|
| 2338 | }
|
---|
| 2339 |
|
---|
[c4be33a] | 2340 | static void e1000_eeprom_get_address(e1000_t *e1000,
|
---|
[1df224c] | 2341 | nic_address_t *address)
|
---|
[bf84871] | 2342 | {
|
---|
[1df224c] | 2343 | uint16_t *mac0_dest = (uint16_t *) address->address;
|
---|
| 2344 | uint16_t *mac2_dest = (uint16_t *) (address->address + 2);
|
---|
| 2345 | uint16_t *mac4_dest = (uint16_t *) (address->address + 4);
|
---|
[a35b458] | 2346 |
|
---|
[c4be33a] | 2347 | *mac0_dest = e1000_eeprom_read(e1000, 0);
|
---|
| 2348 | *mac2_dest = e1000_eeprom_read(e1000, 1);
|
---|
| 2349 | *mac4_dest = e1000_eeprom_read(e1000, 2);
|
---|
[bf84871] | 2350 | }
|
---|
| 2351 |
|
---|
[6d8455d] | 2352 | /** Send frame
|
---|
[1df224c] | 2353 | *
|
---|
[c4be33a] | 2354 | * @param nic NIC driver data structure
|
---|
[6d8455d] | 2355 | * @param data Frame data
|
---|
| 2356 | * @param size Frame size in bytes
|
---|
[bf84871] | 2357 | *
|
---|
[1df224c] | 2358 | * @return EOK if succeed
|
---|
| 2359 | * @return Error code in the case of error
|
---|
[bf84871] | 2360 | *
|
---|
| 2361 | */
|
---|
[6d8455d] | 2362 | static void e1000_send_frame(nic_t *nic, void *data, size_t size)
|
---|
[bf84871] | 2363 | {
|
---|
[c4be33a] | 2364 | assert(nic);
|
---|
[a35b458] | 2365 |
|
---|
[c4be33a] | 2366 | e1000_t *e1000 = DRIVER_DATA_NIC(nic);
|
---|
| 2367 | fibril_mutex_lock(&e1000->tx_lock);
|
---|
[a35b458] | 2368 |
|
---|
[c4be33a] | 2369 | uint32_t tdt = E1000_REG_READ(e1000, E1000_TDT);
|
---|
[1df224c] | 2370 | e1000_tx_descriptor_t *tx_descriptor_addr = (e1000_tx_descriptor_t *)
|
---|
[c4be33a] | 2371 | (e1000->tx_ring_virt + tdt * sizeof(e1000_tx_descriptor_t));
|
---|
[a35b458] | 2372 |
|
---|
[bf84871] | 2373 | bool descriptor_available = false;
|
---|
[a35b458] | 2374 |
|
---|
[1df224c] | 2375 | /* Descriptor never used */
|
---|
| 2376 | if (tx_descriptor_addr->length == 0)
|
---|
[bf84871] | 2377 | descriptor_available = true;
|
---|
[a35b458] | 2378 |
|
---|
[1df224c] | 2379 | /* Descriptor done */
|
---|
[6d8455d] | 2380 | if (tx_descriptor_addr->status & TXDESCRIPTOR_STATUS_DD)
|
---|
[bf84871] | 2381 | descriptor_available = true;
|
---|
[a35b458] | 2382 |
|
---|
[1df224c] | 2383 | if (!descriptor_available) {
|
---|
[1bc35b5] | 2384 | /* Frame lost */
|
---|
[c4be33a] | 2385 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
[bf84871] | 2386 | return;
|
---|
| 2387 | }
|
---|
[a35b458] | 2388 |
|
---|
[6d8455d] | 2389 | memcpy(e1000->tx_frame_virt[tdt], data, size);
|
---|
[a35b458] | 2390 |
|
---|
[6d8455d] | 2391 | tx_descriptor_addr->phys_addr = PTR_TO_U64(e1000->tx_frame_phys[tdt]);
|
---|
| 2392 | tx_descriptor_addr->length = size;
|
---|
[a35b458] | 2393 |
|
---|
[1df224c] | 2394 | /*
|
---|
| 2395 | * Report status to STATUS.DD (descriptor done),
|
---|
| 2396 | * add ethernet CRC, end of packet.
|
---|
| 2397 | */
|
---|
| 2398 | tx_descriptor_addr->command = TXDESCRIPTOR_COMMAND_RS |
|
---|
| 2399 | TXDESCRIPTOR_COMMAND_IFCS |
|
---|
| 2400 | TXDESCRIPTOR_COMMAND_EOP;
|
---|
[a35b458] | 2401 |
|
---|
[bf84871] | 2402 | tx_descriptor_addr->checksum_offset = 0;
|
---|
| 2403 | tx_descriptor_addr->status = 0;
|
---|
[c4be33a] | 2404 | if (e1000->vlan_tag_add) {
|
---|
| 2405 | tx_descriptor_addr->special = e1000->vlan_tag;
|
---|
[bf84871] | 2406 | tx_descriptor_addr->command |= TXDESCRIPTOR_COMMAND_VLE;
|
---|
[1df224c] | 2407 | } else
|
---|
| 2408 | tx_descriptor_addr->special = 0;
|
---|
[a35b458] | 2409 |
|
---|
[bf84871] | 2410 | tx_descriptor_addr->checksum_start_field = 0;
|
---|
[a35b458] | 2411 |
|
---|
[1df224c] | 2412 | tdt++;
|
---|
[1bc35b5] | 2413 | if (tdt == E1000_TX_FRAME_COUNT)
|
---|
[bf84871] | 2414 | tdt = 0;
|
---|
[a35b458] | 2415 |
|
---|
[c4be33a] | 2416 | E1000_REG_WRITE(e1000, E1000_TDT, tdt);
|
---|
[a35b458] | 2417 |
|
---|
[c4be33a] | 2418 | fibril_mutex_unlock(&e1000->tx_lock);
|
---|
[bf84871] | 2419 | }
|
---|
| 2420 |
|
---|
| 2421 | int main(void)
|
---|
| 2422 | {
|
---|
[869d936] | 2423 | printf("%s: HelenOS E1000 network adapter driver\n", NAME);
|
---|
[a35b458] | 2424 |
|
---|
[869d936] | 2425 | if (nic_driver_init(NAME) != EOK)
|
---|
| 2426 | return 1;
|
---|
[a35b458] | 2427 |
|
---|
[c4be33a] | 2428 | nic_driver_implement(&e1000_driver_ops, &e1000_dev_ops,
|
---|
| 2429 | &e1000_nic_iface);
|
---|
[a35b458] | 2430 |
|
---|
[267f235] | 2431 | ddf_log_init(NAME);
|
---|
[bf84871] | 2432 | return ddf_driver_main(&e1000_driver);
|
---|
| 2433 | }
|
---|