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