HelenOS sources
This source file includes following definitions.
- pio_read_buf_16
- pio_write_buf_16
- ne2k_download
- ne2k_upload
- ne2k_init
- ne2k_probe
- ne2k_set_physical_address
- ne2k_up
- ne2k_down
- ne2k_reset
- ne2k_send
- ne2k_receive_frame
- ne2k_receive
- ne2k_interrupt
- ne2k_set_accept_bcast
- ne2k_set_accept_mcast
- ne2k_set_promisc_phys
- ne2k_set_mcast_hash
#include <assert.h>
#include <async.h>
#include <byteorder.h>
#include <errno.h>
#include <stdio.h>
#include <ddi.h>
#include "dp8390.h"
#define DP_PAGE 256
#define SQ_PAGES 6
typedef struct {
uint8_t status;
uint8_t next;
uint8_t rbcl;
uint8_t rbch;
} recv_header_t;
static void pio_read_buf_16(void *port, void *buf, size_t size)
{
size_t i;
for (i = 0; (i << 1) < size; i++)
*((uint16_t *) buf + i) = pio_read_16((ioport16_t *) (port));
}
static void pio_write_buf_16(void *port, void *buf, size_t size)
{
size_t i;
for (i = 0; (i << 1) < size; i++)
pio_write_16((ioport16_t *) port, *((uint16_t *) buf + i));
}
static void ne2k_download(ne2k_t *ne2k, void *buf, size_t addr, size_t size)
{
size_t esize = size & ~1;
pio_write_8(ne2k->port + DP_RBCR0, esize & 0xff);
pio_write_8(ne2k->port + DP_RBCR1, (esize >> 8) & 0xff);
pio_write_8(ne2k->port + DP_RSAR0, addr & 0xff);
pio_write_8(ne2k->port + DP_RSAR1, (addr >> 8) & 0xff);
pio_write_8(ne2k->port + DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
if (esize != 0) {
pio_read_buf_16(ne2k->data_port, buf, esize);
size -= esize;
buf += esize;
}
if (size) {
assert(size == 1);
uint16_t word = pio_read_16(ne2k->data_port);
memcpy(buf, &word, 1);
}
}
static void ne2k_upload(ne2k_t *ne2k, void *buf, size_t addr, size_t size)
{
size_t esize_ru = (size + 1) & ~1;
size_t esize = size & ~1;
pio_write_8(ne2k->port + DP_RBCR0, esize_ru & 0xff);
pio_write_8(ne2k->port + DP_RBCR1, (esize_ru >> 8) & 0xff);
pio_write_8(ne2k->port + DP_RSAR0, addr & 0xff);
pio_write_8(ne2k->port + DP_RSAR1, (addr >> 8) & 0xff);
pio_write_8(ne2k->port + DP_CR, CR_DM_RW | CR_PS_P0 | CR_STA);
if (esize != 0) {
pio_write_buf_16(ne2k->data_port, buf, esize);
size -= esize;
buf += esize;
}
if (size) {
assert(size == 1);
uint16_t word = 0;
memcpy(&word, buf, 1);
pio_write_16(ne2k->data_port, word);
}
}
static void ne2k_init(ne2k_t *ne2k)
{
unsigned int i;
uint8_t val = pio_read_8(ne2k->port + NE2K_RESET);
fibril_usleep(2000);
pio_write_8(ne2k->port + NE2K_RESET, val);
fibril_usleep(2000);
pio_write_8(ne2k->port + DP_CR, CR_STP | CR_DM_ABORT);
for (i = 0; i < NE2K_RETRY; i++) {
if (pio_read_8(ne2k->port + DP_ISR) != 0)
break;
}
}
errno_t ne2k_probe(ne2k_t *ne2k)
{
unsigned int i;
ne2k_init(ne2k);
uint8_t val = pio_read_8(ne2k->port + DP_CR);
if ((val & (CR_STP | CR_TXP | CR_DM_ABORT)) != (CR_STP | CR_DM_ABORT))
return EXDEV;
pio_write_8(ne2k->port + DP_RCR, RCR_MON);
pio_write_8(ne2k->port + DP_TCR, TCR_NORMAL);
pio_write_8(ne2k->port + DP_DCR, DCR_WORDWIDE | DCR_8BYTES | DCR_BMS);
pio_write_8(ne2k->port + DP_RBCR0, ETH_ADDR << 1);
pio_write_8(ne2k->port + DP_RBCR1, 0);
pio_write_8(ne2k->port + DP_RSAR0, 0);
pio_write_8(ne2k->port + DP_RSAR1, 0);
pio_write_8(ne2k->port + DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
for (i = 0; i < ETH_ADDR; i++)
ne2k->mac.address[i] = pio_read_16(ne2k->data_port);
return EOK;
}
void ne2k_set_physical_address(ne2k_t *ne2k, const nic_address_t *address)
{
memcpy(&ne2k->mac, address, sizeof(nic_address_t));
pio_write_8(ne2k->port + DP_CR, CR_PS_P0 | CR_DM_ABORT | CR_STP);
pio_write_8(ne2k->port + DP_RBCR0, ETH_ADDR << 1);
pio_write_8(ne2k->port + DP_RBCR1, 0);
pio_write_8(ne2k->port + DP_RSAR0, 0);
pio_write_8(ne2k->port + DP_RSAR1, 0);
pio_write_8(ne2k->port + DP_CR, CR_DM_RW | CR_PS_P0 | CR_STA);
size_t i;
for (i = 0; i < ETH_ADDR; i++)
pio_write_16(ne2k->data_port, ne2k->mac.address[i]);
}
errno_t ne2k_up(ne2k_t *ne2k)
{
if (!ne2k->probed)
return EXDEV;
ne2k_init(ne2k);
ne2k->sq.dirty = false;
ne2k->sq.page = NE2K_START / DP_PAGE;
fibril_mutex_initialize(&ne2k->sq_mutex);
fibril_condvar_initialize(&ne2k->sq_cv);
ne2k->start_page = ne2k->sq.page + SQ_PAGES;
ne2k->stop_page = ne2k->sq.page + NE2K_SIZE / DP_PAGE;
pio_write_8(ne2k->port + DP_CR, CR_PS_P0 | CR_STP | CR_DM_ABORT);
pio_write_8(ne2k->port + DP_DCR, DCR_WORDWIDE | DCR_8BYTES | DCR_BMS);
pio_write_8(ne2k->port + DP_RBCR0, 0);
pio_write_8(ne2k->port + DP_RBCR1, 0);
pio_write_8(ne2k->port + DP_RCR, ne2k->receive_configuration);
pio_write_8(ne2k->port + DP_TCR, TCR_INTERNAL);
pio_write_8(ne2k->port + DP_BNRY, ne2k->start_page);
pio_write_8(ne2k->port + DP_PSTART, ne2k->start_page);
pio_write_8(ne2k->port + DP_PSTOP, ne2k->stop_page);
pio_write_8(ne2k->port + DP_ISR, 0xff);
pio_write_8(ne2k->port + DP_IMR,
IMR_PRXE | IMR_PTXE | IMR_RXEE | IMR_TXEE | IMR_OVWE | IMR_CNTE);
pio_write_8(ne2k->port + DP_CR, CR_PS_P1 | CR_DM_ABORT | CR_STP);
pio_write_8(ne2k->port + DP_PAR0, ne2k->mac.address[0]);
pio_write_8(ne2k->port + DP_PAR1, ne2k->mac.address[1]);
pio_write_8(ne2k->port + DP_PAR2, ne2k->mac.address[2]);
pio_write_8(ne2k->port + DP_PAR3, ne2k->mac.address[3]);
pio_write_8(ne2k->port + DP_PAR4, ne2k->mac.address[4]);
pio_write_8(ne2k->port + DP_PAR5, ne2k->mac.address[5]);
pio_write_8(ne2k->port + DP_MAR0, 0);
pio_write_8(ne2k->port + DP_MAR1, 0);
pio_write_8(ne2k->port + DP_MAR2, 0);
pio_write_8(ne2k->port + DP_MAR3, 0);
pio_write_8(ne2k->port + DP_MAR4, 0);
pio_write_8(ne2k->port + DP_MAR5, 0);
pio_write_8(ne2k->port + DP_MAR6, 0);
pio_write_8(ne2k->port + DP_MAR7, 0);
pio_write_8(ne2k->port + DP_CURR, ne2k->start_page + 1);
pio_write_8(ne2k->port + DP_CR, CR_PS_P0 | CR_DM_ABORT | CR_STA);
pio_write_8(ne2k->port + DP_TCR, TCR_NORMAL);
pio_read_8(ne2k->port + DP_CNTR0);
pio_read_8(ne2k->port + DP_CNTR1);
pio_read_8(ne2k->port + DP_CNTR2);
ne2k->up = true;
return EOK;
}
void ne2k_down(ne2k_t *ne2k)
{
if ((ne2k->probed) && (ne2k->up)) {
pio_write_8(ne2k->port + DP_CR, CR_STP | CR_DM_ABORT);
ne2k_init(ne2k);
ne2k->up = false;
}
}
static void ne2k_reset(ne2k_t *ne2k)
{
unsigned int i;
fibril_mutex_lock(&ne2k->sq_mutex);
pio_write_8(ne2k->port + DP_CR, CR_STP | CR_DM_ABORT);
pio_write_8(ne2k->port + DP_RBCR0, 0);
pio_write_8(ne2k->port + DP_RBCR1, 0);
for (i = 0; i < NE2K_RETRY; i++) {
if ((pio_read_8(ne2k->port + DP_ISR) & ISR_RST) != 0)
break;
}
pio_write_8(ne2k->port + DP_TCR, TCR_1EXTERNAL | TCR_OFST);
pio_write_8(ne2k->port + DP_CR, CR_STA | CR_DM_ABORT);
pio_write_8(ne2k->port + DP_TCR, TCR_NORMAL);
for (i = 0; i < NE2K_RETRY; i++) {
if ((pio_read_8(ne2k->port + DP_ISR) & ISR_RDC) != 0)
break;
}
uint8_t val = pio_read_8(ne2k->port + DP_ISR);
pio_write_8(ne2k->port + DP_ISR, val & ~ISR_RDC);
ne2k->sq.dirty = false;
fibril_mutex_unlock(&ne2k->sq_mutex);
}
void ne2k_send(nic_t *nic_data, void *data, size_t size)
{
ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
assert(ne2k->probed);
assert(ne2k->up);
fibril_mutex_lock(&ne2k->sq_mutex);
while (ne2k->sq.dirty) {
fibril_condvar_wait(&ne2k->sq_cv, &ne2k->sq_mutex);
}
if ((size < ETH_MIN_PACK_SIZE) || (size > ETH_MAX_PACK_SIZE_TAGGED)) {
fibril_mutex_unlock(&ne2k->sq_mutex);
return;
}
ne2k_upload(ne2k, data, ne2k->sq.page * DP_PAGE, size);
ne2k->sq.dirty = true;
ne2k->sq.size = size;
pio_write_8(ne2k->port + DP_TPSR, ne2k->sq.page);
pio_write_8(ne2k->port + DP_TBCR0, size & 0xff);
pio_write_8(ne2k->port + DP_TBCR1, (size >> 8) & 0xff);
pio_write_8(ne2k->port + DP_CR, CR_TXP | CR_STA);
fibril_mutex_unlock(&ne2k->sq_mutex);
}
static nic_frame_t *ne2k_receive_frame(nic_t *nic_data, uint8_t page,
size_t length)
{
ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
nic_frame_t *frame = nic_alloc_frame(nic_data, length);
if (frame == NULL)
return NULL;
memset(frame->data, 0, length);
uint8_t last = page + length / DP_PAGE;
if (last >= ne2k->stop_page) {
size_t left = (ne2k->stop_page - page) * DP_PAGE -
sizeof(recv_header_t);
ne2k_download(ne2k, frame->data, page * DP_PAGE + sizeof(recv_header_t),
left);
ne2k_download(ne2k, frame->data + left, ne2k->start_page * DP_PAGE,
length - left);
} else {
ne2k_download(ne2k, frame->data, page * DP_PAGE + sizeof(recv_header_t),
length);
}
return frame;
}
static void ne2k_receive(nic_t *nic_data)
{
ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
nic_frame_list_t *frames = nic_alloc_frame_list();
size_t frames_count = 0;
while (frames_count < 16) {
uint8_t boundary = pio_read_8(ne2k->port + DP_BNRY) + 1;
if (boundary == ne2k->stop_page)
boundary = ne2k->start_page;
pio_write_8(ne2k->port + DP_CR, CR_PS_P1 | CR_STA);
uint8_t current = pio_read_8(ne2k->port + DP_CURR);
pio_write_8(ne2k->port + DP_CR, CR_PS_P0 | CR_STA);
if (current == boundary)
break;
recv_header_t header;
size_t size = sizeof(header);
size_t offset = boundary * DP_PAGE;
pio_write_8(ne2k->port + DP_RBCR0, size & 0xff);
pio_write_8(ne2k->port + DP_RBCR1, (size >> 8) & 0xff);
pio_write_8(ne2k->port + DP_RSAR0, offset & 0xff);
pio_write_8(ne2k->port + DP_RSAR1, (offset >> 8) & 0xff);
pio_write_8(ne2k->port + DP_CR, CR_DM_RR | CR_PS_P0 | CR_STA);
pio_read_buf_16(ne2k->data_port, (void *) &header, size);
size_t length =
(((size_t) header.rbcl) | (((size_t) header.rbch) << 8)) - size;
uint8_t next = header.next;
if ((length < ETH_MIN_PACK_SIZE) ||
(length > ETH_MAX_PACK_SIZE_TAGGED)) {
next = current;
} else if ((header.next < ne2k->start_page) ||
(header.next > ne2k->stop_page)) {
next = current;
} else if (header.status & RSR_FO) {
ne2k->overruns++;
next = current;
} else if ((header.status & RSR_PRX) && (ne2k->up)) {
if (frames != NULL) {
nic_frame_t *frame =
ne2k_receive_frame(nic_data, boundary, length);
if (frame != NULL) {
nic_frame_list_append(frames, frame);
frames_count++;
} else {
break;
}
} else
break;
}
if (next == ne2k->start_page)
next = ne2k->stop_page - 1;
else
next--;
pio_write_8(ne2k->port + DP_BNRY, next);
}
nic_received_frame_list(nic_data, frames);
}
void ne2k_interrupt(nic_t *nic_data, uint8_t isr, uint8_t tsr)
{
ne2k_t *ne2k = (ne2k_t *) nic_get_specific(nic_data);
if (isr & (ISR_PTX | ISR_TXE)) {
if (tsr & TSR_COL) {
nic_report_collisions(nic_data,
pio_read_8(ne2k->port + DP_NCR) & 15);
}
if (tsr & TSR_PTX) {
nic_report_send_ok(nic_data, 1, 0);
} else if (tsr & TSR_ABT) {
nic_report_send_error(nic_data, NIC_SEC_ABORTED, 1);
} else if (tsr & TSR_CRS) {
nic_report_send_error(nic_data, NIC_SEC_CARRIER_LOST, 1);
} else if (tsr & TSR_FU) {
ne2k->underruns++;
} else if (tsr & TSR_CDH) {
nic_report_send_error(nic_data, NIC_SEC_HEARTBEAT, 1);
} else if (tsr & TSR_OWC) {
nic_report_send_error(nic_data, NIC_SEC_WINDOW_ERROR, 1);
}
fibril_mutex_lock(&ne2k->sq_mutex);
if (ne2k->sq.dirty) {
ne2k->sq.dirty = false;
ne2k->sq.size = 0;
fibril_condvar_broadcast(&ne2k->sq_cv);
} else {
ne2k->misses++;
}
fibril_mutex_unlock(&ne2k->sq_mutex);
}
if (isr & ISR_CNT) {
unsigned int errors;
for (errors = pio_read_8(ne2k->port + DP_CNTR0); errors > 0; --errors)
nic_report_receive_error(nic_data, NIC_REC_CRC, 1);
for (errors = pio_read_8(ne2k->port + DP_CNTR1); errors > 0; --errors)
nic_report_receive_error(nic_data, NIC_REC_FRAME_ALIGNMENT, 1);
for (errors = pio_read_8(ne2k->port + DP_CNTR2); errors > 0; --errors)
nic_report_receive_error(nic_data, NIC_REC_MISSED, 1);
}
if (isr & ISR_PRX) {
ne2k_receive(nic_data);
}
if (isr & ISR_RST) {
ne2k_reset(ne2k);
}
pio_write_8(ne2k->port + DP_IMR,
IMR_PRXE | IMR_PTXE | IMR_RXEE | IMR_TXEE | IMR_OVWE | IMR_CNTE);
}
void ne2k_set_accept_bcast(ne2k_t *ne2k, int accept)
{
if (accept)
ne2k->receive_configuration |= RCR_AB;
else
ne2k->receive_configuration &= ~RCR_AB;
pio_write_8(ne2k->port + DP_RCR, ne2k->receive_configuration);
}
void ne2k_set_accept_mcast(ne2k_t *ne2k, int accept)
{
if (accept)
ne2k->receive_configuration |= RCR_AM;
else
ne2k->receive_configuration &= ~RCR_AM;
pio_write_8(ne2k->port + DP_RCR, ne2k->receive_configuration);
}
void ne2k_set_promisc_phys(ne2k_t *ne2k, int promisc)
{
if (promisc)
ne2k->receive_configuration |= RCR_PRO;
else
ne2k->receive_configuration &= ~RCR_PRO;
pio_write_8(ne2k->port + DP_RCR, ne2k->receive_configuration);
}
void ne2k_set_mcast_hash(ne2k_t *ne2k, uint64_t hash)
{
pio_write_8(ne2k->port + DP_CR, CR_PS_P1 | CR_DM_ABORT | CR_STP);
pio_write_8(ne2k->port + DP_MAR0, (uint8_t) hash);
pio_write_8(ne2k->port + DP_MAR1, (uint8_t) (hash >> 8));
pio_write_8(ne2k->port + DP_MAR2, (uint8_t) (hash >> 16));
pio_write_8(ne2k->port + DP_MAR3, (uint8_t) (hash >> 24));
pio_write_8(ne2k->port + DP_MAR4, (uint8_t) (hash >> 32));
pio_write_8(ne2k->port + DP_MAR5, (uint8_t) (hash >> 40));
pio_write_8(ne2k->port + DP_MAR6, (uint8_t) (hash >> 48));
pio_write_8(ne2k->port + DP_MAR7, (uint8_t) (hash >> 56));
pio_write_8(ne2k->port + DP_CR, CR_PS_P0 | CR_DM_ABORT | CR_STA);
}
HelenOS homepage, sources at GitHub