source: mainline/uspace/drv/nic/e1k/e1k.c@ 582a0b8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 582a0b8 was 582a0b8, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove unistd.h

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