source: mainline/uspace/drv/nic/e1k/e1k.c@ b1213b0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b1213b0 was b1213b0, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Merge internet stack rewrite (IP+ICMP, UDP, IP over Ethernet).

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