Changeset 1df224c in mainline


Ignore:
Timestamp:
2011-12-13T16:31:20Z (12 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b402dadd
Parents:
63bcbbc
Message:

e1k: initial code revision

Location:
uspace/drv/nic/e1k
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/nic/e1k/e1k.c

    r63bcbbc r1df224c  
    2727 */
    2828
    29 /** @file e1000.c
    30  *
    31  *  Driver for Intel Pro/1000 8254x Family of Gigabit Ethernet Controllers
     29/** @file e1k.c
     30 *
     31 * Driver for Intel Pro/1000 8254x Family of Gigabit Ethernet Controllers
     32 *
    3233 */
    3334
     
    3637#include <errno.h>
    3738#include <adt/list.h>
    38 #include <nlog.h>
    3939#include <align.h>
    4040#include <byteorder.h>
     
    4343#include <ipc/ns.h>
    4444#include <libarch/ddi.h>
    45 
    4645#include <as.h>
    47 #include <dma.h>
    4846#include <ddf/interrupt.h>
    4947#include <devman.h>
     
    5654#include <packet_remote.h>
    5755#include <net/packet_header.h>
    58 
    59 #include "e1000_defs.h"
    60 
    61 /// The driver name
    62 #define NAME "e1000"
    63 
    64 #define E1000_DEFAULT_INTERRUPT_INTEVAL_USEC 250
    65 
    66 
    67 // Must be power of 8
    68 #define E1000_RX_PACKETS_COUNT 128
    69 #define E1000_TX_PACKETS_COUNT 128
    70 
    71 #define E1000_RECEIVE_ADDRESS 16
     56#include "e1k.h"
     57
     58#define NAME  "e1k"
     59
     60#define E1000_DEFAULT_INTERRUPT_INTEVAL_USEC  250
     61
     62/* Must be power of 8 */
     63#define E1000_RX_PACKETS_COUNT  128
     64#define E1000_TX_PACKETS_COUNT  128
     65
     66#define E1000_RECEIVE_ADDRESS  16
    7267
    7368/** Maximum receiving packet size */
    74 #define E1000_MAX_RECEIVE_PACKET_SIZE 2048
     69#define E1000_MAX_RECEIVE_PACKET_SIZE  2048
    7570
    7671/** nic_driver_data_t* -> e1000_t* cast */
    77 #define DRIVER_DATA_NIC(nic_data) ((e1000_t*) nic_get_specific(nic_data))
     72#define DRIVER_DATA_NIC(nic_data) \
     73        ((e1000_t *) nic_get_specific(nic_data))
     74
    7875/** device_t* -> nic_driver_data_t* cast */
    79 #define NIC_DATA_DEV(dev) ((nic_t*)((dev)->driver_data))
     76#define NIC_DATA_DEV(dev) \
     77        ((nic_t *) ((dev)->driver_data))
     78
    8079/** device_t* -> e1000_t* cast */
    81 #define DRIVER_DATA_DEV(dev) (DRIVER_DATA_NIC(NIC_DATA_DEV(dev)))
    82 
    83 /** Cast pointer to uint32_t
    84  *
    85  *  @param ptr The pointer to cast
    86  *  @return The uint32_t pointer representation. The low 32 bit is taken
    87  *  in the case of the 64 bit pointers
    88  */
    89 #define PTR_TO_U64(ptr) ((uint64_t)((size_t)(ptr)))
     80#define DRIVER_DATA_DEV(dev) \
     81        (DRIVER_DATA_NIC(NIC_DATA_DEV(dev)))
     82
     83/** Cast pointer to uint64_t
     84 *
     85 * @param ptr Pointer to cast
     86 *
     87 * @return The uint64_t pointer representation.
     88 *
     89 */
     90#define PTR_TO_U64(ptr)  ((uint64_t) ((uintptr_t) (ptr)))
    9091
    9192/** Cast the memaddr part to the void*
    9293 *
    93  *  @param memaddr The memaddr value
    94  */
    95 #define MEMADDR_TO_PTR(memaddr) ((void*)((size_t)(memaddr)))
    96 
    97 #define E1000_REG_BASE(e1000_data) (e1000_data->virt_reg_base)
    98 #define E1000_REG_ADDR(e1000_data, reg) ((uint32_t *)(E1000_REG_BASE(e1000_data) + reg))
    99 #define E1000_REG_READ(e1000_data, reg) (pio_read_32(E1000_REG_ADDR(e1000_data, reg))) 
    100 #define E1000_REG_WRITE(e1000_data, reg, value) (pio_write_32(E1000_REG_ADDR(e1000_data, reg), value))
    101 
     94 * @param memaddr The memaddr value
     95 *
     96 */
     97#define MEMADDR_TO_PTR(memaddr)  ((void *) ((size_t) (memaddr)))
     98
     99#define E1000_REG_BASE(e1000_data) \
     100        ((e1000_data)->virt_reg_base)
     101
     102#define E1000_REG_ADDR(e1000_data, reg) \
     103        ((uint32_t *) (E1000_REG_BASE(e1000_data) + reg))
     104
     105#define E1000_REG_READ(e1000_data, reg) \
     106        (pio_read_32(E1000_REG_ADDR(e1000_data, reg)))
     107
     108#define E1000_REG_WRITE(e1000_data, reg, value) \
     109        (pio_write_32(E1000_REG_ADDR(e1000_data, reg), value))
    102110
    103111/** E1000 device data */
    104112typedef struct e1000_data {
    105113        /** Physical registers base address */
    106         void * phys_reg_base;
     114        void *phys_reg_base;
    107115        /** Virtual registers base address */
    108         void * virt_reg_base;
     116        void *virt_reg_base;
    109117        /** Tx ring */
    110118        dma_mem_t tx_ring;
    111119        /** Packets in tx ring  */
    112         packet_t ** tx_ring_packets;
     120        packet_t **tx_ring_packets;
    113121        /** Rx ring */
    114122        dma_mem_t rx_ring;
    115123        /** Packets in rx ring  */
    116         packet_t ** rx_ring_packets;
     124        packet_t **rx_ring_packets;
    117125        /** VLAN tag */
    118126        uint16_t vlan_tag;
    119         /** add VLAN tag to packet */
    120         int vlan_tag_add;
     127        /** Add VLAN tag to packet */
     128        bool vlan_tag_add;
    121129        /** Used unicast Receive Address count */
    122130        unsigned int unicast_ra_count;
    123         /** Used milticast Receive addrress count */ 
     131        /** Used milticast Receive addrress count */
    124132        unsigned int multicast_ra_count;
    125133        /** PCI device ID */
     
    141149
    142150static int e1000_get_address(e1000_t *, nic_address_t *);
    143 static void e1000_eeprom_get_address(e1000_t *, nic_address_t *address);
    144 static int e1000_set_addr(ddf_fun_t *dev, const nic_address_t *addr);
    145 
    146 static int e1000_defective_get_mode(ddf_fun_t *device, uint32_t *mode);
    147 static int e1000_defective_set_mode(ddf_fun_t *device, uint32_t mode);
    148 
    149 static int e1000_get_cable_state(ddf_fun_t *dev, nic_cable_state_t *state);
    150 static int e1000_get_device_info(ddf_fun_t *dev, nic_device_info_t *info);
    151 static int e1000_get_operation_mode(ddf_fun_t *device, int *speed,
    152     nic_channel_mode_t *duplex, nic_role_t *role);
    153 static int e1000_set_operation_mode(ddf_fun_t *device, int speed,
    154     nic_channel_mode_t duplex, nic_role_t);
    155 static int e1000_autoneg_enable(ddf_fun_t *device, uint32_t advertisement);
    156 static int e1000_autoneg_disable(ddf_fun_t *device);
    157 static int e1000_autoneg_restart(ddf_fun_t *device);
    158 
    159 static int e1000_vlan_set_tag(ddf_fun_t *device, uint16_t tag, int add, int strip);
     151static void e1000_eeprom_get_address(e1000_t *, nic_address_t *);
     152static int e1000_set_addr(ddf_fun_t *, const nic_address_t *);
     153
     154static int e1000_defective_get_mode(ddf_fun_t *, uint32_t *);
     155static int e1000_defective_set_mode(ddf_fun_t *, uint32_t);
     156
     157static int e1000_get_cable_state(ddf_fun_t *, nic_cable_state_t *);
     158static int e1000_get_device_info(ddf_fun_t *, nic_device_info_t *);
     159static int e1000_get_operation_mode(ddf_fun_t *, int *,
     160    nic_channel_mode_t *, nic_role_t *);
     161static int e1000_set_operation_mode(ddf_fun_t *, int,
     162    nic_channel_mode_t, nic_role_t);
     163static int e1000_autoneg_enable(ddf_fun_t *, uint32_t);
     164static int e1000_autoneg_disable(ddf_fun_t *);
     165static int e1000_autoneg_restart(ddf_fun_t *);
     166
     167static int e1000_vlan_set_tag(ddf_fun_t *, uint16_t, bool, bool);
    160168
    161169/** Network interface options for E1000 card driver */
     
    173181        .autoneg_restart = &e1000_autoneg_restart,
    174182        .vlan_set_tag = &e1000_vlan_set_tag,
    175 
    176183        .defective_get_mode = &e1000_defective_get_mode,
    177184        .defective_set_mode = &e1000_defective_set_mode,
    178 
    179185};
    180186
     
    182188static ddf_dev_ops_t e1000_dev_ops;
    183189
    184 static int e1000_add_device(ddf_dev_t *dev);
     190static int e1000_add_device(ddf_dev_t *);
    185191
    186192/** Basic driver operations for E1000 driver */
     
    196202
    197203/* The default implementation callbacks */
    198 static int e1000_on_activating(nic_t *nic_data);
    199 static int e1000_on_stopping(nic_t *nic_data);
    200 static void e1000_write_packet(nic_t *nic_data, packet_t *packet);
     204static int e1000_on_activating(nic_t *);
     205static int e1000_on_stopping(nic_t *);
     206static void e1000_write_packet(nic_t *, packet_t *);
    201207
    202208/** Commands to deal with interrupt
     
    204210 */
    205211irq_cmd_t e1000_irq_commands[] = {
    206                 {
    207                                 /* Get the interrupt status */
    208                                 .cmd = CMD_PIO_READ_32,
    209                                 .addr = NULL,
    210                                 .dstarg = 2
    211                 },
    212                 {
    213                                 .cmd = CMD_PREDICATE,
    214                                 .value = 2,
    215                                 .srcarg = 2
    216                 },
    217                 {
    218                                 /* Disable interrupts until interrupt routine is finished */
    219                                 .cmd = CMD_PIO_WRITE_32,
    220                                 .addr = NULL,
    221                                 .value = 0xFFFFFFFF
    222                 },
    223                 {
    224                                 .cmd = CMD_ACCEPT
    225                 }
     212        {
     213                /* Get the interrupt status */
     214                .cmd = CMD_PIO_READ_32,
     215                .addr = NULL,
     216                .dstarg = 2
     217        },
     218        {
     219                .cmd = CMD_PREDICATE,
     220                .value = 2,
     221                .srcarg = 2
     222        },
     223        {
     224                /* Disable interrupts until interrupt routine is finished */
     225                .cmd = CMD_PIO_WRITE_32,
     226                .addr = NULL,
     227                .value = 0xFFFFFFFF
     228        },
     229        {
     230                .cmd = CMD_ACCEPT
     231        }
    226232};
    227233
    228234/** Interrupt code definition */
    229235irq_code_t e1000_irq_code = {
    230         .cmdcount = sizeof(e1000_irq_commands)/sizeof(irq_cmd_t),
     236        .cmdcount = sizeof(e1000_irq_commands) / sizeof(irq_cmd_t),
    231237        .cmds = e1000_irq_commands
    232238};
     
    234240/** Get the device information
    235241 *
    236  *  @param dev The NIC device
    237  *  @param info The information to fill
    238  *  @return EOK
     242 * @param dev  NIC device
     243 * @param info Information to fill
     244 *
     245 * @return EOK
     246 *
    239247 */
    240248static int e1000_get_device_info(ddf_fun_t *dev, nic_device_info_t *info)
     
    242250        assert(dev);
    243251        assert(info);
    244 
     252       
    245253        bzero(info, sizeof(nic_device_info_t));
    246 
    247         info->vendor_id = 0x8086;
    248         str_cpy(info->vendor_name, NIC_VENDOR_MAX_LENGTH, "Intel Corporation");
    249         str_cpy(info->model_name, NIC_MODEL_MAX_LENGTH, "Intel Pro");
     254       
     255        info->vendor_id = 0x8086;
     256        str_cpy(info->vendor_name, NIC_VENDOR_MAX_LENGTH,
     257            "Intel Corporation");
     258        str_cpy(info->model_name, NIC_MODEL_MAX_LENGTH,
     259            "Intel Pro");
     260       
    250261        info->ethernet_support[ETH_10M] = ETH_10BASE_T;
    251262        info->ethernet_support[ETH_100M] = ETH_100BASE_TX;
    252263        info->ethernet_support[ETH_1000M] = ETH_1000BASE_T;
     264       
    253265        return EOK;
    254266}
     
    256268/** Check the cable state
    257269 *
    258  *  @param[in] dev The device
    259  *  @param[out] state The state to fill
    260  *  @return EOK
     270 * @param[in]  dev   device
     271 * @param[out] state state to fill
     272 *
     273 * @return EOK
     274 *
    261275 */
    262276static int e1000_get_cable_state(ddf_fun_t *dev, nic_cable_state_t *state)
     
    265279        assert(DRIVER_DATA_DEV(dev));
    266280        assert(state);
    267 
     281       
    268282        e1000_t *e1000_data = DRIVER_DATA_DEV(dev);
    269         if (E1000_REG_READ(e1000_data, E1000_STATUS) & (STATUS_LU)) {
     283        if (E1000_REG_READ(e1000_data, E1000_STATUS) & (STATUS_LU))
    270284                *state = NIC_CS_PLUGGED;
    271         } else {
     285        else
    272286                *state = NIC_CS_UNPLUGGED;
    273         }
    274 
     287       
    275288        return EOK;
    276289}
    277290
    278 static uint16_t e1000_calculate_itr_interval_from_usecs(suseconds_t useconds) {
     291static uint16_t e1000_calculate_itr_interval_from_usecs(suseconds_t useconds)
     292{
    279293        return useconds * 4;
    280294}
    281295
    282296/** Get operation mode of the device
     297 *
    283298 */
    284299static int e1000_get_operation_mode(ddf_fun_t *dev, int *speed,
     
    287302        e1000_t *e1000_data = DRIVER_DATA_DEV(dev);
    288303        uint32_t status = E1000_REG_READ(e1000_data, E1000_STATUS);
    289 
    290         if (status & STATUS_FD) {
     304       
     305        if (status & STATUS_FD)
    291306                *duplex = NIC_CM_FULL_DUPLEX;
    292         } else {
     307        else
    293308                *duplex = NIC_CM_HALF_DUPLEX;
    294         }
    295 
    296         uint32_t speed_bits =
    297                 (status >> STATUS_SPEED_SHIFT) & STATUS_SPEED_ALL;
    298 
    299         if (speed_bits == STATUS_SPEED_10) {
     309       
     310        uint32_t speed_bits =
     311            (status >> STATUS_SPEED_SHIFT) & STATUS_SPEED_ALL;
     312       
     313        if (speed_bits == STATUS_SPEED_10)
    300314                *speed = 10;
    301         } else if (speed_bits == STATUS_SPEED_100) {
     315        else if (speed_bits == STATUS_SPEED_100)
    302316                *speed = 100;
    303         } else if ((speed_bits == STATUS_SPEED_1000A)
    304                 || (speed_bits == STATUS_SPEED_1000B)) {
     317        else if ((speed_bits == STATUS_SPEED_1000A) ||
     318            (speed_bits == STATUS_SPEED_1000B))
    305319                *speed = 1000;
    306         }
    307 
     320       
    308321        *role = NIC_ROLE_UNKNOWN;
    309322        return EOK;
    310323}
    311324
    312 static void e1000_link_restart(e1000_t * e1000_data)
    313 {
    314         fibril_mutex_lock(&e1000_data->ctrl_lock);;
     325static void e1000_link_restart(e1000_t * e1000_data)
     326{
     327        fibril_mutex_lock(&e1000_data->ctrl_lock);
     328       
    315329        uint32_t ctrl = E1000_REG_READ(e1000_data, E1000_CTRL);
    316330       
     
    322336                ctrl |= CTRL_SLU;
    323337        }
     338       
    324339        fibril_mutex_unlock(&e1000_data->ctrl_lock);
    325 
     340       
    326341        e1000_link_restart(e1000_data);
    327        
    328342}
    329343
     
    334348    nic_channel_mode_t duplex, nic_role_t role)
    335349{
    336         if (speed != 10 && speed != 100 && speed != 1000)
     350        if ((speed != 10) && (speed != 100) && (speed != 1000))
    337351                return EINVAL;
    338         if (duplex != NIC_CM_HALF_DUPLEX && duplex != NIC_CM_FULL_DUPLEX)
     352       
     353        if ((duplex != NIC_CM_HALF_DUPLEX) && (duplex != NIC_CM_FULL_DUPLEX))
    339354                return EINVAL;
    340 
     355       
    341356        e1000_t *e1000_data = DRIVER_DATA_DEV(dev);
    342         fibril_mutex_lock(&e1000_data->ctrl_lock);;
     357       
     358        fibril_mutex_lock(&e1000_data->ctrl_lock);
    343359        uint32_t ctrl = E1000_REG_READ(e1000_data, E1000_CTRL);
    344 
     360       
    345361        ctrl |= CTRL_FRCSPD;
    346362        ctrl |= CTRL_FRCDPLX;
    347363        ctrl &= ~(CTRL_ASDE);
    348 
    349         if (duplex == NIC_CM_FULL_DUPLEX) {
     364       
     365        if (duplex == NIC_CM_FULL_DUPLEX)
    350366                ctrl |= CTRL_FD;
    351         } else {
     367        else
    352368                ctrl &= ~(CTRL_FD);
    353         }
    354 
    355369       
    356370        ctrl &= ~(CTRL_SPEED_MASK);
    357         if (speed == 1000) {
     371        if (speed == 1000)
    358372                ctrl |= CTRL_SPEED_1000 << CTRL_SPEED_SHIFT;
    359         } else if (speed == 100) {
     373        else if (speed == 100)
    360374                ctrl |= CTRL_SPEED_100 << CTRL_SPEED_SHIFT;
    361         } else {
     375        else
    362376                ctrl |= CTRL_SPEED_10 << CTRL_SPEED_SHIFT;
    363         }
    364 
     377       
    365378        E1000_REG_WRITE(e1000_data, E1000_CTRL, ctrl);
     379       
    366380        fibril_mutex_unlock(&e1000_data->ctrl_lock);
    367 
     381       
    368382        e1000_link_restart(e1000_data);
    369 
     383       
    370384        return EOK;
    371385}
    372386
    373 /** Enable autonegoation
    374  *
    375  *  @param dev The device to update
    376  *  @param advertisement Ignored on E1000
    377  *  @returns EOK if advertisement mode set successfully
     387/** Enable auto-negotiation
     388 *
     389 * @param dev           Device to update
     390 * @param advertisement Ignored on E1000
     391 *
     392 * @return EOK if advertisement mode set successfully
     393 *
    378394 */
    379395static int e1000_autoneg_enable(ddf_fun_t *dev, uint32_t advertisement)
    380396{
    381397        e1000_t *e1000_data = DRIVER_DATA_DEV(dev);
     398       
    382399        fibril_mutex_lock(&e1000_data->ctrl_lock);
     400       
    383401        uint32_t ctrl = E1000_REG_READ(e1000_data, E1000_CTRL);
    384 
     402       
    385403        ctrl &= ~(CTRL_FRCSPD);
    386404        ctrl &= ~(CTRL_FRCDPLX);
    387405        ctrl |= CTRL_ASDE;
    388 
     406       
    389407        E1000_REG_WRITE(e1000_data, E1000_CTRL, ctrl);
     408       
    390409        fibril_mutex_unlock(&e1000_data->ctrl_lock);
    391 
     410       
    392411        e1000_link_restart(e1000_data);
    393 
     412       
    394413        return EOK;
    395414}
    396415
    397 /** Disable autonegoation
    398  *
    399  *  @param dev The device to update
    400  *  @returns EOK
     416/** Disable auto-negotiation
     417 *
     418 * @param dev Device to update
     419 *
     420 * @return EOK
     421 *
    401422 */
    402423static int e1000_autoneg_disable(ddf_fun_t *dev)
    403424{
    404425        e1000_t *e1000_data = DRIVER_DATA_DEV(dev);
     426       
    405427        fibril_mutex_lock(&e1000_data->ctrl_lock);
     428       
    406429        uint32_t ctrl = E1000_REG_READ(e1000_data, E1000_CTRL);
    407 
     430       
    408431        ctrl |= CTRL_FRCSPD;
    409432        ctrl |= CTRL_FRCDPLX;
    410433        ctrl &= ~(CTRL_ASDE);
    411 
     434       
    412435        E1000_REG_WRITE(e1000_data, E1000_CTRL, ctrl);
     436       
    413437        fibril_mutex_unlock(&e1000_data->ctrl_lock);
    414 
     438       
    415439        e1000_link_restart(e1000_data);
    416 
     440       
    417441        return EOK;
    418442}
    419443
    420 /** Restart autonegoation
    421  *
    422  *  @param dev The device to update
    423  *  @returns EOK if advertisement mode set successfully
     444/** Restart auto-negotiation
     445 *
     446 * @param dev Device to update
     447 *
     448 * @return EOK if advertisement mode set successfully
     449 *
    424450 */
    425451static int e1000_autoneg_restart(ddf_fun_t *dev)
     
    428454}
    429455
    430 
    431456/** Get state of acceptance of weird packets
    432457 *
    433  *  @param device The device to check
    434  *  @param [out] mode The current mode
     458 * @param      device Device to check
     459 * @param[out] mode   Current mode
     460 *
    435461 */
    436462static int e1000_defective_get_mode(ddf_fun_t *device, uint32_t *mode)
    437463{
    438464        e1000_t *e1000_data = DRIVER_DATA_DEV(device);
     465       
    439466        *mode = 0;
    440467        uint32_t rctl = E1000_REG_READ(e1000_data, E1000_RCTL);
    441         if (rctl & RCTL_SBP) {
     468        if (rctl & RCTL_SBP)
    442469                *mode = NIC_DEFECTIVE_BAD_CRC | NIC_DEFECTIVE_SHORT;
    443         }
     470       
    444471        return EOK;
    445472};
     
    447474/** Set acceptance of weird packets
    448475 *
    449  *  @param device The device to update
    450  *  @param mode The mode to set
    451  *  @returns ENOTSUP if the mode is not supported
    452  *  @returns EOK of mode was set
     476 * @param device Device to update
     477 * @param mode   Mode to set
     478 *
     479 * @return ENOTSUP if the mode is not supported
     480 * @return EOK of mode was set
     481 *
    453482 */
    454483static int e1000_defective_set_mode(ddf_fun_t *device, uint32_t mode)
    455484{
    456        
    457485        e1000_t *e1000_data = DRIVER_DATA_DEV(device);
     486        int rc = EOK;
     487       
    458488        fibril_mutex_lock(&e1000_data->rx_lock);
    459 
    460         int rc = EOK;
     489       
    461490        uint32_t rctl = E1000_REG_READ(e1000_data, E1000_RCTL);
    462491        bool short_mode = (mode & NIC_DEFECTIVE_SHORT ? true : false);
    463492        bool bad_mode = (mode & NIC_DEFECTIVE_BAD_CRC ? true : false);
    464         if (short_mode && bad_mode) {
     493       
     494        if (short_mode && bad_mode)
    465495                rctl |= RCTL_SBP;
    466         } else if ((!short_mode) && (!bad_mode)) {
     496        else if ((!short_mode) && (!bad_mode))
    467497                rctl &= ~RCTL_SBP;
    468         } else {
     498        else
    469499                rc = ENOTSUP;
    470         }
     500       
    471501        E1000_REG_WRITE(e1000_data, E1000_RCTL, rctl);
     502       
    472503        fibril_mutex_unlock(&e1000_data->rx_lock);
    473504        return rc;
    474505};
    475506
    476 
    477507/** Write receive address to RA registr
    478508 *
    479  *  @param e1000_data The E1000 data structure
    480  *  @param position RA register position
    481  *  @param address Ethernet address
    482  *  @param set_av_bit Set the Addtess Valid bit
     509 * @param e1000_data E1000 data structure
     510 * @param position   RA register position
     511 * @param address    Ethernet address
     512 * @param set_av_bit Set the Addtess Valid bit
     513 *
    483514 */
    484515static void e1000_write_receive_address(e1000_t *e1000_data,
    485         unsigned int position, const nic_address_t * address,
    486         bool set_av_bit)
    487 {
    488         uint8_t *mac0 = (uint8_t *)address->address;
    489         uint8_t *mac1 = (uint8_t *)address->address + 1;
    490         uint8_t *mac2 = (uint8_t *)address->address + 2;
    491         uint8_t *mac3 = (uint8_t *)address->address + 3;
    492         uint8_t *mac4 = (uint8_t *)address->address + 4;
    493         uint8_t *mac5 = (uint8_t *)address->address + 5;
    494 
     516    unsigned int position, const nic_address_t * address,
     517    bool set_av_bit)
     518{
     519        uint8_t *mac0 = (uint8_t *) address->address;
     520        uint8_t *mac1 = (uint8_t *) address->address + 1;
     521        uint8_t *mac2 = (uint8_t *) address->address + 2;
     522        uint8_t *mac3 = (uint8_t *) address->address + 3;
     523        uint8_t *mac4 = (uint8_t *) address->address + 4;
     524        uint8_t *mac5 = (uint8_t *) address->address + 5;
     525       
    495526        uint32_t rah;
    496527        uint32_t ral;
    497 
     528       
    498529        ral = ((*mac3) << 24) | ((*mac2) << 16) | ((*mac1) << 8) | (*mac0);
    499530        rah = ((*mac5) << 8) | ((*mac4));
    500         if (set_av_bit) {
     531       
     532        if (set_av_bit)
    501533                rah |= RAH_AV;
    502         } else {
    503                 rah |= E1000_REG_READ(e1000_data, E1000_RAH_ARRAY(position))
    504                         & RAH_AV;
    505         }
    506 
     534        else
     535                rah |= E1000_REG_READ(e1000_data, E1000_RAH_ARRAY(position)) &
     536                    RAH_AV;
     537       
    507538        E1000_REG_WRITE(e1000_data, E1000_RAH_ARRAY(position), rah);
    508539        E1000_REG_WRITE(e1000_data, E1000_RAL_ARRAY(position), ral);
     
    510541
    511542/** Disable receive address in RA registr
    512  *  Clears Address Valid bit
    513  *
    514  *  @param e1000_data The E1000 data structure
    515  *  @param position RA register position
     543 *
     544 *  Clear Address Valid bit
     545 *
     546 * @param e1000_data E1000 data structure
     547 * @param position   RA register position
     548 *
    516549 */
    517550static void e1000_disable_receive_address(e1000_t *e1000_data,
    518         unsigned int position)
    519 {
    520         uint32_t rah = E1000_REG_READ(e1000_data, E1000_RAH_ARRAY(position));
     551    unsigned int position)
     552{
     553        uint32_t rah =
     554            E1000_REG_READ(e1000_data, E1000_RAH_ARRAY(position));
    521555        rah = rah & ~RAH_AV;
    522556        E1000_REG_WRITE(e1000_data, E1000_RAH_ARRAY(position), rah);
    523557}
    524558
    525 /** Clears all unicast addresses from RA registers
    526  *
    527  *  @param e1000_data The E1000 data structure
     559/** Clear all unicast addresses from RA registers
     560 *
     561 * @param e1000_data E1000 data structure
     562 *
    528563 */
    529564static void e1000_clear_unicast_receive_addresses(e1000_t *e1000_data)
    530565{
    531         unsigned int ra_num;
    532         for(ra_num = 1; ra_num <= e1000_data->unicast_ra_count; ra_num++) {
    533                 e1000_disable_receive_address(e1000_data, ra_num);
    534         }
     566        for (unsigned int ra_num = 1;
     567            ra_num <= e1000_data->unicast_ra_count;
     568            ra_num++)
     569                e1000_disable_receive_address(e1000_data, ra_num);
     570       
    535571        e1000_data->unicast_ra_count = 0;
    536572}
    537573
    538 /** Clears all multicast addresses from RA registers
    539  *
    540  *  @param e1000_data The E1000 data structure
     574/** Clear all multicast addresses from RA registers
     575 *
     576 * @param e1000_data E1000 data structure
     577 *
    541578 */
    542579static void e1000_clear_multicast_receive_addresses(e1000_t *e1000_data)
    543580{
    544         unsigned int first_multicast_ra_num =
    545                 E1000_RECEIVE_ADDRESS - e1000_data->multicast_ra_count;
    546         unsigned int ra_num;
    547         for (ra_num = E1000_RECEIVE_ADDRESS - 1;
    548                 ra_num >= first_multicast_ra_num; ra_num--) {
    549                 e1000_disable_receive_address(e1000_data, ra_num);
    550         }
     581        unsigned int first_multicast_ra_num =
     582            E1000_RECEIVE_ADDRESS - e1000_data->multicast_ra_count;
     583       
     584        for (unsigned int ra_num = E1000_RECEIVE_ADDRESS - 1;
     585            ra_num >= first_multicast_ra_num;
     586            ra_num--)
     587                e1000_disable_receive_address(e1000_data, ra_num);
     588       
    551589        e1000_data->multicast_ra_count = 0;
    552590}
    553591
    554 /**
    555  * Returns receive address filter positions count usable for unicast
    556  *
    557  * @param e1000_data The E1000 data structure
     592/** Return receive address filter positions count usable for unicast
     593 *
     594 * @param e1000_data E1000 data structure
     595 *
    558596 * @return receive address filter positions count usable for unicast
     597 *
    559598 */
    560599static unsigned int get_free_unicast_address_count(e1000_t *e1000_data)
    561600{
    562         return E1000_RECEIVE_ADDRESS - 1 - e1000_data->multicast_ra_count; 
    563 }
    564 
    565 /**
    566  * Returns receive address filter positions count usable for multicast
    567  *
    568  * @param e1000_data The E1000 data structure
     601        return E1000_RECEIVE_ADDRESS - 1 - e1000_data->multicast_ra_count;
     602}
     603
     604/** Return receive address filter positions count usable for multicast
     605 *
     606 * @param e1000_data E1000 data structure
     607 *
    569608 * @return receive address filter positions count usable for multicast
     609 *
    570610 */
    571611static unsigned int get_free_multicast_address_count(e1000_t *e1000_data)
    572612{
    573         return E1000_RECEIVE_ADDRESS - 1 - e1000_data->unicast_ra_count; 
    574 }
    575 
    576 /**
    577  * Writes unicast receive addresses to receive address filter registers
    578  *
    579  * @param e1000_data The E1000 data structure
    580  * @param addr Pointer to address array
    581  * @param addr_cnt Address array count
     613        return E1000_RECEIVE_ADDRESS - 1 - e1000_data->unicast_ra_count;
     614}
     615
     616/** Write unicast receive addresses to receive address filter registers
     617 *
     618 * @param e1000_data E1000 data structure
     619 * @param addr       Pointer to address array
     620 * @param addr_cnt   Address array count
     621 *
    582622 */
    583623static void e1000_add_unicast_receive_addresses(e1000_t *e1000_data,
    584         const nic_address_t * addr, size_t addr_cnt)
     624    const nic_address_t *addr, size_t addr_cnt)
    585625{
    586626        assert(addr_cnt <= get_free_unicast_address_count(e1000_data));
    587         nic_address_t * addr_iterator = (nic_address_t *) addr;
    588         unsigned int ra_num;
    589         // ra_num=0 is primary address
    590         for (ra_num = 1; ra_num <= addr_cnt; ra_num++) {
    591                 e1000_write_receive_address(e1000_data, ra_num, addr_iterator, true);   
     627       
     628        nic_address_t *addr_iterator = (nic_address_t *) addr;
     629       
     630        /* ra_num = 0 is primary address */
     631        for (unsigned int ra_num = 1;
     632            ra_num <= addr_cnt;
     633            ra_num++) {
     634                e1000_write_receive_address(e1000_data, ra_num, addr_iterator, true);
    592635                addr_iterator++;
    593636        }
    594637}
    595638
    596 /**
    597  * Writes multicast receive addresses to receive address filter registers
    598  *
    599  * @param e1000_data The E1000 data structure
    600  * @param addr Pointer to address array
    601  * @param addr_cnt Address array count
     639/** Write multicast receive addresses to receive address filter registers
     640 *
     641 * @param e1000_data E1000 data structure
     642 * @param addr       Pointer to address array
     643 * @param addr_cnt   Address array count
     644 *
    602645 */
    603646static void e1000_add_multicast_receive_addresses(e1000_t *e1000_data,
    604         const nic_address_t * addr, size_t addr_cnt)
    605 {
    606         nic_address_t * addr_iterator = (nic_address_t *) addr;
     647    const nic_address_t *addr, size_t addr_cnt)
     648{
    607649        assert(addr_cnt <= get_free_multicast_address_count(e1000_data));
     650       
     651        nic_address_t *addr_iterator = (nic_address_t *) addr;
     652       
    608653        unsigned int first_multicast_ra_num = E1000_RECEIVE_ADDRESS - addr_cnt;
    609         unsigned int ra_num;
    610         for (
    611                 ra_num = E1000_RECEIVE_ADDRESS - 1;
    612                 ra_num >= first_multicast_ra_num;
    613                 ra_num--
    614         ) {
    615                 e1000_write_receive_address(e1000_data, ra_num, addr_iterator, true);   
     654        for (unsigned int ra_num = E1000_RECEIVE_ADDRESS - 1;
     655            ra_num >= first_multicast_ra_num;
     656            ra_num-- {
     657                e1000_write_receive_address(e1000_data, ra_num, addr_iterator, true);
    616658                addr_iterator++;
    617659        }
    618660}
    619661
    620 /**
    621  * Disables receiving packets for default address
    622  *
    623  * @param e1000_data The E1000 data structure
     662/** Disable receiving packets for default address
     663 *
     664 * @param e1000_data E1000 data structure
     665 *
    624666 */
    625667static void disable_ra0_address_filter(e1000_t *e1000_data)
     
    630672}
    631673
    632 /**
    633  * Enables receiving packets for default address
    634  *
    635  * @param e1000_data The E1000 data structure
     674/** Enable receiving packets for default address
     675 *
     676 * @param e1000_data E1000 data structure
     677 *
    636678 */
    637679static void enable_ra0_address_filter(e1000_t *e1000_data)
     
    642684}
    643685
    644 /**
    645  * Disables unicast promiscuous mode
    646  *
    647  * @param e1000_data The E1000 data structure
     686/** Disable unicast promiscuous mode
     687 *
     688 * @param e1000_data E1000 data structure
     689 *
    648690 */
    649691static void e1000_disable_unicast_promisc(e1000_t *e1000_data)
     
    654696}
    655697
    656 /**
    657  * Enables unicast promiscuous mode
    658  *
    659  * @param e1000_data The E1000 data structure
     698/** Enable unicast promiscuous mode
     699 *
     700 * @param e1000_data E1000 data structure
     701 *
    660702 */
    661703static void e1000_enable_unicast_promisc(e1000_t *e1000_data)
     
    666708}
    667709
    668 /**
    669  * Disables multicast promiscuous mode
    670  *
    671  * @param e1000_data The E1000 data structure
     710/** Disable multicast promiscuous mode
     711 *
     712 * @param e1000_data E1000 data structure
     713 *
    672714 */
    673715static void e1000_disable_multicast_promisc(e1000_t *e1000_data)
     
    678720}
    679721
    680 /**
    681  * Enables multicast promiscuous mode
    682  *
    683  * @param e1000_data The E1000 data structure
     722/** Enable multicast promiscuous mode
     723 *
     724 * @param e1000_data E1000 data structure
     725 *
    684726 */
    685727static void e1000_enable_multicast_promisc(e1000_t *e1000_data)
     
    690732}
    691733
    692 /**
    693  * Enables accepting of broadcast packets
    694  *
    695  * @param e1000_data The E1000 data structure
     734/** Enable accepting of broadcast packets
     735 *
     736 * @param e1000_data E1000 data structure
     737 *
    696738 */
    697739static void e1000_enable_broadcast_accept(e1000_t *e1000_data)
     
    702744}
    703745
    704 /**
    705  * Disables accepting of broadcast packets
    706  *
    707  * @param e1000_data The E1000 data structure
     746/** Disable accepting of broadcast packets
     747 *
     748 * @param e1000_data E1000 data structure
     749 *
    708750 */
    709751static void e1000_disable_broadcast_accept(e1000_t *e1000_data)
     
    714756}
    715757
    716 /**
    717  * Enables VLAN filtering according to VFTA registers
    718  *
    719  * @param e1000_data The E1000 data structure
     758/** Enable VLAN filtering according to VFTA registers
     759 *
     760 * @param e1000_data E1000 data structure
     761 *
    720762 */
    721763static void e1000_enable_vlan_filter(e1000_t *e1000_data)
     
    726768}
    727769
    728 /**
    729  * Disables VLAN filtering
    730  *
    731  * @param e1000_data The E1000 data structure
     770/** Disable VLAN filtering
     771 *
     772 * @param e1000_data E1000 data structure
     773 *
    732774 */
    733775static void e1000_disable_vlan_filter(e1000_t *e1000_data)
     
    738780}
    739781
    740 
    741782/** Set multicast packets acceptance mode
    742783 *
    743  *  @param nic_data The nic device to update
    744  *  @param mode The mode to set
    745  *  @param addr address list - used in mode=NIC_MULTICAST_LIST
    746  *  @param addr_cnt length of address list - used in mode=NIC_MULTICAST_LIST
    747  *
    748  *  @returns EOK
    749  */
    750 static int e1000_on_multicast_mode_change(nic_t *nic_data,
    751         nic_multicast_mode_t mode, const nic_address_t * addr, size_t addr_cnt)
    752 {
     784 * @param nic_data NIC device to update
     785 * @param mode     Mode to set
     786 * @param addr     Address list (used in mode = NIC_MULTICAST_LIST)
     787 * @param addr_cnt Length of address list (used in mode = NIC_MULTICAST_LIST)
     788 *
     789 * @return EOK
     790 *
     791 */
     792static int e1000_on_multicast_mode_change(nic_t *nic_data,
     793    nic_multicast_mode_t mode, const nic_address_t *addr, size_t addr_cnt)
     794{
     795        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
    753796        int rc = EOK;
    754         e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
     797       
    755798        fibril_mutex_lock(&e1000_data->rx_lock);
     799       
    756800        switch (mode) {
    757801        case NIC_MULTICAST_BLOCKED:
     
    763807                e1000_clear_multicast_receive_addresses(e1000_data);
    764808                if (addr_cnt > get_free_multicast_address_count(e1000_data)) {
    765                         //TODO: fill MTA table
    766                         //not neccessary - it only saves some compares in NIC library
     809                        /*
     810                         * Future work: fill MTA table
     811                         * Not strictly neccessary, it only saves some compares
     812                         * in the NIC library.
     813                         */
    767814                        e1000_enable_multicast_promisc(e1000_data);
    768815                        nic_report_hw_filtering(nic_data, -1, 0, -1);
     
    782829                break;
    783830        }
     831       
    784832        fibril_mutex_unlock(&e1000_data->rx_lock);
    785833        return rc;
    786834}
     835
    787836/** Set unicast packets acceptance mode
    788837 *
    789  *  @param nic_data The nic device to update
    790  *  @param mode The mode to set
    791  *  @param addr address list - used in mode=NIC_MULTICAST_LIST
    792  *  @param addr_cnt length of address list - used in mode=NIC_MULTICAST_LIST
    793  *
    794  *  @returns EOK
     838 * @param nic_data NIC device to update
     839 * @param mode     Mode to set
     840 * @param addr     Address list (used in mode = NIC_MULTICAST_LIST)
     841 * @param addr_cnt Length of address list (used in mode = NIC_MULTICAST_LIST)
     842 *
     843 * @return EOK
     844 *
    795845 */
    796846static int e1000_on_unicast_mode_change(nic_t *nic_data,
    797         nic_unicast_mode_t mode, const nic_address_t * addr, size_t addr_cnt)
    798 {
     847    nic_unicast_mode_t mode, const nic_address_t *addr, size_t addr_cnt)
     848{
     849        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
    799850        int rc = EOK;
    800         e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
     851       
    801852        fibril_mutex_lock(&e1000_data->rx_lock);
     853       
    802854        switch (mode) {
    803855        case NIC_UNICAST_BLOCKED:
     
    835887                break;
    836888        }
     889       
    837890        fibril_mutex_unlock(&e1000_data->rx_lock);
    838891        return rc;
     
    841894/** Set broadcast packets acceptance mode
    842895 *
    843  *  @param nic_data The nic device to update
    844  *  @param mode The mode to set
    845  *
    846  *  @returns EOK
     896 * @param nic_data NIC device to update
     897 * @param mode     Mode to set
     898 *
     899 * @return EOK
     900 *
    847901 */
    848902static int e1000_on_broadcast_mode_change(nic_t *nic_data,
    849         nic_broadcast_mode_t mode)
    850 {
     903    nic_broadcast_mode_t mode)
     904{
     905        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
    851906        int rc = EOK;
    852         e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
     907       
    853908        fibril_mutex_lock(&e1000_data->rx_lock);
    854 
     909       
    855910        switch (mode) {
    856911        case NIC_BROADCAST_BLOCKED:
     
    864919                break;
    865920        }
     921       
    866922        fibril_mutex_unlock(&e1000_data->rx_lock);
    867923        return rc;
    868924}
    869925
    870 /**
    871  * Checks if receiving is enabled
    872  *
    873  * @param e1000_data The E1000 data structure
     926/** Check if receiving is enabled
     927 *
     928 * @param e1000_data E1000 data structure
     929 *
    874930 * @return true if receiving is enabled
     931 *
    875932 */
    876933static bool e1000_is_rx_enabled(e1000_t *e1000_data)
    877934{
    878         if (E1000_REG_READ(e1000_data, E1000_RCTL) & (RCTL_EN)) {
     935        if (E1000_REG_READ(e1000_data, E1000_RCTL) & (RCTL_EN))
    879936                return true;
    880         } else {
    881                 return false;
    882         }
    883 }
    884 
    885 /**
    886  * Enables receiving
    887  *
    888  * @param e1000_data The E1000 data structure
     937       
     938        return false;
     939}
     940
     941/** Enable receiving
     942 *
     943 * @param e1000_data E1000 data structure
     944 *
    889945 */
    890946static void e1000_enable_rx(e1000_t *e1000_data)
    891947{
    892         //setting Receive Enable Bit
     948        /* Set Receive Enable Bit */
    893949        E1000_REG_WRITE(e1000_data, E1000_RCTL,
    894                 E1000_REG_READ(e1000_data, E1000_RCTL) | (RCTL_EN));
    895 }
    896 
    897 /**
    898  * Disables receiving
    899  *
    900  * @param e1000_data The E1000 data structure
     950            E1000_REG_READ(e1000_data, E1000_RCTL) | (RCTL_EN));
     951}
     952
     953/** Disable receiving
     954 *
     955 * @param e1000_data E1000 data structure
     956 *
    901957 */
    902958static void e1000_disable_rx(e1000_t *e1000_data)
    903959{
    904         //clearing Receive Enable Bit
     960        /* Clear Receive Enable Bit */
    905961        E1000_REG_WRITE(e1000_data, E1000_RCTL,
    906                 E1000_REG_READ(e1000_data, E1000_RCTL) & ~(RCTL_EN));
    907 }
    908 
     962            E1000_REG_READ(e1000_data, E1000_RCTL) & ~(RCTL_EN));
     963}
    909964
    910965/** Set VLAN mask
    911966 *
    912  *  @param nic_data The nic device to update
    913  *  @param vlan_mask VLAN mask
     967 * @param nic_data  NIC device to update
     968 * @param vlan_mask VLAN mask
     969 *
    914970 */
    915971static void e1000_on_vlan_mask_change(nic_t *nic_data,
    916         const nic_vlan_mask_t * vlan_mask)
     972   const nic_vlan_mask_t *vlan_mask)
    917973{
    918974        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
    919 
     975       
    920976        fibril_mutex_lock(&e1000_data->rx_lock);
     977       
    921978        if (vlan_mask) {
     979                /*
     980                 * Disable receiving, so that packet matching
     981                 * partially written VLAN is not received.
     982                 */
    922983                bool rx_enabled = e1000_is_rx_enabled(e1000_data);
    923                 if (rx_enabled) {
    924                         //Disable receiving, so that packet matching
    925                         //partially written wlan is not received
     984                if (rx_enabled)
    926985                        e1000_disable_rx(e1000_data);
    927                 }
    928                 int i;
    929                 for (i = 0; i < NIC_VLAN_BITMAP_SIZE; i += 4) {
    930                         uint32_t bitmap_part =
    931                                 ((uint32_t) vlan_mask->bitmap[i]) |
    932                                 (((uint32_t) vlan_mask->bitmap[i + 1]) << 8) |
    933                                 (((uint32_t) vlan_mask->bitmap[i + 2]) << 16) |
    934                                 (((uint32_t) vlan_mask->bitmap[i + 3]) << 24);
     986               
     987                for (unsigned int i = 0; i < NIC_VLAN_BITMAP_SIZE; i += 4) {
     988                        uint32_t bitmap_part =
     989                            ((uint32_t) vlan_mask->bitmap[i]) |
     990                            (((uint32_t) vlan_mask->bitmap[i + 1]) << 8) |
     991                            (((uint32_t) vlan_mask->bitmap[i + 2]) << 16) |
     992                            (((uint32_t) vlan_mask->bitmap[i + 3]) << 24);
    935993                        E1000_REG_WRITE(e1000_data, E1000_VFTA_ARRAY(i / 4), bitmap_part);
    936994                }
     995               
    937996                e1000_enable_vlan_filter(e1000_data);
    938                 if (rx_enabled) {
     997                if (rx_enabled)
    939998                        e1000_enable_rx(e1000_data);
    940                 }
    941         } else {
     999        } else
    9421000                e1000_disable_vlan_filter(e1000_data);
    943         }
     1001       
    9441002        fibril_mutex_unlock(&e1000_data->rx_lock);
    945        
    9461003}
    9471004
    9481005/** Set VLAN mask
    9491006 *
    950  *  @param device The E1000 device
    951  *  @param tag VLAN tag
    952  *
    953  *  @return EOK
    954  *  @return ENOTSUP
    955  */
    956 static int e1000_vlan_set_tag(ddf_fun_t *device, uint16_t tag, int add,
    957         int strip)
    958 {
    959 
    960         //VLAN CFI bit cannot be set
    961         if (tag & VLANTAG_CFI) {
     1007 * @param device E1000 device
     1008 * @param tag    VLAN tag
     1009 *
     1010 * @return EOK
     1011 * @return ENOTSUP
     1012 *
     1013 */
     1014static int e1000_vlan_set_tag(ddf_fun_t *device, uint16_t tag, bool add,
     1015    bool strip)
     1016{
     1017        /* VLAN CFI bit cannot be set */
     1018        if (tag & VLANTAG_CFI)
    9621019                return ENOTSUP;
    963         }
    964         if (!strip && add) {
    965                 //CTRL.VME is neccessary for both strip and add
    966                 //but CTRL.VME means stripping tags on receive
     1020       
     1021        /*
     1022         * CTRL.VME is neccessary for both strip and add
     1023         * but CTRL.VME means stripping tags on receive.
     1024         */
     1025        if (!strip && add)
    9671026                return ENOTSUP;
    968         }
    9691027       
    9701028        e1000_t *e1000_data = DRIVER_DATA_DEV(device);
     1029       
    9711030        e1000_data->vlan_tag = tag;
    9721031        e1000_data->vlan_tag_add = add;
    973 
     1032       
    9741033        fibril_mutex_lock(&e1000_data->ctrl_lock);
     1034       
    9751035        uint32_t ctrl = E1000_REG_READ(e1000_data, E1000_CTRL);
    976         if (strip) {
     1036        if (strip)
    9771037                ctrl |= CTRL_VME;
    978         } else {
     1038        else
    9791039                ctrl &= ~CTRL_VME;
    980         }
     1040       
    9811041        E1000_REG_WRITE(e1000_data, E1000_CTRL, ctrl);
     1042       
    9821043        fibril_mutex_unlock(&e1000_data->ctrl_lock);
    983 
    9841044        return EOK;
    9851045}
    986        
     1046
    9871047/** Fill receive descriptor with new empty packet
    988  * stores packet in e1000_data->rx_ring_packets
    989  *
    990  *  @param nic_data NIC data stricture
    991  *  @param offset Receive descriptor offset
     1048 *
     1049 * Store packet in e1000_data->rx_ring_packets
     1050 *
     1051 * @param nic_data NIC data stricture
     1052 * @param offset   Receive descriptor offset
     1053 *
    9921054 */
    9931055static void e1000_fill_new_rx_descriptor(nic_t *nic_data, unsigned int offset)
    9941056{
    9951057        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
    996 
    9971058        packet_t *packet = nic_alloc_packet(nic_data, E1000_MAX_RECEIVE_PACKET_SIZE);
    998 
     1059       
    9991060        assert(packet);
    1000 
     1061       
    10011062        *(e1000_data->rx_ring_packets + offset) = packet;
    10021063        e1000_rx_descriptor_t * rx_descriptor = (e1000_rx_descriptor_t *)
    1003                 (e1000_data->rx_ring.virtual +
    1004                 offset * sizeof(e1000_rx_descriptor_t));
    1005 
    1006         void * phys_addr = nic_dma_lock_packet(packet);
    1007 
     1064            (e1000_data->rx_ring.virtual +
     1065            offset * sizeof(e1000_rx_descriptor_t));
     1066       
     1067        void *phys_addr = nic_dma_lock_packet(packet);
     1068       
    10081069        if (phys_addr) {
    1009                 rx_descriptor->phys_addr = PTR_TO_U64(phys_addr +
    1010                         packet->data_start);
    1011         } else {
    1012                 rx_descriptor->phys_addr = 0; 
    1013         }
     1070                rx_descriptor->phys_addr =
     1071                    PTR_TO_U64(phys_addr + packet->data_start);
     1072        } else
     1073                rx_descriptor->phys_addr = 0;
     1074       
    10141075        rx_descriptor->length = 0;
    10151076        rx_descriptor->checksum = 0;
     
    10171078        rx_descriptor->errors = 0;
    10181079        rx_descriptor->special = 0;
    1019 
    10201080}
    10211081
    10221082/** Clear receive descriptor
    10231083 *
    1024  *  @param e1000_data E1000 data
    1025  *  @param offset Receive descriptor offset
    1026  */
    1027 static void e1000_clear_rx_descriptor(e1000_t *e1000_data, unsigned int offset) {
    1028 
    1029         e1000_rx_descriptor_t * rx_descriptor = (e1000_rx_descriptor_t *)
    1030                 (e1000_data->rx_ring.virtual +
    1031                 offset * sizeof(e1000_rx_descriptor_t));
    1032                
     1084 * @param e1000_data E1000 data
     1085 * @param offset     Receive descriptor offset
     1086 *
     1087 */
     1088static void e1000_clear_rx_descriptor(e1000_t *e1000_data, unsigned int offset)
     1089{
     1090        e1000_rx_descriptor_t *rx_descriptor = (e1000_rx_descriptor_t *)
     1091            (e1000_data->rx_ring.virtual +
     1092            offset * sizeof(e1000_rx_descriptor_t));
     1093       
    10331094        rx_descriptor->length = 0;
    10341095        rx_descriptor->checksum = 0;
     
    10401101/** Clear receive descriptor
    10411102 *
    1042  *  @param nic_data NIC data
    1043  *  @param offset Receive descriptor offset
     1103 * @param nic_data NIC data
     1104 * @param offset   Receive descriptor offset
     1105 *
    10441106 */
    10451107static void e1000_clear_tx_descriptor(nic_t *nic_data, unsigned int offset)
    10461108{
    10471109        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
    1048 
     1110       
    10491111        e1000_tx_descriptor_t * tx_descriptor = (e1000_tx_descriptor_t *)
    1050                 (e1000_data->tx_ring.virtual +
    1051                 offset * sizeof(e1000_tx_descriptor_t));
     1112            (e1000_data->tx_ring.virtual +
     1113            offset * sizeof(e1000_tx_descriptor_t));
    10521114       
    10531115        if (tx_descriptor->length) {
    10541116                packet_t * old_packet = *(e1000_data->tx_ring_packets + offset);
    1055                 if (old_packet) {
     1117                if (old_packet)
    10561118                        nic_release_packet(nic_data, old_packet);
    1057                 }
    10581119        }
     1120       
    10591121        tx_descriptor->phys_addr = 0;
    10601122        tx_descriptor->length = 0;
     
    10681130/** Increment tail pointer for receive or transmit ring
    10691131 *
    1070  *  @param tail old Tail
    1071  *  @param descriptors_count ring length
    1072  *
    1073  *  @return new tail
     1132 * @param tail              Old Tail
     1133 * @param descriptors_count Ring length
     1134 *
     1135 * @return New tail
     1136 *
    10741137 */
    10751138static uint32_t e1000_inc_tail(uint32_t tail, uint32_t descriptors_count)
    10761139{
    1077         if (tail + 1 == descriptors_count) {
     1140        if (tail + 1 == descriptors_count)
    10781141                return 0;
    1079         } else {
     1142        else
    10801143                return tail + 1;
    1081         }
    10821144}
    10831145
    10841146/** Receive packets
    1085  * 
    1086  *  @param nic_data The NIC data
    1087  */
    1088 
     1147 *
     1148 * @param nic_data NIC data
     1149 *
     1150 */
    10891151static void e1000_receive_packets(nic_t *nic_data)
    10901152{
    10911153        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
    1092 
     1154       
    10931155        fibril_mutex_lock(&e1000_data->rx_lock);
    10941156       
    1095         uint32_t * tail_addr = E1000_REG_ADDR(e1000_data, E1000_RDT);
    1096         uint32_t next_tail;
    1097         next_tail = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT);
    1098         e1000_rx_descriptor_t * rx_descriptor = (e1000_rx_descriptor_t *)
    1099                         (e1000_data->rx_ring.virtual +
    1100                         next_tail * sizeof(e1000_rx_descriptor_t));
    1101         while (rx_descriptor->status & 0x1) {
     1157        uint32_t *tail_addr = E1000_REG_ADDR(e1000_data, E1000_RDT);
     1158        uint32_t next_tail = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT);
     1159       
     1160        e1000_rx_descriptor_t *rx_descriptor = (e1000_rx_descriptor_t *)
     1161            (e1000_data->rx_ring.virtual +
     1162            next_tail * sizeof(e1000_rx_descriptor_t));
     1163       
     1164        while (rx_descriptor->status & 0x01) {
    11021165                uint32_t packet_size = rx_descriptor->length - E1000_CRC_SIZE;
    1103        
    1104                 packet_t * packet = *(e1000_data->rx_ring_packets + next_tail);
     1166               
     1167                packet_t *packet = *(e1000_data->rx_ring_packets + next_tail);
    11051168                packet_suffix(packet, packet_size);
    1106                        
     1169               
    11071170                nic_dma_unlock_packet(packet);
    11081171                nic_received_packet(nic_data, packet);
    1109 
     1172               
    11101173                e1000_fill_new_rx_descriptor(nic_data, next_tail);
    1111 
    1112                 *tail_addr = e1000_inc_tail(* tail_addr, E1000_RX_PACKETS_COUNT);
     1174               
     1175                *tail_addr = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT);
    11131176                next_tail = e1000_inc_tail(*tail_addr, E1000_RX_PACKETS_COUNT);
    1114 
    1115                 rx_descriptor = (e1000_rx_descriptor_t *) 
    1116                         (e1000_data->rx_ring.virtual +
    1117                         next_tail * sizeof(e1000_rx_descriptor_t));
     1177               
     1178                rx_descriptor = (e1000_rx_descriptor_t *)
     1179                    (e1000_data->rx_ring.virtual +
     1180                    next_tail * sizeof(e1000_rx_descriptor_t));
    11181181        }
    11191182       
     
    11211184}
    11221185
    1123 /**
    1124  * Enable E1000 interupts
    1125  *
    1126  * @param e1000_data The E1000 data structure
    1127  */
    1128 static void e1000_enable_interrupts(e1000_t * e1000_data)
     1186/** Enable E1000 interupts
     1187 *
     1188 * @param e1000_data E1000 data structure
     1189 *
     1190 */
     1191static void e1000_enable_interrupts(e1000_t *e1000_data)
    11291192{
    11301193        E1000_REG_WRITE(e1000_data, E1000_IMS, ICR_RXT0);
    11311194}
    11321195
    1133 /**
    1134  * Disable E1000 interupts
    1135  *
    1136  * @param e1000_data The E1000 data structure
    1137  */
    1138 static void e1000_disable_interrupts(e1000_t * e1000_data)
     1196/** Disable E1000 interupts
     1197 *
     1198 * @param e1000_data E1000 data structure
     1199 *
     1200 */
     1201static void e1000_disable_interrupts(e1000_t *e1000_data)
    11391202{
    11401203        E1000_REG_WRITE(e1000_data, E1000_IMS, 0);
     
    11421205
    11431206/** Interrupt handler implementation
    1144  *  It is called from e1000_interrupt_handler() and e1000_poll()
    1145  *
    1146  *  @param nic_data The NIC data
    1147  *  @param icr ICR register value
    1148  */
    1149 static void e1000_interrupt_handler_impl(nic_t * nic_data, uint32_t icr) {
    1150         if (icr & ICR_RXT0) {
     1207 *
     1208 * This function is called from e1000_interrupt_handler()
     1209 * and e1000_poll()
     1210 *
     1211 * @param nic_data NIC data
     1212 * @param icr      ICR register value
     1213 *
     1214 */
     1215static void e1000_interrupt_handler_impl(nic_t *nic_data, uint32_t icr)
     1216{
     1217        if (icr & ICR_RXT0)
    11511218                e1000_receive_packets(nic_data);
    1152         }
    11531219}
    11541220
    11551221/** Handle device interrupt
    11561222 *
    1157  *  @param dev The e1000 device
    1158  *  @param iid The IPC call id
    1159  *  @param icall The IPC call structure
     1223 * @param dev   E1000 device
     1224 * @param iid   IPC call id
     1225 * @param icall IPC call structure
     1226 *
    11601227 */
    11611228static void e1000_interrupt_handler(ddf_dev_t *dev, ipc_callid_t iid,
     
    11681235        e1000_interrupt_handler_impl(nic_data, icr);
    11691236        e1000_enable_interrupts(e1000_data);
    1170 };
     1237}
    11711238
    11721239/** Register interrupt handler for the card in the system
    11731240 *
    1174  *  Note: the global irq_reg_mutex is locked because of work with global
    1175  *  structure.
    1176  *
    1177  *  @param nic_data The driver data
    1178  *  @return EOK if the handler was registered, negative error code otherwise
     1241 * Note: The global irq_reg_mutex is locked because of work with global
     1242 * structure.
     1243 *
     1244 * @param nic_data Driver data
     1245 *
     1246 * @return EOK if the handler was registered
     1247 * @return Negative error code otherwise
     1248 *
    11791249 */
    11801250inline static int e1000_register_int_handler(nic_t *nic_data)
    11811251{
    11821252        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
    1183 
     1253       
    11841254        /* Lock the mutex in whole driver while working with global structure */
    11851255        fibril_mutex_lock(&irq_reg_mutex);
    1186 
    1187         /* TODO remove this dirty hack after accessing memmory mapped registers
    1188          * from interrupt pseudeocode is fixed
    1189          *
    1190          * dirty hack allowing accessing E1000 registers from interrupt
    1191          * handler
    1192          * 0xffff800000000000 is PA2KA mapping on amd64
    1193          * on ia32 adding it does nothing
    1194          * see also
    1195          * kernel/arch/amd64/src/mm/page.c
    1196          * kernel/arch/ia32/src/mm/page.c
    1197          * */
    1198         e1000_irq_code.cmds[0].addr = 0xffff800000000000 +
    1199                 e1000_data->phys_reg_base + E1000_ICR;
    1200         e1000_irq_code.cmds[2].addr = 0xffff800000000000 +
    1201                 e1000_data->phys_reg_base + E1000_IMC;
    1202         /* End of dirty hack */
    1203 
    1204         int rc = register_interrupt_handler(
    1205                 nic_get_ddf_dev(nic_data),
    1206                 e1000_data->irq,
    1207                 e1000_interrupt_handler,
    1208                 &e1000_irq_code
    1209         );
    1210 
     1256       
     1257        // FIXME: This is not supported in mainline
     1258        e1000_irq_code.cmds[0].addr =
     1259            0xffff800000000000 + e1000_data->phys_reg_base + E1000_ICR;
     1260        e1000_irq_code.cmds[2].addr =
     1261            0xffff800000000000 + e1000_data->phys_reg_base + E1000_IMC;
     1262       
     1263        int rc = register_interrupt_handler(nic_get_ddf_dev(nic_data),
     1264            e1000_data->irq, e1000_interrupt_handler, &e1000_irq_code);
     1265       
    12111266        fibril_mutex_unlock(&irq_reg_mutex);
    1212 
    12131267        return rc;
    12141268}
     
    12161270/** Force receiving all packets in the receive buffer
    12171271 *
    1218  *  @param nic_data  The NIC data
     1272 * @param nic_data NIC data
     1273 *
    12191274 */
    12201275static void e1000_poll(nic_t *nic_data)
    12211276{
    12221277        assert(nic_data);
     1278       
    12231279        e1000_t *e1000_data = nic_get_specific(nic_data);
    12241280        assert(e1000_data);
    1225 
     1281       
    12261282        uint32_t icr = E1000_REG_READ(e1000_data, E1000_ICR);
    12271283        e1000_interrupt_handler_impl(nic_data, icr);
     
    12301286/** Calculates ITR register interrupt from timeval structure
    12311287 *
    1232  *  @param period Period
    1233  */
    1234 static uint16_t e1000_calculate_itr_interval(const struct timeval *period) {
    1235         //TODO use also tv_sec
     1288 * @param period Period
     1289 *
     1290 */
     1291static uint16_t e1000_calculate_itr_interval(const struct timeval *period)
     1292{
     1293        // TODO: use also tv_sec
    12361294        return e1000_calculate_itr_interval_from_usecs(period->tv_usec);
    12371295}
     
    12391297/** Set polling mode
    12401298 *
    1241  *  @param device  The device to set
    1242  *  @param mode    The mode to set
    1243  *  @param period  The period for NIC_POLL_PERIODIC
    1244  *
    1245  *  @returns EOK if succeed
    1246  *  @returns ENOTSUP if the mode is not supported
     1299 * @param device  Device to set
     1300 * @param mode    Mode to set
     1301 * @param period  Period for NIC_POLL_PERIODIC
     1302 *
     1303 * @return EOK if succeed
     1304 * @return ENOTSUP if the mode is not supported
     1305 *
    12471306 */
    12481307static int e1000_poll_mode_change(nic_t *nic_data, nic_poll_mode_t mode,
     
    12501309{
    12511310        assert(nic_data);
    1252 
     1311       
    12531312        e1000_t *e1000_data = nic_get_specific(nic_data);
    12541313        assert(e1000_data);
    12551314       
    1256         switch(mode) {
     1315        switch (mode) {
    12571316        case NIC_POLL_IMMEDIATE:
    12581317                E1000_REG_WRITE(e1000_data, E1000_ITR, 0);
     
    12711330                return ENOTSUP;
    12721331        }
    1273 
     1332       
    12741333        return EOK;
    12751334}
    12761335
    1277 /**
    1278  * Initialize receive registers
    1279  *
    1280  * @param e1000_data The E1000 data structure
     1336/** Initialize receive registers
     1337 *
     1338 * @param e1000_data E1000 data structure
     1339 *
    12811340 */
    12821341static void e1000_initialize_rx_registers(e1000_t * e1000_data)
     
    12841343        E1000_REG_WRITE(e1000_data, E1000_RDLEN, E1000_RX_PACKETS_COUNT * 16);
    12851344        E1000_REG_WRITE(e1000_data, E1000_RDH, 0);
    1286         //It is not posible to let HW use all descriptors
     1345       
     1346        /* It is not posible to let HW use all descriptors */
    12871347        E1000_REG_WRITE(e1000_data, E1000_RDT, E1000_RX_PACKETS_COUNT - 1);
    12881348       
    1289         //set broadcast enable bit
     1349        /* Set Broadcast Enable Bit */
    12901350        E1000_REG_WRITE(e1000_data, E1000_RCTL, RCTL_BAM);
    12911351}
    12921352
    1293 /** Initializes receive structure
    1294  *
    1295  * @param nic_data The NIC data
    1296  * @return EOK if succeed, negative error code otherwise
     1353/** Initialize receive structure
     1354 *
     1355 * @param nic_data NIC data
     1356 *
     1357 * @return EOK if succeed
     1358 * @return Negative error code otherwise
     1359 *
    12971360 */
    12981361static int e1000_initialize_rx_structure(nic_t *nic_data)
     
    13001363        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
    13011364        fibril_mutex_lock(&e1000_data->rx_lock);
    1302 
    1303         e1000_data->rx_ring.size = ALIGN_UP(E1000_RX_PACKETS_COUNT *
    1304                 sizeof(e1000_rx_descriptor_t), PAGE_SIZE) / PAGE_SIZE;
     1365       
     1366        e1000_data->rx_ring.size =
     1367            ALIGN_UP(E1000_RX_PACKETS_COUNT * sizeof(e1000_rx_descriptor_t),
     1368            PAGE_SIZE) / PAGE_SIZE;
    13051369        e1000_data->rx_ring.mapping_flags = AS_AREA_READ | AS_AREA_WRITE;
     1370       
    13061371        int rc = dma_allocate_anonymous(&e1000_data->rx_ring, 0);
    1307         if( rc != EOK ) {
    1308                 nlog_error("Can not allocate rx ring.");
     1372        if (rc != EOK)
    13091373                return rc;
    1310         }
    1311  
     1374       
    13121375        E1000_REG_WRITE(e1000_data, E1000_RDBAH,
    1313                 (uint32_t) (PTR_TO_U64(e1000_data->rx_ring.physical) >> 32));
     1376            (uint32_t) (PTR_TO_U64(e1000_data->rx_ring.physical) >> 32));
    13141377        E1000_REG_WRITE(e1000_data, E1000_RDBAL,
    1315                 (uint32_t) PTR_TO_U64(e1000_data->rx_ring.physical));
    1316        
    1317         e1000_data->rx_ring_packets =
    1318                 malloc(E1000_RX_PACKETS_COUNT * sizeof(packet_t *));
    1319 
    1320         //write descriptor
    1321         unsigned int offset;
    1322         for (offset = 0; offset < E1000_RX_PACKETS_COUNT; offset++) {
    1323                 e1000_fill_new_rx_descriptor(nic_data, offset);
    1324         }
    1325 
     1378            (uint32_t) PTR_TO_U64(e1000_data->rx_ring.physical));
     1379       
     1380        e1000_data->rx_ring_packets =
     1381            malloc(E1000_RX_PACKETS_COUNT * sizeof(packet_t *));
     1382        // FIXME: Check return value
     1383       
     1384        /* Write descriptor */
     1385        for (unsigned int offset = 0;
     1386            offset < E1000_RX_PACKETS_COUNT;
     1387            offset++)
     1388                e1000_fill_new_rx_descriptor(nic_data, offset);
     1389       
    13261390        e1000_initialize_rx_registers(e1000_data);
    1327 
     1391       
    13281392        fibril_mutex_unlock(&e1000_data->rx_lock);
    1329        
    13301393        return EOK;
    13311394}
    13321395
    1333 /** Uninitializes receive structure
    1334  *
    1335  * @param nic_data The NIC data
     1396/** Uninitialize receive structure
     1397 *
     1398 * @param nic_data NIC data
     1399 *
    13361400 */
    13371401static void e1000_uninitialize_rx_structure(nic_t *nic_data)
    13381402{
    13391403        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
    1340 
    1341         //write descriptor
    1342         unsigned int offset;
    1343         for (offset = 0; offset < E1000_RX_PACKETS_COUNT; offset++) {
    1344                 packet_t * packet = *(e1000_data->rx_ring_packets + offset);
     1404       
     1405        /* Write descriptor */
     1406        for (unsigned int offset = 0;
     1407            offset < E1000_RX_PACKETS_COUNT;
     1408            offset++) {
     1409                packet_t *packet = *(e1000_data->rx_ring_packets + offset);
    13451410                nic_dma_unlock_packet(packet);
    13461411                nic_release_packet(nic_data, packet);
    13471412        }
    1348 
     1413       
    13491414        free(e1000_data->rx_ring_packets);
    1350        
    13511415        dma_free(&e1000_data->rx_ring);
    13521416}
     
    13541418/** Clear receive descriptor ring
    13551419 *
    1356  * @param e1000_data The E1000 data
     1420 * @param e1000_data E1000 data
     1421 *
    13571422 */
    13581423static void e1000_clear_rx_ring(e1000_t * e1000_data)
    13591424{
    1360         //write descriptor
    1361         unsigned int offset;
    1362         for (offset = 0; offset < E1000_RX_PACKETS_COUNT; offset++) {
    1363                 e1000_clear_rx_descriptor(e1000_data, offset); 
    1364         }
     1425        /* Write descriptor */
     1426        for (unsigned int offset = 0;
     1427            offset < E1000_RX_PACKETS_COUNT;
     1428            offset++)
     1429                e1000_clear_rx_descriptor(e1000_data, offset);
    13651430}
    13661431
    13671432/** Initialize filters
    13681433 *
    1369  * @param e1000_data The E1000 data
     1434 * @param e1000_data E1000 data
     1435 *
    13701436 */
    13711437static void e1000_initialize_filters(e1000_t *e1000_data)
    13721438{
    1373         //initialize address filter
     1439        /* Initialize address filter */
    13741440        e1000_data->unicast_ra_count = 0;
    13751441        e1000_data->multicast_ra_count = 0;
     
    13791445/** Initialize VLAN
    13801446 *
    1381  * @param e1000_data The E1000 data
     1447 * @param e1000_data E1000 data
     1448 *
    13821449 */
    13831450static void e1000_initialize_vlan(e1000_t *e1000_data)
     
    13861453}
    13871454
    1388 /** Fill mac address from EEPROM to RA[0] register
    1389  *
    1390  * @param e1000_data The E1000 data
     1455/** Fill MAC address from EEPROM to RA[0] register
     1456 *
     1457 * @param e1000_data E1000 data
     1458 *
    13911459 */
    13921460static void e1000_fill_mac_from_eeprom(e1000_t *e1000_data)
    13931461{
    1394         //mac address from eeprom to RA[0]
     1462        /* MAC address from eeprom to RA[0] */
    13951463        nic_address_t address;
    13961464        e1000_eeprom_get_address(e1000_data, &address);
     
    13981466}
    13991467
    1400 /** Initializes other registers
    1401  *
    1402  * @param dev   The E1000 data.
    1403  * @return EOK if succeed, negative error code otherwise
    1404  */
    1405 static void e1000_initialize_registers(e1000_t * e1000_data)
     1468/** Initialize other registers
     1469 *
     1470 * @param dev E1000 data.
     1471 *
     1472 * @return EOK if succeed
     1473 * @return Negative error code otherwise
     1474 *
     1475 */
     1476static void e1000_initialize_registers(e1000_t *e1000_data)
    14061477{
    14071478        E1000_REG_WRITE(e1000_data, E1000_ITR,
    1408                 e1000_calculate_itr_interval_from_usecs(
    1409                         E1000_DEFAULT_INTERRUPT_INTEVAL_USEC));
     1479            e1000_calculate_itr_interval_from_usecs(
     1480            E1000_DEFAULT_INTERRUPT_INTEVAL_USEC));
    14101481        E1000_REG_WRITE(e1000_data, E1000_FCAH, 0);
    14111482        E1000_REG_WRITE(e1000_data, E1000_FCAL, 0);
     
    14131484        E1000_REG_WRITE(e1000_data, E1000_FCTTV, 0);
    14141485        E1000_REG_WRITE(e1000_data, E1000_VET, VET_VALUE);
    1415         E1000_REG_WRITE(e1000_data, E1000_CTRL, CTRL_ASDE); 
    1416 }
    1417 
    1418 /** Initializes transmit registers
    1419  *
    1420  * @param e1000_data The E1000 data.
    1421  */
    1422 static void e1000_initialize_tx_registers(e1000_t * e1000_data)
    1423 {
    1424        
     1486        E1000_REG_WRITE(e1000_data, E1000_CTRL, CTRL_ASDE);
     1487}
     1488
     1489/** Initialize transmit registers
     1490 *
     1491 * @param e1000_data E1000 data.
     1492 *
     1493 */
     1494static void e1000_initialize_tx_registers(e1000_t *e1000_data)
     1495{
    14251496        E1000_REG_WRITE(e1000_data, E1000_TDLEN, E1000_TX_PACKETS_COUNT * 16);
    14261497        E1000_REG_WRITE(e1000_data, E1000_TDH, 0);
     
    14281499       
    14291500        E1000_REG_WRITE(e1000_data, E1000_TIPG,
    1430                 10 << TIPG_IPGT_SHIFT |
    1431                 8 << TIPG_IPGR1_SHIFT |
    1432                 6 << TIPG_IPGR2_SHIFT
    1433         );
     1501            10 << TIPG_IPGT_SHIFT |
     1502            8 << TIPG_IPGR1_SHIFT |
     1503            6 << TIPG_IPGR2_SHIFT);
     1504       
    14341505        E1000_REG_WRITE(e1000_data, E1000_TCTL,
    1435                 //Collision Threshold
    1436                 0x0F << TCTL_CT_SHIFT |
    1437                 //Collision DISTANCE
    1438                 0x40 << TCTL_COLD_SHIFT |
    1439                 //Pad Short Packets
    1440                 TCTL_PSP
    1441         );
    1442        
     1506            0x0F << TCTL_CT_SHIFT /* Collision Threshold */ |
     1507            0x40 << TCTL_COLD_SHIFT /* Collision Distance */ |
     1508            TCTL_PSP /* Pad Short Packets */);
    14431509}
    14441510
    14451511/** Initialize transmit structure
    14461512 *
    1447  * @param e1000_data The E1000 data.
    1448  */
    1449 static int e1000_initialize_tx_structure(e1000_t * e1000_data)
    1450 {
    1451 
     1513 * @param e1000_data E1000 data.
     1514 *
     1515 */
     1516static int e1000_initialize_tx_structure(e1000_t *e1000_data)
     1517{
    14521518        fibril_mutex_lock(&e1000_data->tx_lock);
    1453 
    1454         e1000_data->tx_ring.size = ALIGN_UP(E1000_TX_PACKETS_COUNT *
    1455                 sizeof(e1000_tx_descriptor_t), PAGE_SIZE) / PAGE_SIZE;
     1519       
     1520        e1000_data->tx_ring.size =
     1521            ALIGN_UP(E1000_TX_PACKETS_COUNT * sizeof(e1000_tx_descriptor_t),
     1522            PAGE_SIZE) / PAGE_SIZE;
    14561523        e1000_data->tx_ring.mapping_flags = AS_AREA_READ | AS_AREA_WRITE;
     1524       
    14571525        int rc = dma_allocate_anonymous(&e1000_data->tx_ring, 0);
    1458         if( rc != EOK ) {
    1459                 nlog_error("Can not allocate tx ring.");
     1526        if (rc != EOK)
    14601527                return rc;
    1461         }
    14621528       
    14631529        bzero(e1000_data->tx_ring.virtual,
    1464                 E1000_TX_PACKETS_COUNT * sizeof(e1000_tx_descriptor_t));
    1465 
     1530            E1000_TX_PACKETS_COUNT * sizeof(e1000_tx_descriptor_t));
     1531       
    14661532        E1000_REG_WRITE(e1000_data, E1000_TDBAH,
    1467                 (uint32_t) (PTR_TO_U64(e1000_data->tx_ring.physical) >> 32));
     1533            (uint32_t) (PTR_TO_U64(e1000_data->tx_ring.physical) >> 32));
    14681534        E1000_REG_WRITE(e1000_data, E1000_TDBAL,
    1469                 (uint32_t) PTR_TO_U64(e1000_data->tx_ring.physical));
    1470 
    1471         e1000_data->tx_ring_packets = malloc(E1000_TX_PACKETS_COUNT *
    1472                 sizeof(packet_t *));
    1473        
    1474 
     1535            (uint32_t) PTR_TO_U64(e1000_data->tx_ring.physical));
     1536       
     1537        e1000_data->tx_ring_packets =
     1538            malloc(E1000_TX_PACKETS_COUNT * sizeof(packet_t *));
     1539        // FIXME: Check return value
     1540       
    14751541        e1000_initialize_tx_registers(e1000_data);
    14761542       
    14771543        fibril_mutex_unlock(&e1000_data->tx_lock);
    1478 
    14791544        return EOK;
    14801545}
    14811546
    1482 /** Uninitializes transmit structure
    1483  *
    1484  * @param nic_data The NIC data
    1485  */
    1486 static void e1000_uninitialize_tx_structure(e1000_t * e1000_data)
     1547/** Uninitialize transmit structure
     1548 *
     1549 * @param nic_data NIC data
     1550 *
     1551 */
     1552static void e1000_uninitialize_tx_structure(e1000_t *e1000_data)
    14871553{
    14881554        free(e1000_data->tx_ring_packets);
    1489        
    14901555        dma_free(&e1000_data->tx_ring);
    14911556}
    14921557
    1493 
    14941558/** Clear transmit descriptor ring
    14951559 *
    1496  * @param nic_data The NIC data
    1497  */
    1498 static void e1000_clear_tx_ring(nic_t * nic_data)
    1499 {
    1500         //write descriptor
    1501         unsigned int offset;
    1502         for (offset = 0; offset < E1000_TX_PACKETS_COUNT; offset++) {
    1503                 e1000_clear_tx_descriptor(nic_data, offset);   
    1504         }
     1560 * @param nic_data NIC data
     1561 *
     1562 */
     1563static void e1000_clear_tx_ring(nic_t *nic_data)
     1564{
     1565        /* Write descriptor */
     1566        for (unsigned int offset = 0;
     1567            offset < E1000_TX_PACKETS_COUNT;
     1568            offset++)
     1569                e1000_clear_tx_descriptor(nic_data, offset);
    15051570}
    15061571
    15071572/** Enable transmit
    15081573 *
     1574 * @param e1000_data E1000 data
     1575 *
     1576 */
     1577static void e1000_enable_tx(e1000_t *e1000_data)
     1578{
     1579        /* Set Transmit Enable Bit */
     1580        E1000_REG_WRITE(e1000_data, E1000_TCTL,
     1581            E1000_REG_READ(e1000_data, E1000_TCTL) | (TCTL_EN));
     1582}
     1583
     1584/** Disable transmit
     1585 *
     1586 * @param e1000_data E1000 data
     1587 *
     1588 */
     1589static void e1000_disable_tx(e1000_t *e1000_data)
     1590{
     1591        /* Clear Transmit Enable Bit */
     1592        E1000_REG_WRITE(e1000_data, E1000_TCTL,
     1593            E1000_REG_READ(e1000_data, E1000_TCTL) & ~(TCTL_EN));
     1594}
     1595
     1596/** Reset E1000 device
     1597 *
    15091598 * @param e1000_data The E1000 data
    1510  */
    1511 static void e1000_enable_tx(e1000_t *e1000_data)
    1512 {
    1513         //setting Transmit Enable Bit
    1514         E1000_REG_WRITE(e1000_data, E1000_TCTL,
    1515                 E1000_REG_READ(e1000_data, E1000_TCTL) | (TCTL_EN));
    1516 }
    1517 
    1518 /** Disable transmit
    1519  *
    1520  * @param e1000_data The E1000 data
    1521  */
    1522 static void e1000_disable_tx(e1000_t *e1000_data)
    1523 {
    1524         //clearing Transmit Enable Bit
    1525         E1000_REG_WRITE(e1000_data, E1000_TCTL,
    1526                 E1000_REG_READ(e1000_data, E1000_TCTL) & ~(TCTL_EN));
    1527 }
    1528 
    1529 /** Reset E1000 device
    1530  *
    1531  * @param e1000_data The E1000 data
     1599 *
    15321600 */
    15331601static int e1000_reset(nic_t *nic_data)
     
    15361604       
    15371605        E1000_REG_WRITE(e1000_data, E1000_CTRL, CTRL_RST);
    1538         //wait for the reset
    1539         usleep(10);
    1540         //check if RST_BIT cleared
    1541         assert(! (E1000_REG_READ(e1000_data, E1000_CTRL)
    1542                 & (CTRL_RST)));
    1543 
     1606       
     1607        /* Wait for the reset */
     1608        usleep(20);
     1609       
     1610        /* check if RST_BIT cleared */
     1611        if (E1000_REG_READ(e1000_data, E1000_CTRL) & (CTRL_RST))
     1612                return EINVAL;
     1613       
    15441614        e1000_initialize_registers(e1000_data);
    15451615        e1000_initialize_rx_registers(e1000_data);
    15461616        e1000_initialize_tx_registers(e1000_data);
    1547 
    15481617        e1000_fill_mac_from_eeprom(e1000_data);
    1549 
    15501618        e1000_initialize_filters(e1000_data);
    1551        
    15521619        e1000_initialize_vlan(e1000_data);
    15531620       
    15541621        return EOK;
    1555        
    1556 }
    1557 
     1622}
    15581623
    15591624/** Activate the device to receive and transmit packets
    15601625 *
    1561  *  @param nic_data The nic driver data
    1562  *  @return EOK if activated successfully, error code otherwise
     1626 * @param nic_data NIC driver data
     1627 *
     1628 * @return EOK if activated successfully
     1629 * @return Error code otherwise
     1630 *
    15631631 */
    15641632static int e1000_on_activating(nic_t *nic_data)
    15651633{
    15661634        assert(nic_data);
    1567 
     1635       
    15681636        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
    1569 
     1637       
    15701638        fibril_mutex_lock(&e1000_data->rx_lock);
    15711639        fibril_mutex_lock(&e1000_data->tx_lock);
    15721640        fibril_mutex_lock(&e1000_data->ctrl_lock);
    1573 
     1641       
    15741642        e1000_enable_interrupts(e1000_data);
    1575 
     1643       
    15761644        nic_enable_interrupt(nic_data, e1000_data->irq);
    15771645       
    15781646        e1000_clear_rx_ring(e1000_data);
    15791647        e1000_enable_rx(e1000_data);
    1580 
     1648       
    15811649        e1000_clear_tx_ring(nic_data);
    15821650        e1000_enable_tx(e1000_data);
     
    15891657        fibril_mutex_unlock(&e1000_data->tx_lock);
    15901658        fibril_mutex_unlock(&e1000_data->rx_lock);
    1591 
     1659       
    15921660        return EOK;
    15931661}
     
    15951663/** Callback for NIC_STATE_DOWN change
    15961664 *
    1597  *  @param nic_data The nic driver data
    1598  *  @return EOK if succeed, error code otherwise
     1665 * @param nic_data NIC driver data
     1666 *
     1667 * @return EOK if succeed
     1668 * @return Error code otherwise
     1669 *
    15991670 */
    16001671static int e1000_on_down_unlocked(nic_t *nic_data)
     
    16071678       
    16081679        e1000_disable_tx(e1000_data);
    1609        
    16101680        e1000_disable_rx(e1000_data);
    1611 
     1681       
    16121682        nic_disable_interrupt(nic_data, e1000_data->irq);
    1613        
    16141683        e1000_disable_interrupts(e1000_data);
    16151684       
    1616         //wait for the for the end of all data transfers to descriptors
     1685        /*
     1686         * Wait for the for the end of all data
     1687         * transfers to descriptors.
     1688         */
    16171689        usleep(100);
    1618 
     1690       
    16191691        return EOK;
    16201692}
     
    16221694/** Callback for NIC_STATE_DOWN change
    16231695 *
    1624  *  @param nic_data The nic driver data
    1625  *  @return EOK if succeed, error code otherwise
     1696 * @param nic_data NIC driver data
     1697 *
     1698 * @return EOK if succeed
     1699 * @return Error code otherwise
     1700 *
    16261701 */
    16271702static int e1000_on_down(nic_t *nic_data)
     
    16321707        fibril_mutex_lock(&e1000_data->tx_lock);
    16331708        fibril_mutex_lock(&e1000_data->ctrl_lock);
    1634 
     1709       
    16351710        int rc = e1000_on_down_unlocked(nic_data);
    16361711       
     
    16441719/** Callback for NIC_STATE_STOPPED change
    16451720 *
    1646  *  @param nic_data The nic driver data
    1647  *  @return EOK if succeed, error code otherwise
     1721 * @param nic_data NIC driver data
     1722 *
     1723 * @return EOK if succeed
     1724 * @return Error code otherwise
     1725 *
    16481726 */
    16491727static int e1000_on_stopping(nic_t *nic_data)
     
    16561734       
    16571735        int rc = e1000_on_down_unlocked(nic_data);
    1658         if (rc == EOK) {
     1736        if (rc == EOK)
    16591737                rc = e1000_reset(nic_data);
    1660         }
    16611738       
    16621739        fibril_mutex_unlock(&e1000_data->ctrl_lock);
     
    16671744}
    16681745
    1669 
    1670 
    16711746/** Create driver data structure
    16721747 *
    1673  *  @return Intialized device data structure or NULL
     1748 * @return Intialized device data structure or NULL
     1749 *
    16741750 */
    16751751static e1000_t *e1000_create_dev_data(ddf_dev_t *dev)
     
    16771753        assert(dev);
    16781754        assert(!dev->driver_data);
    1679 
     1755       
    16801756        nic_t *nic_data = nic_create_and_bind(dev);
    16811757        if (!nic_data)
    16821758                return NULL;
    1683 
     1759       
    16841760        e1000_t *e1000_data = malloc(sizeof(e1000_t));
    16851761        if (!e1000_data) {
     
    16871763                return NULL;
    16881764        }
    1689 
     1765       
    16901766        bzero(e1000_data, sizeof(e1000_t));
    1691 
     1767       
    16921768        nic_set_specific(nic_data, e1000_data);
    16931769        nic_set_write_packet_handler(nic_data, e1000_write_packet);
    1694         nic_set_state_change_handlers(
    1695                 nic_data,
    1696                 e1000_on_activating,
    1697                 e1000_on_down,
    1698                 e1000_on_stopping
    1699         );
    1700         nic_set_filtering_change_handlers(
    1701                 nic_data,
    1702                 e1000_on_unicast_mode_change,
    1703                 e1000_on_multicast_mode_change,
    1704                 e1000_on_broadcast_mode_change,
    1705                 NULL,
    1706                 e1000_on_vlan_mask_change
    1707         );
    1708        
     1770        nic_set_state_change_handlers(nic_data, e1000_on_activating,
     1771            e1000_on_down, e1000_on_stopping);
     1772        nic_set_filtering_change_handlers(nic_data,
     1773            e1000_on_unicast_mode_change, e1000_on_multicast_mode_change,
     1774            e1000_on_broadcast_mode_change, NULL, e1000_on_vlan_mask_change);
    17091775        nic_set_poll_handlers(nic_data, e1000_poll_mode_change, e1000_poll);
    1710 
     1776       
    17111777        fibril_mutex_initialize(&e1000_data->ctrl_lock);
    17121778        fibril_mutex_initialize(&e1000_data->rx_lock);
     
    17171783}
    17181784
    1719 /** Delete driver data structure if not null and null the pointer
    1720  *
    1721  *  @param data The e1000 device data structure
     1785/** Delete driver data structure
     1786 *
     1787 * @param data E1000 device data structure
     1788 *
    17221789 */
    17231790inline static void e1000_delete_dev_data(ddf_dev_t *dev)
    17241791{
    17251792        assert(dev);
     1793       
    17261794        if (dev->driver_data != NULL)
    17271795                nic_unbind_and_destroy(dev);
    17281796}
    17291797
    1730 /** Clean up the e1000 device structure.
    1731  *
    1732  * @param dev The device structure.
     1798/** Clean up the E1000 device structure.
     1799 *
     1800 * @param dev Device structure.
     1801 *
    17331802 */
    17341803static void e1000_dev_cleanup(ddf_dev_t *dev)
    17351804{
    17361805        assert(dev);
    1737 
     1806       
    17381807        e1000_delete_dev_data(dev);
    1739 
     1808       
    17401809        if (dev->parent_sess != NULL) {
    17411810                async_hangup(dev->parent_sess);
     
    17461815/** Fill the irq and io_addr part of device data structure
    17471816 *
    1748  *  The hw_resources must be obtained before calling this function
    1749  *
    1750  *  @param dev The device structure
    1751  *  @param hw_resources Drive hardware resources obtained from the parent device
    1752  *  @return EOK if succeed, negative error code otherwise
    1753  */
    1754 static int e1000_fill_resource_info(ddf_dev_t *dev, const hw_res_list_parsed_t
    1755     *hw_resources)
     1817 * The hw_resources must be obtained before calling this function
     1818 *
     1819 * @param dev          Device structure
     1820 * @param hw_resources Hardware resources obtained from the parent device
     1821 *
     1822 * @return EOK if succeed
     1823 * @return Negative error code otherwise
     1824 *
     1825 */
     1826static int e1000_fill_resource_info(ddf_dev_t *dev,
     1827    const hw_res_list_parsed_t *hw_resources)
    17561828{
    17571829        assert(dev != NULL);
    17581830        assert(hw_resources != NULL);
    17591831        assert(dev->driver_data != NULL);
    1760 
     1832       
    17611833        e1000_t *e1000_data = DRIVER_DATA_DEV(dev);
    1762 
    1763         if (hw_resources->irqs.count != 1) {
    1764                 nlog_error("%s device: unexpected irq count", dev->name);
     1834       
     1835        if (hw_resources->irqs.count != 1)
    17651836                return EINVAL;
    1766         };
    1767 
     1837       
    17681838        e1000_data->irq = hw_resources->irqs.irqs[0];
    1769 
    1770         e1000_data->phys_reg_base =
    1771                 MEMADDR_TO_PTR(hw_resources->mem_ranges.ranges[0].address);
    1772         //TODO remove when hack is not neccessary
    1773         nlog_info("%s DIRTY HACK: FILL THIS ADDRESS %p "
    1774                 "to kernel/arch/(ia32|amd64)/src/mm/page.c ", dev->name,
    1775                 e1000_data->phys_reg_base);
    1776 
     1839        e1000_data->phys_reg_base =
     1840            MEMADDR_TO_PTR(hw_resources->mem_ranges.ranges[0].address);
     1841       
    17771842        return EOK;
    17781843}
     
    17801845/** Obtain information about hardware resources of the device
    17811846 *
    1782  *  The device must be connected to the parent
    1783  *
    1784  *  @param dev The device structure
    1785  *  @return EOK if succeed, negative error code otherwise
     1847 * The device must be connected to the parent
     1848 *
     1849 * @param dev Device structure
     1850 *
     1851 * @return EOK if succeed
     1852 * @return Negative error code otherwise
     1853 *
    17861854 */
    17871855static int e1000_get_resource_info(ddf_dev_t *dev)
     
    17891857        assert(dev != NULL);
    17901858        assert(NIC_DATA_DEV(dev) != NULL);
    1791 
     1859       
    17921860        hw_res_list_parsed_t hw_res_parsed;
    17931861        hw_res_list_parsed_init(&hw_res_parsed);
    1794 
     1862       
    17951863        /* Get hw resources form parent driver */
    17961864        int rc = nic_get_resources(NIC_DATA_DEV(dev), &hw_res_parsed);
    17971865        if (rc != EOK)
    17981866                return rc;
    1799 
     1867       
    18001868        /* Fill resources information to the device */
    18011869        rc = e1000_fill_resource_info(dev, &hw_res_parsed);
    18021870        hw_res_list_parsed_clean(&hw_res_parsed);
    1803 
     1871       
    18041872        return rc;
    18051873}
    18061874
    1807 /** Initialize the e1000 device structure
    1808  *
    1809  *  @param dev The device information
    1810  *  @return EOK if succeed, negative error code otherwise
     1875/** Initialize the E1000 device structure
     1876 *
     1877 * @param dev Device information
     1878 *
     1879 * @return EOK if succeed
     1880 * @return Negative error code otherwise
     1881 *
    18111882 */
    18121883static int e1000_device_initialize(ddf_dev_t *dev)
    18131884{
    1814         int rc = EOK;
    1815 
    18161885        /* Allocate driver data for the device. */
    18171886        e1000_t *e1000_data = e1000_create_dev_data(dev);
    1818         if (e1000_data == NULL) {
    1819                 nlog_error("Not enough memory for initializing %s.", dev->name);
     1887        if (e1000_data == NULL)
    18201888                return ENOMEM;
    1821         }
    1822 
     1889       
    18231890        /* Obtain and fill hardware resources info */
    18241891        rc = e1000_get_resource_info(dev);
    18251892        if (rc != EOK) {
    1826                 nlog_error("Can not obatin hw resources information");
    1827                 goto failed;
     1893                e1000_dev_cleanup(dev);
     1894                return rc;
    18281895        }
    18291896       
    18301897        rc = pci_config_space_read_16(dev->parent_sess, PCI_DEVICE_ID,
    1831                 &e1000_data->device_id);
     1898            &e1000_data->device_id);
    18321899        if (rc != EOK) {
    1833                 nlog_error("Can not load PCI device_id.");
    1834                 goto failed;
     1900                e1000_dev_cleanup(dev);
     1901                return rc;
    18351902        }
    1836 
    1837         return rc;
    1838 
    1839 failed:
    1840         nlog_error("The device initialization failed");
    1841         e1000_dev_cleanup(dev);
    1842         return rc;
    1843 }
    1844 
    1845 /** Enable the i/o ports of the device.
    1846  *
    1847  * @param dev   The E1000 device.
    1848  * @return              EOK if successed, negative error code otherwise
     1903       
     1904        return EOK;
     1905}
     1906
     1907/** Enable the I/O ports of the device.
     1908 *
     1909 * @param dev E1000 device.
     1910 *
     1911 * @return EOK if successed
     1912 * @return Negative error code otherwise
     1913 *
    18491914 */
    18501915static int e1000_pio_enable(ddf_dev_t *dev)
    18511916{
    18521917        e1000_t *e1000_data = DRIVER_DATA_DEV(dev);
    1853 
    1854         /* Gain control over port's registers. */
    1855         if (pio_enable(e1000_data->phys_reg_base, 8 * PAGE_SIZE,
    1856                 &e1000_data->virt_reg_base)) { //TODO 8*PAGE_SIZE nahradit
    1857                 nlog_error(
    1858                         "Cannot gain the memory mapped registers %lx for device %s.",
    1859                         e1000_data->phys_reg_base,
    1860                     dev->name
    1861                 );
     1918       
     1919        int rc = pio_enable(e1000_data->phys_reg_base, 8 * PAGE_SIZE,
     1920            &e1000_data->virt_reg_base);
     1921        if (rc != EOK)
    18621922                return EADDRNOTAVAIL;
    1863         }
    1864 
     1923       
    18651924        return EOK;
    18661925}
     
    18701929 * Probe and initialize the newly added device.
    18711930 *
    1872  * @param dev   The E1000 device.
     1931 * @param dev E1000 device.
     1932 *
    18731933 */
    18741934int e1000_add_device(ddf_dev_t *dev)
    18751935{
    18761936        assert(dev);
    1877 
    1878         /* Init device structure for e1000 */
     1937       
     1938        /* Initialize device structure for E1000 */
    18791939        int rc = e1000_device_initialize(dev);
    18801940        if (rc != EOK)
    18811941                return rc;
    18821942       
    1883         // Device initialization
    1884         nic_t * nic_data = dev->driver_data;
    1885        
     1943        /* Device initialization */
     1944        nic_t *nic_data = dev->driver_data;
    18861945        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
    1887 
     1946       
    18881947        /* Map registers */
    18891948        rc = e1000_pio_enable(dev);
    18901949        if (rc != EOK)
    18911950                goto err_destroy;
    1892 
     1951       
    18931952        e1000_initialize_registers(e1000_data);
    1894 
    18951953        rc = e1000_initialize_tx_structure(e1000_data);
    1896         if( rc!= EOK )
     1954        if (rc != EOK)
    18971955                goto err_pio;
    1898 
     1956       
    18991957        fibril_mutex_lock(&e1000_data->rx_lock);
    19001958       
     
    19031961       
    19041962        fibril_mutex_unlock(&e1000_data->rx_lock);
    1905 
     1963       
    19061964        e1000_initialize_vlan(e1000_data);
    1907 
     1965       
    19081966        rc = nic_register_as_ddf_fun(nic_data, &e1000_dev_ops);
    1909         if (rc != EOK) {
    1910                 nlog_error("Failed to register as DDF function - error %d", rc);
     1967        if (rc != EOK)
    19111968                goto err_tx_structure;
    1912         }
    19131969       
    19141970        rc = e1000_register_int_handler(nic_data);
    1915         if (rc != EOK) {
     1971        if (rc != EOK)
    19161972                goto err_tx_structure;
    1917         }
    19181973       
    19191974        rc = nic_connect_to_services(nic_data);
    1920         if (rc != EOK) {
    1921                 nlog_error("Failed to connect to essential services - error %d", rc);
     1975        if (rc != EOK)
    19221976                goto err_irq;
    1923         }
    19241977       
    19251978        rc = e1000_initialize_rx_structure(nic_data);
    1926         if (rc != EOK) {
    1927                 nlog_error("Failed to init rx - error %d", rc);
     1979        if (rc != EOK)
    19281980                goto err_irq;
    1929         }
    19301981       
    19311982        nic_address_t e1000_address;
    19321983        e1000_get_address(e1000_data, &e1000_address);
    19331984        rc = nic_report_address(nic_data, &e1000_address);
    1934         if (rc != EOK) {
    1935                 nlog_error("Failed to setup address - error %d", rc);
     1985        if (rc != EOK)
    19361986                goto err_rx_structure;
    1937         }
    1938 
     1987       
    19391988        struct timeval period;
    19401989        period.tv_sec = 0;
    1941         period.tv_usec = E1000_DEFAULT_INTERRUPT_INTEVAL_USEC;
     1990        period.tv_usec = E1000_DEFAULT_INTERRUPT_INTERVAL_USEC;
    19421991        rc = nic_report_poll_mode(nic_data, NIC_POLL_PERIODIC, &period);
    1943         if (rc != EOK) {
    1944                 nlog_error("Failed report poll mode %d", rc);
     1992        if (rc != EOK)
    19451993                goto err_rx_structure;
    1946         }
    1947        
    1948         nlog_info("The %s device has been successfully initialized.",
    1949             dev->name);
    1950 
     1994       
    19511995        return EOK;
    1952 
     1996       
    19531997err_rx_structure:
    19541998        e1000_uninitialize_rx_structure(nic_data);
     
    19582002        e1000_uninitialize_tx_structure(e1000_data);
    19592003err_pio:
    1960         // e1000_pio_disable(dev);
    1961         /* TODO: find out if the pio_disable is needed */
     2004        // TODO: e1000_pio_disable(dev);
    19622005err_destroy:
    19632006        e1000_dev_cleanup(dev);
    19642007        return rc;
    1965 };
     2008}
    19662009
    19672010/** Read 16-bit value from EEPROM of E1000 adapter
    1968  *  Function uses EERD register.
    1969  *
    1970  *  @param device The E1000 device
    1971  *  @param eeprom_address 8-bit EEPROM address
    1972  *  @return 16-bit value from EEPROM
     2011 *
     2012 * Read using the EERD register.
     2013 *
     2014 * @param device         E1000 device
     2015 * @param eeprom_address 8-bit EEPROM address
     2016 *
     2017 * @return 16-bit value from EEPROM
     2018 *
    19732019 */
    19742020static uint16_t e1000_eeprom_read(e1000_t *e1000_data, uint8_t eeprom_address)
    19752021{
    1976 
    19772022        fibril_mutex_lock(&e1000_data->eeprom_lock);
    1978 
     2023       
    19792024        uint32_t eerd_done;
    19802025        uint32_t eerd_address_offset;
    1981 
     2026       
    19822027        switch (e1000_data->device_id) {
    1983                 case 0x107c:
    1984                 case 0x1013:
    1985                 case 0x1018:
    1986                 case 0x1019:
    1987                 case 0x101A:
    1988                 case 0x1076:
    1989                 case 0x1077:
    1990                 case 0x1078:
    1991                 case 0x10b9:
    1992                         //82541xx and 82547GI/EI
    1993                         //TODO add more device ids
    1994                         eerd_done =
    1995                                 EERD_DONE_82541XX_82547GI_EI;
    1996                         eerd_address_offset =
    1997                                 EERD_ADDRESS_OFFSET_82541XX_82547GI_EI;
    1998                         break;
    1999                 default:
    2000                         eerd_done = EERD_DONE;
    2001                         eerd_address_offset = EERD_ADDRESS_OFFSET;
    2002                         break;
     2028        case 0x107c:
     2029        case 0x1013:
     2030        case 0x1018:
     2031        case 0x1019:
     2032        case 0x101A:
     2033        case 0x1076:
     2034        case 0x1077:
     2035        case 0x1078:
     2036        case 0x10b9:
     2037                /* 82541xx and 82547GI/EI */
     2038                eerd_done = EERD_DONE_82541XX_82547GI_EI;
     2039                eerd_address_offset = EERD_ADDRESS_OFFSET_82541XX_82547GI_EI;
     2040                break;
     2041        default:
     2042                eerd_done = EERD_DONE;
     2043                eerd_address_offset = EERD_ADDRESS_OFFSET;
     2044                break;
    20032045        }
    2004 
    2005         //write address and START bit to EERD register
    2006         uint32_t write_data =
    2007                 EERD_START | (((uint32_t)eeprom_address) << eerd_address_offset);
     2046       
     2047        /* Write address and START bit to EERD register */
     2048        uint32_t write_data = EERD_START |
     2049            (((uint32_t) eeprom_address) << eerd_address_offset);
    20082050        E1000_REG_WRITE(e1000_data, E1000_EERD, write_data);
    20092051       
    20102052        uint32_t eerd = E1000_REG_READ(e1000_data, E1000_EERD);
    20112053        while ((eerd & eerd_done) == 0) {
    2012                         usleep(1);
    2013                         eerd = E1000_REG_READ(e1000_data, E1000_EERD);
     2054                usleep(1);
     2055                eerd = E1000_REG_READ(e1000_data, E1000_EERD);
    20142056        }
    20152057       
    20162058        fibril_mutex_unlock(&e1000_data->eeprom_lock);
    2017 
    2018         return (uint16_t)(eerd >> EERD_DATA_OFFSET);
     2059       
     2060        return (uint16_t) (eerd >> EERD_DATA_OFFSET);
    20192061}
    20202062
    20212063/** Get MAC address of the E1000 adapter
    20222064 *
    2023  *  @param device The E10000 device
    2024  *  @param address The place to store the address
    2025  *  @param max_len Maximal addresss length to store
    2026  *  @return EOK if succeed, negative error code otherwise
     2065 * @param device  E1000 device
     2066 * @param address Place to store the address
     2067 * @param max_len Maximal addresss length to store
     2068 *
     2069 * @return EOK if succeed
     2070 * @return Negative error code otherwise
     2071 *
    20272072 */
    20282073static int e1000_get_address(e1000_t *e1000_data, nic_address_t *address)
    20292074{
    20302075        fibril_mutex_lock(&e1000_data->rx_lock);
    2031 
    2032         uint8_t *mac0_dest = (uint8_t *)address->address;
    2033         uint8_t *mac1_dest = (uint8_t *)address->address + 1;
    2034         uint8_t *mac2_dest = (uint8_t *)address->address + 2;
    2035         uint8_t *mac3_dest = (uint8_t *)address->address + 3;
    2036         uint8_t *mac4_dest = (uint8_t *)address->address + 4;
    2037         uint8_t *mac5_dest = (uint8_t *)address->address + 5;
    2038 
     2076       
     2077        uint8_t *mac0_dest = (uint8_t *) address->address;
     2078        uint8_t *mac1_dest = (uint8_t *) address->address + 1;
     2079        uint8_t *mac2_dest = (uint8_t *) address->address + 2;
     2080        uint8_t *mac3_dest = (uint8_t *) address->address + 3;
     2081        uint8_t *mac4_dest = (uint8_t *) address->address + 4;
     2082        uint8_t *mac5_dest = (uint8_t *) address->address + 5;
     2083       
    20392084        uint32_t rah = E1000_REG_READ(e1000_data, E1000_RAH_ARRAY(0));
    20402085        uint32_t ral = E1000_REG_READ(e1000_data, E1000_RAL_ARRAY(0));
     
    20462091        *mac4_dest = (uint8_t) rah;
    20472092        *mac5_dest = (uint8_t) (rah >> 8);
    2048 
     2093       
    20492094        fibril_mutex_unlock(&e1000_data->rx_lock);
    2050 
    20512095        return EOK;
    20522096};
     
    20542098/** Set card MAC address
    20552099 *
    2056  *  @param device The E1000 device
    2057  *  @param address The place to store the address
    2058  *  @param max_len Maximal addresss length to store
    2059  *  @return EOK if succeed, negative error code otherwise
     2100 * @param device  E1000 device
     2101 * @param address Address
     2102 *
     2103 * @return EOK if succeed
     2104 * @return Negative error code otherwise
    20602105 */
    20612106static int e1000_set_addr(ddf_fun_t *dev, const nic_address_t *addr)
     
    20632108        nic_t *nic_data = NIC_DATA_DEV(dev);
    20642109        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
     2110       
    20652111        fibril_mutex_lock(&e1000_data->rx_lock);
    20662112        fibril_mutex_lock(&e1000_data->tx_lock);
    2067 
     2113       
    20682114        int rc = nic_report_address(nic_data, addr);
    2069         if (rc == EOK) {
    2070                 e1000_write_receive_address(e1000_data, 0, addr, false);       
    2071         }
    2072 
     2115        if (rc == EOK)
     2116                e1000_write_receive_address(e1000_data, 0, addr, false)
     2117       
    20732118        fibril_mutex_unlock(&e1000_data->tx_lock);
    20742119        fibril_mutex_unlock(&e1000_data->rx_lock);
    2075 
     2120       
    20762121        return rc;
    20772122}
    20782123
    20792124static void e1000_eeprom_get_address(e1000_t *e1000_data,
    2080         nic_address_t *address)
    2081 {
    2082         uint16_t *mac0_dest = (uint16_t *)address->address;
    2083         uint16_t *mac2_dest = (uint16_t *)(address->address + 2);
    2084         uint16_t *mac4_dest = (uint16_t *)(address->address + 4);
    2085 
     2125    nic_address_t *address)
     2126{
     2127        uint16_t *mac0_dest = (uint16_t *) address->address;
     2128        uint16_t *mac2_dest = (uint16_t *) (address->address + 2);
     2129        uint16_t *mac4_dest = (uint16_t *) (address->address + 4);
     2130       
    20862131        *mac0_dest = e1000_eeprom_read(e1000_data, 0);
    20872132        *mac2_dest = e1000_eeprom_read(e1000_data, 1);
     
    20892134}
    20902135
    2091 /** Send packet with the hardware
    2092  *
    2093  * @param nic_data The nic driver data structure
    2094  * @param packet The packet to send
    2095  *
    2096  * @return EOK if succeed, error code in the case of error
     2136/** Send packet
     2137 *
     2138 * @param nic_data NIC driver data structure
     2139 * @param packet   Packet to send
     2140 *
     2141 * @return EOK if succeed
     2142 * @return Error code in the case of error
     2143 *
    20972144 */
    20982145static void e1000_write_packet(nic_t *nic_data, packet_t *packet)
    20992146{
    21002147        assert(nic_data);
    2101 
     2148       
    21022149        e1000_t *e1000_data = DRIVER_DATA_NIC(nic_data);
    21032150        fibril_mutex_lock(&e1000_data->tx_lock);
    2104 
     2151       
    21052152        uint32_t tdt = E1000_REG_READ(e1000_data, E1000_TDT);
    2106         e1000_tx_descriptor_t * tx_descriptor_addr = (e1000_tx_descriptor_t *)
    2107                 (e1000_data->tx_ring.virtual + tdt * sizeof(e1000_tx_descriptor_t));
    2108 
     2153        e1000_tx_descriptor_t *tx_descriptor_addr = (e1000_tx_descriptor_t *)
     2154            (e1000_data->tx_ring.virtual + tdt * sizeof(e1000_tx_descriptor_t));
     2155       
    21092156        bool descriptor_available = false;
    2110         //Descriptor never used
    2111         if (tx_descriptor_addr->length == 0) {
     2157       
     2158        /* Descriptor never used */
     2159        if (tx_descriptor_addr->length == 0)
    21122160                descriptor_available = true;
    2113         }
    2114         //Descriptor done
     2161       
     2162        /* Descriptor done */
    21152163        if (tx_descriptor_addr->status & TXDESCRIPTOR_STATUS_DD) {
    21162164                descriptor_available = true;
    2117                 packet_t * old_packet = *(e1000_data->tx_ring_packets + tdt);
     2165                packet_t *old_packet = *(e1000_data->tx_ring_packets + tdt);
    21182166                if (old_packet) {
    21192167                        nic_dma_unlock_packet(old_packet);
     
    21212169                }
    21222170        }
    2123         if (! descriptor_available) {
    2124                 nlog_error("Packet %d lost no space in tx ring", packet->packet_id);
     2171       
     2172        if (!descriptor_available) {
     2173                /* Packet lost */
    21252174                fibril_mutex_unlock(&e1000_data->tx_lock);
    21262175                return;
    21272176        }
    2128 
     2177       
    21292178        size_t packet_size = packet_get_data_length(packet);
    2130 
    2131         void * phys_addr;
    2132         phys_addr = nic_dma_lock_packet(packet);
     2179       
     2180        void *phys_addr = nic_dma_lock_packet(packet);
    21332181        if (!phys_addr) {
    21342182                fibril_mutex_unlock(&e1000_data->tx_lock);
    21352183                return;
    21362184        }
    2137 
    2138         *(e1000_data->tx_ring_packets + tdt) = packet; 
    2139 
     2185       
     2186        *(e1000_data->tx_ring_packets + tdt) = packet;
     2187       
    21402188        tx_descriptor_addr->phys_addr =
    2141                 PTR_TO_U64(phys_addr + packet->data_start);
     2189            PTR_TO_U64(phys_addr + packet->data_start);
    21422190        tx_descriptor_addr->length = packet_size;
    2143         tx_descriptor_addr->command =
    2144                 TXDESCRIPTOR_COMMAND_RS | // report status to STATUS.DD (descr. done)
    2145                 TXDESCRIPTOR_COMMAND_IFCS | // add ethernet CRC
    2146                 TXDESCRIPTOR_COMMAND_EOP; // end of packet
     2191       
     2192        /*
     2193         * Report status to STATUS.DD (descriptor done),
     2194         * add ethernet CRC, end of packet.
     2195         */
     2196        tx_descriptor_addr->command = TXDESCRIPTOR_COMMAND_RS |
     2197            TXDESCRIPTOR_COMMAND_IFCS |
     2198            TXDESCRIPTOR_COMMAND_EOP;
     2199       
    21472200        tx_descriptor_addr->checksum_offset = 0;
    21482201        tx_descriptor_addr->status = 0;
     
    21502203                tx_descriptor_addr->special = e1000_data->vlan_tag;
    21512204                tx_descriptor_addr->command |= TXDESCRIPTOR_COMMAND_VLE;
    2152         } else {
    2153                 tx_descriptor_addr->special = 0;       
    2154         }
     2205        } else
     2206                tx_descriptor_addr->special = 0;
     2207       
    21552208        tx_descriptor_addr->checksum_start_field = 0;
    21562209       
    2157         ++tdt;
    2158         if (tdt == E1000_TX_PACKETS_COUNT ) {
     2210        tdt++;
     2211        if (tdt == E1000_TX_PACKETS_COUNT)
    21592212                tdt = 0;
    2160         }
    2161 
     2213       
    21622214        E1000_REG_WRITE(e1000_data, E1000_TDT, tdt);
    2163 
    2164 
     2215       
    21652216        fibril_mutex_unlock(&e1000_data->tx_lock);
    2166 
    2167 };
    2168 
    2169 /** Initialize the driver
    2170  */
    2171 static void e1000_driver_init()
     2217}
     2218
     2219int main(void)
    21722220{
    21732221        int rc = dma_allocator_init();
    2174         if (rc != EOK) {
    2175                 nlog_error("Unable to initialize DMA allocator");
    2176         }
    2177 }
    2178 
    2179 /** Main function of E1000 driver
    2180  *
    2181  *  Just initialize the driver structures and
    2182  *  put it into the device drivers interface
    2183  */
    2184 int main(void)
    2185 {
    2186         int rc = nic_driver_init(NAME);
    2187         if (rc != EOK) {
     2222        if (rc != EOK)
    21882223                return rc;
    2189         }
    2190 
    2191         nlog_set_min_severity(DEBUG);
     2224       
    21922225        nic_driver_implement(&e1000_driver_ops, &e1000_dev_ops, &e1000_nic_iface);
    2193 
    21942226        e1000_driver_init();
    2195         nlog_info("HelenOS E1000 driver started");
    21962227        return ddf_driver_main(&e1000_driver);
    21972228}
  • uspace/drv/nic/e1k/e1k.h

    r63bcbbc r1df224c  
    2727 */
    2828
    29 /** @file e1000_defs.h
    30  *
    31  *  Registers, bit positions and masks definition of the E1000 network family
    32  *  cards
     29/** @file e1k.h
     30 *
     31 * Registers, bit positions and masks definition of the E1000 network family
     32 * cards
     33 *
    3334 */
    3435
    35 #ifndef E1000_DEFS_H_INCLUDED_
    36 #define E1000_DEFS_H_INCLUDED_
     36#ifndef E1K_H_
     37#define E1K_H_
    3738
    3839/** Ethernet CRC size after packet received in rx_descriptor */
    39 #define E1000_CRC_SIZE 4
     40#define E1000_CRC_SIZE  4
     41
     42#define VET_VALUE  0x8100
     43
     44#define E1000_RAL_ARRAY(n)   (E1000_RAL + ((n) * 8))
     45#define E1000_RAH_ARRAY(n)   (E1000_RAH + ((n) * 8))
     46#define E1000_VFTA_ARRAY(n)  (E1000_VFTA + (0x04 * (n)))
    4047
    4148/** Receive descriptior */
     
    7582/** VLAN tag bits */
    7683enum e1000_vlantag {
    77         VLANTAG_CFI = (1 << 12), /**< Canonical Form Indicator */
    78 };
    79 
    80 /** transmit descriptor COMMAND field bits */
     84        VLANTAG_CFI = (1 << 12),  /**< Canonical Form Indicator */
     85};
     86
     87/** Transmit descriptor COMMAND field bits */
    8188enum e1000_txdescriptor_command {
    82         TXDESCRIPTOR_COMMAND_VLE = (1 << 6), /**< VLAN Packet Enable */
    83         TXDESCRIPTOR_COMMAND_RS = (1 << 3), /**< Report Status */
    84         TXDESCRIPTOR_COMMAND_IFCS = (1 << 1), /**< Insert FCS */
    85         TXDESCRIPTOR_COMMAND_EOP = (1 << 0) /**< End Of Packet */
    86 };
    87 
    88 /** transmit descriptor STATUS field bits */
     89        TXDESCRIPTOR_COMMAND_VLE = (1 << 6),   /**< VLAN Packet Enable */
     90        TXDESCRIPTOR_COMMAND_RS = (1 << 3),    /**< Report Status */
     91        TXDESCRIPTOR_COMMAND_IFCS = (1 << 1),  /**< Insert FCS */
     92        TXDESCRIPTOR_COMMAND_EOP = (1 << 0)    /**< End Of Packet */
     93};
     94
     95/** Transmit descriptor STATUS field bits */
    8996enum e1000_txdescriptor_status {
    90         TXDESCRIPTOR_STATUS_DD = (1 << 0) /**< Descriptor Done */
    91 };
    92 
    93 #define VET_VALUE 0x8100
    94 
    95 /** e1000 Registers */
     97        TXDESCRIPTOR_STATUS_DD = (1 << 0)  /**< Descriptor Done */
     98};
     99
     100/** E1000 Registers */
    96101enum e1000_registers {
    97         E1000_CTRL = 0x0, /**< Device Control Register */
    98         E1000_STATUS = 0x8, /**< Device Status Register */
    99         E1000_EERD = 0x14, /**< EEPROM Read Register */
    100         E1000_TCTL = 0x400, /**< Transmit Control Register */
    101         E1000_TIPG = 0x410, /**< Transmit IPG Register */
    102         E1000_TDBAL = 0x3800, /**< Transmit Descriptor Base Address Low */
    103         E1000_TDBAH = 0x3804, /**< Transmit Descriptor Base Address High */
    104         E1000_TDLEN = 0x3808, /**< Transmit Descriptor Length */
    105         E1000_TDH = 0x3810, /**< Transmit Descriptor Head */
    106         E1000_TDT = 0x3818, /**< Transmit Descriptor Tail */
    107         E1000_RCTL = 0x100, /**< Receive Control Register */
    108         E1000_RDBAL = 0x2800, /**< Receive Descriptor Base Address Low */
    109         E1000_RDBAH = 0x2804, /**< Receive Descriptor Base Address High */
    110         E1000_RDLEN = 0x2808, /**< Receive Descriptor Length */
    111         E1000_RDH = 0x2810, /**< Receive Descriptor Head */
    112         E1000_RDT = 0x2818, /**< Receive Descriptor Tail */
    113         E1000_RAL = 0x5400, /**< Receive Address Low */
    114         E1000_RAH = 0x5404, /**< Receive Address High */
    115         E1000_VFTA = 0x5600, /**< VLAN Filter Table Array */
    116         E1000_VET = 0x38, /**< VLAN Ether Type */
    117         E1000_FCAL = 0x28, /**< Flow Control Address Low */
    118         E1000_FCAH = 0x2C, /**< Flow Control Address High */
    119         E1000_FCTTV = 0x170, /**< Flow Control Transmit Timer Value */
    120         E1000_FCT = 0x30, /**< Flow Control Type */
    121         E1000_ICR = 0xC0, /**< Interrupt Cause Read Register */
    122         E1000_ITR = 0xC4, /**< Interrupt Throttling Register */
    123         E1000_IMS = 0xD0, /**< Interrupt Mask Set/Read Register */
    124         E1000_IMC = 0xD8 /**< Interrupt Mask Clear Register */
    125 }; 
    126 #define E1000_RAL_ARRAY(n)  (E1000_RAL + ((n) * 8))
    127 #define E1000_RAH_ARRAY(n)  (E1000_RAH + ((n) * 8))
    128 #define E1000_VFTA_ARRAY(n)  (E1000_VFTA + (0x4 * (n)))
     102        E1000_CTRL = 0x0,      /**< Device Control Register */
     103        E1000_STATUS = 0x8,    /**< Device Status Register */
     104        E1000_EERD = 0x14,     /**< EEPROM Read Register */
     105        E1000_TCTL = 0x400,    /**< Transmit Control Register */
     106        E1000_TIPG = 0x410,    /**< Transmit IPG Register */
     107        E1000_TDBAL = 0x3800,  /**< Transmit Descriptor Base Address Low */
     108        E1000_TDBAH = 0x3804,  /**< Transmit Descriptor Base Address High */
     109        E1000_TDLEN = 0x3808,  /**< Transmit Descriptor Length */
     110        E1000_TDH = 0x3810,    /**< Transmit Descriptor Head */
     111        E1000_TDT = 0x3818,    /**< Transmit Descriptor Tail */
     112        E1000_RCTL = 0x100,    /**< Receive Control Register */
     113        E1000_RDBAL = 0x2800,  /**< Receive Descriptor Base Address Low */
     114        E1000_RDBAH = 0x2804,  /**< Receive Descriptor Base Address High */
     115        E1000_RDLEN = 0x2808,  /**< Receive Descriptor Length */
     116        E1000_RDH = 0x2810,    /**< Receive Descriptor Head */
     117        E1000_RDT = 0x2818,    /**< Receive Descriptor Tail */
     118        E1000_RAL = 0x5400,    /**< Receive Address Low */
     119        E1000_RAH = 0x5404,    /**< Receive Address High */
     120        E1000_VFTA = 0x5600,   /**< VLAN Filter Table Array */
     121        E1000_VET = 0x38,      /**< VLAN Ether Type */
     122        E1000_FCAL = 0x28,     /**< Flow Control Address Low */
     123        E1000_FCAH = 0x2C,     /**< Flow Control Address High */
     124        E1000_FCTTV = 0x170,   /**< Flow Control Transmit Timer Value */
     125        E1000_FCT = 0x30,      /**< Flow Control Type */
     126        E1000_ICR = 0xC0,      /**< Interrupt Cause Read Register */
     127        E1000_ITR = 0xC4,      /**< Interrupt Throttling Register */
     128        E1000_IMS = 0xD0,      /**< Interrupt Mask Set/Read Register */
     129        E1000_IMC = 0xD8       /**< Interrupt Mask Clear Register */
     130};
    129131
    130132/** EEPROM Read Register fields */
    131133enum e1000_eerd {
    132         EERD_START = (1 << 0), /**< Start Read */
    133         EERD_DONE = (1 << 4), /**< Read Done */
    134         EERD_DONE_82541XX_82547GI_EI = (1 << 1), /**<   Read Done for
    135                                                                                           *             82541xx and 82547GI/EI
    136                                                                                           */
    137         EERD_ADDRESS_OFFSET = 8, /**< Read Address offset */
    138         EERD_ADDRESS_OFFSET_82541XX_82547GI_EI = 2, /**<        Read Address offset
    139                                                                                                  *              82541xx and 82547GI/EI
    140                                                                                                  */
    141         EERD_DATA_OFFSET = 16 /**< Read Data */
     134        /** Start Read */
     135        EERD_START = (1 << 0),
     136        /** Read Done */
     137        EERD_DONE = (1 << 4),
     138        /** Read Done for 82541xx and 82547GI/EI */
     139        EERD_DONE_82541XX_82547GI_EI = (1 << 1),
     140        /** Read Address offset */
     141        EERD_ADDRESS_OFFSET = 8,
     142        /** Read Address offset for 82541xx and 82547GI/EI */
     143        EERD_ADDRESS_OFFSET_82541XX_82547GI_EI = 2,
     144        /** Read Data */
     145        EERD_DATA_OFFSET = 16
    142146};
    143147
    144148/** Device Control Register fields */
    145149enum e1000_ctrl {
    146         CTRL_FD = (1 << 0), /**< Full-Duplex */
    147         CTRL_LRST = (1 << 3), /**< Link Reset */
    148         CTRL_ASDE = (1 << 5), /*< Auto-Speed Detection Enable */
    149         CTRL_SLU = (1 << 6), /**< Set Link Up */
    150         CTRL_ILOS = (1 << 7), /**< Invert Loss-of-Signal */
    151 
    152         CTRL_SPEED_SHIFT = 8, /**< Speed selection shift */
    153         CTRL_SPEED_SIZE = 2, /**< Speed selection size */
    154         CTRL_SPEED_ALL = ((1 << CTRL_SPEED_SIZE) - 1), /**< Speed selection all
    155                                                                                                          *      bit set value
    156                                                                                                          */
    157         CTRL_SPEED_MASK = CTRL_SPEED_ALL << CTRL_SPEED_SHIFT, /**< Speed selection
    158                                                                                                                         * shift
    159                                                                                                                         */
    160         CTRL_SPEED_10 = 0, /**< Speed selection 10 Mb/s value */
    161         CTRL_SPEED_100 = 1, /**< Speed selection 10 Mb/s value */
    162         CTRL_SPEED_1000 = 2, /**< Speed selection 10 Mb/s value */
    163 
    164         CTRL_FRCSPD = (1 << 11), /**< Force Speed */
    165         CTRL_FRCDPLX = (1 << 12), /**< Force Duplex */
    166         CTRL_RST = (1 << 26), /**< Device Reset */
    167         CTRL_VME = (1 << 30), /**< VLAN Mode Enable */
    168         CTRL_PHY_RST = (1 << 31) /**< PHY Reset */
     150        CTRL_FD = (1 << 0),    /**< Full-Duplex */
     151        CTRL_LRST = (1 << 3),  /**< Link Reset */
     152        CTRL_ASDE = (1 << 5),  /**< Auto-Speed Detection Enable */
     153        CTRL_SLU = (1 << 6),   /**< Set Link Up */
     154        CTRL_ILOS = (1 << 7),  /**< Invert Loss-of-Signal */
     155       
     156        /** Speed selection shift */
     157        CTRL_SPEED_SHIFT = 8,
     158        /** Speed selection size */
     159        CTRL_SPEED_SIZE = 2,
     160        /** Speed selection all bit set value */
     161        CTRL_SPEED_ALL = ((1 << CTRL_SPEED_SIZE) - 1),
     162        /** Speed selection shift */
     163        CTRL_SPEED_MASK = CTRL_SPEED_ALL << CTRL_SPEED_SHIFT,
     164        /** Speed selection 10 Mb/s value */
     165        CTRL_SPEED_10 = 0,
     166        /** Speed selection 10 Mb/s value */
     167        CTRL_SPEED_100 = 1,
     168        /** Speed selection 10 Mb/s value */
     169        CTRL_SPEED_1000 = 2,
     170       
     171        CTRL_FRCSPD = (1 << 11),   /**< Force Speed */
     172        CTRL_FRCDPLX = (1 << 12),  /**< Force Duplex */
     173        CTRL_RST = (1 << 26),      /**< Device Reset */
     174        CTRL_VME = (1 << 30),      /**< VLAN Mode Enable */
     175        CTRL_PHY_RST = (1 << 31)   /**< PHY Reset */
    169176};
    170177
    171178/** Device Status Register fields */
    172179enum e1000_status {
    173         STATUS_FD = (1 << 0), /**< Link Full Duplex configuration Indication */
    174         STATUS_LU = (1 << 1), /**< Link Up Indication */
    175 
    176         STATUS_SPEED_SHIFT = 6, /**< Link speed setting shift */
    177         STATUS_SPEED_SIZE = 2, /**< Link speed setting size */
    178         STATUS_SPEED_ALL = ((1 << STATUS_SPEED_SIZE) - 1), /**< Link speed setting
    179                                                                                                                 *       all bits set
    180                                                                                                                 */
    181         STATUS_SPEED_10 = 0, /**< Link speed setting 10 Mb/s value */
    182         STATUS_SPEED_100 = 1, /**< Link speed setting 100 Mb/s value */
    183         STATUS_SPEED_1000A = 2, /**< Link speed setting 1000 Mb/s value variant A */
    184         STATUS_SPEED_1000B = 3, /**< Link speed setting 1000 Mb/s value variant B */
    185 };
    186 
    187 /** Transmit IPG Register fields
     180        STATUS_FD = (1 << 0),  /**< Link Full Duplex configuration Indication */
     181        STATUS_LU = (1 << 1),  /**< Link Up Indication */
     182       
     183        /** Link speed setting shift */
     184        STATUS_SPEED_SHIFT = 6,
     185        /** Link speed setting size */
     186        STATUS_SPEED_SIZE = 2,
     187        /** Link speed setting all bits set */
     188        STATUS_SPEED_ALL = ((1 << STATUS_SPEED_SIZE) - 1),
     189        /** Link speed setting 10 Mb/s value */
     190        STATUS_SPEED_10 = 0,
     191        /** Link speed setting 100 Mb/s value */
     192        STATUS_SPEED_100 = 1,
     193        /** Link speed setting 1000 Mb/s value variant A */
     194        STATUS_SPEED_1000A = 2,
     195        /** Link speed setting 1000 Mb/s value variant B */
     196        STATUS_SPEED_1000B = 3,
     197};
     198
     199/** Transmit IPG Register fields
     200 *
    188201 * IPG = Inter Packet Gap
     202 *
    189203 */
    190204enum e1000_tipg {
    191         TIPG_IPGT_SHIFT = 0, /**< IPG Transmit Time shift */
    192         TIPG_IPGR1_SHIFT = 10, /**< IPG Receive Time 1 */
    193         TIPG_IPGR2_SHIFT = 20 /**< IPG Receive Time 2 */
     205        TIPG_IPGT_SHIFT = 0,    /**< IPG Transmit Time shift */
     206        TIPG_IPGR1_SHIFT = 10,  /**< IPG Receive Time 1 */
     207        TIPG_IPGR2_SHIFT = 20   /**< IPG Receive Time 2 */
    194208};
    195209
    196210/** Transmit Control Register fields */
    197211enum e1000_tctl {
    198         TCTL_EN = (1 << 1), /**< Transmit Enable */
    199         TCTL_PSP =  (1 << 3), /**< Pad Short Packets */
    200         TCTL_CT_SHIFT = 4, /**< Collision Threshold shift */
    201         TCTL_COLD_SHIFT = 12 /**< Collision Distance shift */
     212        TCTL_EN = (1 << 1),    /**< Transmit Enable */
     213        TCTL_PSP =  (1 << 3),  /**< Pad Short Packets */
     214        TCTL_CT_SHIFT = 4,     /**< Collision Threshold shift */
     215        TCTL_COLD_SHIFT = 12   /**< Collision Distance shift */
    202216};
    203217
    204218/** ICR register fields */
    205219enum e1000_icr {
    206         ICR_TXDW = (1 << 0), /**< Transmit Descriptor Written Back */
    207         ICR_RXT0 = (1 << 7) /**< Receiver Timer Interrupt */
     220        ICR_TXDW = (1 << 0),  /**< Transmit Descriptor Written Back */
     221        ICR_RXT0 = (1 << 7)   /**< Receiver Timer Interrupt */
    208222};
    209223
    210224/** RAH register fields */
    211225enum e1000_rah {
    212         RAH_AV = (1 << 31) /**< Address Valid */
     226        RAH_AV = (1 << 31)   /**< Address Valid */
    213227};
    214228
    215229/** RCTL register fields */
    216230enum e1000_rctl {
    217         RCTL_EN = (1 << 1), /**< Receiver Enable */
    218         RCTL_SBP = (1 << 2), /**< Store Bad Packets */
    219         RCTL_UPE = (1 << 3), /**< Unicast Promiscuous Enabled */
    220         RCTL_MPE = (1 << 4), /**< Multicast Promiscuous Enabled */
    221         RCTL_BAM = (1 << 15), /**< Broadcast Accept Mode */
    222         RCTL_VFE = (1 << 18) /**< VLAN Filter Enable */
     231        RCTL_EN = (1 << 1),    /**< Receiver Enable */
     232        RCTL_SBP = (1 << 2),   /**< Store Bad Packets */
     233        RCTL_UPE = (1 << 3),   /**< Unicast Promiscuous Enabled */
     234        RCTL_MPE = (1 << 4),   /**< Multicast Promiscuous Enabled */
     235        RCTL_BAM = (1 << 15),  /**< Broadcast Accept Mode */
     236        RCTL_VFE = (1 << 18)   /**< VLAN Filter Enable */
    223237};
    224238
Note: See TracChangeset for help on using the changeset viewer.