HelenOS sources
This source file includes following definitions.
- nic_driver_init
- nic_driver_implement
- nic_set_send_frame_handler
- nic_set_state_change_handlers
- nic_set_filtering_change_handlers
- nic_set_wol_virtue_change_handlers
- nic_set_poll_handlers
- nic_get_resources
- nic_alloc_frame
- nic_release_frame
- nic_alloc_frame_list
- nic_driver_release_frame_list
- nic_frame_list_append
- nic_query_poll_mode
- nic_report_poll_mode
- nic_report_address
- nic_query_address
- nic_set_tx_busy
- nic_received_frame
- nic_received_frame_list
- nic_create
- nic_create_and_bind
- nic_destroy
- nic_unbind_and_destroy
- nic_report_hw_filtering
- nic_mcast_hash
- nic_query_mcast_hash
- nic_query_unicast
- nic_query_multicast
- nic_query_broadcast
- nic_query_blocked_sources
- nic_query_vlan_mask
- nic_query_wol_max_caps
- nic_set_wol_max_caps
- nic_get_specific
- nic_set_specific
- nic_query_state
- nic_get_ddf_dev
- nic_get_ddf_fun
- nic_set_ddf_fun
- nic_get_from_ddf_dev
- nic_get_from_ddf_fun
- nic_report_send_ok
- nic_report_send_error
- nic_report_receive_error
- nic_report_collisions
- timespec_nonpositive
- period_fibril_fun
- nic_sw_period_start
- nic_sw_period_stop
#include <assert.h>
#include <fibril_synch.h>
#include <ns.h>
#include <stdio.h>
#include <str_error.h>
#include <sysinfo.h>
#include <as.h>
#include <ddf/interrupt.h>
#include <ops/nic.h>
#include <errno.h>
#include "nic_driver.h"
#include "nic_ev.h"
#include "nic_impl.h"
#define NIC_GLOBALS_MAX_CACHE_SIZE 16
nic_globals_t nic_globals;
errno_t nic_driver_init(const char *name)
{
list_initialize(&nic_globals.frame_list_cache);
nic_globals.frame_list_cache_size = 0;
list_initialize(&nic_globals.frame_cache);
nic_globals.frame_cache_size = 0;
fibril_mutex_initialize(&nic_globals.lock);
char buffer[256];
snprintf(buffer, 256, "drv/" DEVICE_CATEGORY_NIC "/%s", name);
return EOK;
}
void nic_driver_implement(driver_ops_t *driver_ops, ddf_dev_ops_t *dev_ops,
nic_iface_t *iface)
{
if (dev_ops) {
if (!dev_ops->open)
dev_ops->open = nic_open_impl;
if (!dev_ops->close)
dev_ops->close = nic_close_impl;
if (!dev_ops->interfaces[NIC_DEV_IFACE])
dev_ops->interfaces[NIC_DEV_IFACE] = iface;
if (!dev_ops->default_handler)
dev_ops->default_handler = nic_default_handler_impl;
}
if (iface) {
if (!iface->get_state)
iface->get_state = nic_get_state_impl;
if (!iface->set_state)
iface->set_state = nic_set_state_impl;
if (!iface->send_frame)
iface->send_frame = nic_send_frame_impl;
if (!iface->callback_create)
iface->callback_create = nic_callback_create_impl;
if (!iface->get_address)
iface->get_address = nic_get_address_impl;
if (!iface->get_stats)
iface->get_stats = nic_get_stats_impl;
if (!iface->unicast_get_mode)
iface->unicast_get_mode = nic_unicast_get_mode_impl;
if (!iface->unicast_set_mode)
iface->unicast_set_mode = nic_unicast_set_mode_impl;
if (!iface->multicast_get_mode)
iface->multicast_get_mode = nic_multicast_get_mode_impl;
if (!iface->multicast_set_mode)
iface->multicast_set_mode = nic_multicast_set_mode_impl;
if (!iface->broadcast_get_mode)
iface->broadcast_get_mode = nic_broadcast_get_mode_impl;
if (!iface->broadcast_set_mode)
iface->broadcast_set_mode = nic_broadcast_set_mode_impl;
if (!iface->blocked_sources_get)
iface->blocked_sources_get = nic_blocked_sources_get_impl;
if (!iface->blocked_sources_set)
iface->blocked_sources_set = nic_blocked_sources_set_impl;
if (!iface->vlan_get_mask)
iface->vlan_get_mask = nic_vlan_get_mask_impl;
if (!iface->vlan_set_mask)
iface->vlan_set_mask = nic_vlan_set_mask_impl;
if (!iface->wol_virtue_add)
iface->wol_virtue_add = nic_wol_virtue_add_impl;
if (!iface->wol_virtue_remove)
iface->wol_virtue_remove = nic_wol_virtue_remove_impl;
if (!iface->wol_virtue_probe)
iface->wol_virtue_probe = nic_wol_virtue_probe_impl;
if (!iface->wol_virtue_list)
iface->wol_virtue_list = nic_wol_virtue_list_impl;
if (!iface->wol_virtue_get_caps)
iface->wol_virtue_get_caps = nic_wol_virtue_get_caps_impl;
if (!iface->poll_get_mode)
iface->poll_get_mode = nic_poll_get_mode_impl;
if (!iface->poll_set_mode)
iface->poll_set_mode = nic_poll_set_mode_impl;
if (!iface->poll_now)
iface->poll_now = nic_poll_now_impl;
}
}
void nic_set_send_frame_handler(nic_t *nic_data, send_frame_handler sffunc)
{
nic_data->send_frame = sffunc;
}
void nic_set_state_change_handlers(nic_t *nic_data,
state_change_handler on_activating, state_change_handler on_going_down,
state_change_handler on_stopping)
{
nic_data->on_activating = on_activating;
nic_data->on_going_down = on_going_down;
nic_data->on_stopping = on_stopping;
}
void nic_set_filtering_change_handlers(nic_t *nic_data,
unicast_mode_change_handler on_unicast_mode_change,
multicast_mode_change_handler on_multicast_mode_change,
broadcast_mode_change_handler on_broadcast_mode_change,
blocked_sources_change_handler on_blocked_sources_change,
vlan_mask_change_handler on_vlan_mask_change)
{
nic_data->on_unicast_mode_change = on_unicast_mode_change;
nic_data->on_multicast_mode_change = on_multicast_mode_change;
nic_data->on_broadcast_mode_change = on_broadcast_mode_change;
nic_data->on_blocked_sources_change = on_blocked_sources_change;
nic_data->on_vlan_mask_change = on_vlan_mask_change;
}
void nic_set_wol_virtue_change_handlers(nic_t *nic_data,
wol_virtue_add_handler on_wv_add, wol_virtue_remove_handler on_wv_remove)
{
assert(on_wv_add != NULL && on_wv_remove != NULL);
nic_data->on_wol_virtue_add = on_wv_add;
nic_data->on_wol_virtue_remove = on_wv_remove;
}
void nic_set_poll_handlers(nic_t *nic_data,
poll_mode_change_handler on_poll_mode_change, poll_request_handler on_poll_req)
{
nic_data->on_poll_mode_change = on_poll_mode_change;
nic_data->on_poll_request = on_poll_req;
}
errno_t nic_get_resources(nic_t *nic_data, hw_res_list_parsed_t *resources)
{
ddf_dev_t *dev = nic_data->dev;
async_sess_t *parent_sess;
parent_sess = ddf_dev_parent_sess_get(dev);
if (parent_sess == NULL)
return EIO;
return hw_res_get_list_parsed(parent_sess, resources, 0);
}
nic_frame_t *nic_alloc_frame(nic_t *nic_data, size_t size)
{
nic_frame_t *frame;
fibril_mutex_lock(&nic_globals.lock);
if (nic_globals.frame_cache_size > 0) {
link_t *first = list_first(&nic_globals.frame_cache);
list_remove(first);
nic_globals.frame_cache_size--;
frame = list_get_instance(first, nic_frame_t, link);
fibril_mutex_unlock(&nic_globals.lock);
} else {
fibril_mutex_unlock(&nic_globals.lock);
frame = malloc(sizeof(nic_frame_t));
if (!frame)
return NULL;
link_initialize(&frame->link);
}
frame->data = malloc(size);
if (frame->data == NULL) {
free(frame);
return NULL;
}
frame->size = size;
return frame;
}
void nic_release_frame(nic_t *nic_data, nic_frame_t *frame)
{
if (!frame)
return;
if (frame->data != NULL) {
free(frame->data);
frame->data = NULL;
frame->size = 0;
}
fibril_mutex_lock(&nic_globals.lock);
if (nic_globals.frame_cache_size >= NIC_GLOBALS_MAX_CACHE_SIZE) {
fibril_mutex_unlock(&nic_globals.lock);
free(frame);
} else {
list_prepend(&frame->link, &nic_globals.frame_cache);
nic_globals.frame_cache_size++;
fibril_mutex_unlock(&nic_globals.lock);
}
}
nic_frame_list_t *nic_alloc_frame_list(void)
{
nic_frame_list_t *frames;
fibril_mutex_lock(&nic_globals.lock);
if (nic_globals.frame_list_cache_size > 0) {
frames =
list_get_instance(list_first(&nic_globals.frame_list_cache),
nic_frame_list_t, head);
list_remove(&frames->head);
list_initialize(frames);
nic_globals.frame_list_cache_size--;
fibril_mutex_unlock(&nic_globals.lock);
} else {
fibril_mutex_unlock(&nic_globals.lock);
frames = malloc(sizeof (nic_frame_list_t));
if (frames != NULL)
list_initialize(frames);
}
return frames;
}
static void nic_driver_release_frame_list(nic_frame_list_t *frames)
{
if (!frames)
return;
fibril_mutex_lock(&nic_globals.lock);
if (nic_globals.frame_list_cache_size >= NIC_GLOBALS_MAX_CACHE_SIZE) {
fibril_mutex_unlock(&nic_globals.lock);
free(frames);
} else {
list_prepend(&frames->head, &nic_globals.frame_list_cache);
nic_globals.frame_list_cache_size++;
fibril_mutex_unlock(&nic_globals.lock);
}
}
void nic_frame_list_append(nic_frame_list_t *frames,
nic_frame_t *frame)
{
assert(frame != NULL && frames != NULL);
list_append(&frame->link, frames);
}
nic_poll_mode_t nic_query_poll_mode(nic_t *nic_data, struct timespec *period)
{
if (period)
*period = nic_data->poll_period;
return nic_data->poll_mode;
}
errno_t nic_report_poll_mode(nic_t *nic_data, nic_poll_mode_t mode,
struct timespec *period)
{
errno_t rc = EOK;
fibril_rwlock_write_lock(&nic_data->main_lock);
nic_data->poll_mode = mode;
nic_data->default_poll_mode = mode;
if (mode == NIC_POLL_PERIODIC) {
if (period) {
memcpy(&nic_data->default_poll_period, period, sizeof(struct timespec));
memcpy(&nic_data->poll_period, period, sizeof(struct timespec));
} else {
rc = EINVAL;
}
}
fibril_rwlock_write_unlock(&nic_data->main_lock);
return rc;
}
errno_t nic_report_address(nic_t *nic_data, const nic_address_t *address)
{
assert(nic_data);
if (address->address[0] & 1)
return EINVAL;
fibril_rwlock_write_lock(&nic_data->main_lock);
if (nic_data->client_session != NULL) {
errno_t rc = nic_ev_addr_changed(nic_data->client_session,
address);
if (rc != EOK) {
fibril_rwlock_write_unlock(&nic_data->main_lock);
return rc;
}
}
fibril_rwlock_write_lock(&nic_data->rxc_lock);
errno_t rc = nic_rxc_set_addr(&nic_data->rx_control,
&nic_data->mac, address);
if (MAC_IS_ZERO(nic_data->default_mac.address)) {
assert(MAC_IS_ZERO(nic_data->mac.address));
memcpy(&nic_data->default_mac, address, sizeof(nic_address_t));
}
fibril_rwlock_write_unlock(&nic_data->rxc_lock);
if ((rc != EOK) && (rc != ENOENT)) {
fibril_rwlock_write_unlock(&nic_data->main_lock);
return rc;
}
memcpy(&nic_data->mac, address, sizeof(nic_address_t));
fibril_rwlock_write_unlock(&nic_data->main_lock);
return EOK;
}
void nic_query_address(nic_t *nic_data, nic_address_t *addr)
{
if (!addr)
return;
memcpy(addr, &nic_data->mac, sizeof(nic_address_t));
}
void nic_set_tx_busy(nic_t *nic_data, int busy)
{
nic_data->tx_busy = busy;
}
void nic_received_frame(nic_t *nic_data, nic_frame_t *frame)
{
fibril_rwlock_read_lock(&nic_data->rxc_lock);
nic_frame_type_t frame_type;
bool check = nic_rxc_check(&nic_data->rx_control, frame->data,
frame->size, &frame_type);
fibril_rwlock_read_unlock(&nic_data->rxc_lock);
fibril_rwlock_write_lock(&nic_data->stats_lock);
if (nic_data->state == NIC_STATE_ACTIVE && check) {
nic_data->stats.receive_packets++;
nic_data->stats.receive_bytes += frame->size;
switch (frame_type) {
case NIC_FRAME_MULTICAST:
nic_data->stats.receive_multicast++;
break;
case NIC_FRAME_BROADCAST:
nic_data->stats.receive_broadcast++;
break;
default:
break;
}
fibril_rwlock_write_unlock(&nic_data->stats_lock);
nic_ev_received(nic_data->client_session, frame->data,
frame->size);
} else {
switch (frame_type) {
case NIC_FRAME_UNICAST:
nic_data->stats.receive_filtered_unicast++;
break;
case NIC_FRAME_MULTICAST:
nic_data->stats.receive_filtered_multicast++;
break;
case NIC_FRAME_BROADCAST:
nic_data->stats.receive_filtered_broadcast++;
break;
}
fibril_rwlock_write_unlock(&nic_data->stats_lock);
}
nic_release_frame(nic_data, frame);
}
void nic_received_frame_list(nic_t *nic_data, nic_frame_list_t *frames)
{
if (frames == NULL)
return;
while (!list_empty(frames)) {
nic_frame_t *frame =
list_get_instance(list_first(frames), nic_frame_t, link);
list_remove(&frame->link);
nic_received_frame(nic_data, frame);
}
nic_driver_release_frame_list(frames);
}
static nic_t *nic_create(ddf_dev_t *dev)
{
nic_t *nic_data = ddf_dev_data_alloc(dev, sizeof(nic_t));
if (nic_data == NULL)
return NULL;
if (nic_rxc_init(&nic_data->rx_control) != EOK) {
return NULL;
}
if (nic_wol_virtues_init(&nic_data->wol_virtues) != EOK) {
return NULL;
}
nic_data->dev = NULL;
nic_data->fun = NULL;
nic_data->state = NIC_STATE_STOPPED;
nic_data->client_session = NULL;
nic_data->poll_mode = NIC_POLL_IMMEDIATE;
nic_data->default_poll_mode = NIC_POLL_IMMEDIATE;
nic_data->send_frame = NULL;
nic_data->on_activating = NULL;
nic_data->on_going_down = NULL;
nic_data->on_stopping = NULL;
nic_data->specific = NULL;
fibril_rwlock_initialize(&nic_data->main_lock);
fibril_rwlock_initialize(&nic_data->stats_lock);
fibril_rwlock_initialize(&nic_data->rxc_lock);
fibril_rwlock_initialize(&nic_data->wv_lock);
memset(&nic_data->mac, 0, sizeof(nic_address_t));
memset(&nic_data->default_mac, 0, sizeof(nic_address_t));
memset(&nic_data->stats, 0, sizeof(nic_device_stats_t));
return nic_data;
}
nic_t *nic_create_and_bind(ddf_dev_t *device)
{
nic_t *nic_data = nic_create(device);
if (!nic_data)
return NULL;
nic_data->dev = device;
return nic_data;
}
static void nic_destroy(nic_t *nic_data)
{
free(nic_data->specific);
}
void nic_unbind_and_destroy(ddf_dev_t *device)
{
nic_destroy(nic_get_from_ddf_dev(device));
return;
}
void nic_report_hw_filtering(nic_t *nic_data,
int unicast_exact, int multicast_exact, int vlan_exact)
{
nic_rxc_hw_filtering(&nic_data->rx_control,
unicast_exact, multicast_exact, vlan_exact);
}
uint64_t nic_mcast_hash(const nic_address_t *list, size_t count)
{
return nic_rxc_mcast_hash(list, count);
}
uint64_t nic_query_mcast_hash(nic_t *nic_data)
{
fibril_rwlock_read_lock(&nic_data->rxc_lock);
uint64_t hash = nic_rxc_multicast_get_hash(&nic_data->rx_control);
fibril_rwlock_read_unlock(&nic_data->rxc_lock);
return hash;
}
void nic_query_unicast(const nic_t *nic_data,
nic_unicast_mode_t *mode,
size_t max_count, nic_address_t *address_list, size_t *address_count)
{
assert(mode != NULL);
nic_rxc_unicast_get_mode(&nic_data->rx_control, mode,
max_count, address_list, address_count);
}
void nic_query_multicast(const nic_t *nic_data,
nic_multicast_mode_t *mode,
size_t max_count, nic_address_t *address_list, size_t *address_count)
{
assert(mode != NULL);
nic_rxc_multicast_get_mode(&nic_data->rx_control, mode,
max_count, address_list, address_count);
}
void nic_query_broadcast(const nic_t *nic_data,
nic_broadcast_mode_t *mode)
{
assert(mode != NULL);
nic_rxc_broadcast_get_mode(&nic_data->rx_control, mode);
}
void nic_query_blocked_sources(const nic_t *nic_data,
size_t max_count, nic_address_t *address_list, size_t *address_count)
{
nic_rxc_blocked_sources_get(&nic_data->rx_control,
max_count, address_list, address_count);
}
errno_t nic_query_vlan_mask(const nic_t *nic_data, nic_vlan_mask_t *mask)
{
assert(mask);
return nic_rxc_vlan_get_mask(&nic_data->rx_control, mask);
}
int nic_query_wol_max_caps(const nic_t *nic_data, nic_wv_type_t type)
{
return nic_data->wol_virtues.caps_max[type];
}
void nic_set_wol_max_caps(nic_t *nic_data, nic_wv_type_t type, int count)
{
nic_data->wol_virtues.caps_max[type] = count;
}
void *nic_get_specific(nic_t *nic_data)
{
return nic_data->specific;
}
void nic_set_specific(nic_t *nic_data, void *specific)
{
nic_data->specific = specific;
}
nic_device_state_t nic_query_state(nic_t *nic_data)
{
return nic_data->state;
}
ddf_dev_t *nic_get_ddf_dev(nic_t *nic_data)
{
return nic_data->dev;
}
ddf_fun_t *nic_get_ddf_fun(nic_t *nic_data)
{
return nic_data->fun;
}
void nic_set_ddf_fun(nic_t *nic_data, ddf_fun_t *fun)
{
nic_data->fun = fun;
}
nic_t *nic_get_from_ddf_dev(ddf_dev_t *dev)
{
return (nic_t *) ddf_dev_data_get(dev);
}
nic_t *nic_get_from_ddf_fun(ddf_fun_t *fun)
{
return (nic_t *) ddf_dev_data_get(ddf_fun_get_dev(fun));
}
void nic_report_send_ok(nic_t *nic_data, size_t packets, size_t bytes)
{
fibril_rwlock_write_lock(&nic_data->stats_lock);
nic_data->stats.send_packets += packets;
nic_data->stats.send_bytes += bytes;
fibril_rwlock_write_unlock(&nic_data->stats_lock);
}
void nic_report_send_error(nic_t *nic_data, nic_send_error_cause_t cause,
unsigned count)
{
if (count == 0)
return;
fibril_rwlock_write_lock(&nic_data->stats_lock);
nic_data->stats.send_errors += count;
switch (cause) {
case NIC_SEC_BUFFER_FULL:
nic_data->stats.send_dropped += count;
break;
case NIC_SEC_ABORTED:
nic_data->stats.send_aborted_errors += count;
break;
case NIC_SEC_CARRIER_LOST:
nic_data->stats.send_carrier_errors += count;
break;
case NIC_SEC_FIFO_OVERRUN:
nic_data->stats.send_fifo_errors += count;
break;
case NIC_SEC_HEARTBEAT:
nic_data->stats.send_heartbeat_errors += count;
break;
case NIC_SEC_WINDOW_ERROR:
nic_data->stats.send_window_errors += count;
break;
case NIC_SEC_OTHER:
break;
}
fibril_rwlock_write_unlock(&nic_data->stats_lock);
}
void nic_report_receive_error(nic_t *nic_data,
nic_receive_error_cause_t cause, unsigned count)
{
fibril_rwlock_write_lock(&nic_data->stats_lock);
nic_data->stats.receive_errors += count;
switch (cause) {
case NIC_REC_BUFFER_FULL:
nic_data->stats.receive_dropped += count;
break;
case NIC_REC_LENGTH:
nic_data->stats.receive_length_errors += count;
break;
case NIC_REC_BUFFER_OVERFLOW:
nic_data->stats.receive_dropped += count;
break;
case NIC_REC_CRC:
nic_data->stats.receive_crc_errors += count;
break;
case NIC_REC_FRAME_ALIGNMENT:
nic_data->stats.receive_frame_errors += count;
break;
case NIC_REC_FIFO_OVERRUN:
nic_data->stats.receive_fifo_errors += count;
break;
case NIC_REC_MISSED:
nic_data->stats.receive_missed_errors += count;
break;
case NIC_REC_OTHER:
break;
}
fibril_rwlock_write_unlock(&nic_data->stats_lock);
}
void nic_report_collisions(nic_t *nic_data, unsigned count)
{
fibril_rwlock_write_lock(&nic_data->stats_lock);
nic_data->stats.collisions += count;
fibril_rwlock_write_unlock(&nic_data->stats_lock);
}
static int timespec_nonpositive(struct timespec t)
{
return (t.tv_sec <= 0) && (t.tv_nsec <= 0);
}
static errno_t period_fibril_fun(void *data)
{
nic_t *nic = data;
struct sw_poll_info *info = &nic->sw_poll_info;
while (true) {
fibril_rwlock_read_lock(&nic->main_lock);
int run = info->run;
int running = info->running;
struct timespec remaining = nic->poll_period;
fibril_rwlock_read_unlock(&nic->main_lock);
if (!running) {
remaining.tv_sec = 5;
remaining.tv_nsec = 0;
}
while (!timespec_nonpositive(remaining)) {
usec_t wait = 0;
if (remaining.tv_sec > 0) {
time_t wait_sec = remaining.tv_sec;
if (wait_sec > 5)
wait_sec = 5;
wait = SEC2USEC(wait_sec);
remaining.tv_sec -= wait_sec;
} else {
wait = NSEC2USEC(remaining.tv_nsec);
if (wait > 5 * 1000000) {
wait = 5 * 1000000;
}
remaining.tv_nsec -= USEC2NSEC(wait);
}
fibril_usleep(wait);
if (info->run != run)
break;
}
fibril_rwlock_read_lock(&nic->main_lock);
if (info->running && info->run == run) {
nic->on_poll_request(nic);
}
fibril_rwlock_read_unlock(&nic->main_lock);
}
return EOK;
}
void nic_sw_period_start(nic_t *nic_data)
{
if (nic_data->sw_poll_info.fibril == 0) {
nic_data->sw_poll_info.fibril = fibril_create(period_fibril_fun,
nic_data);
nic_data->sw_poll_info.running = 0;
nic_data->sw_poll_info.run = 0;
fibril_add_ready(nic_data->sw_poll_info.fibril);
}
nic_data->sw_poll_info.run = (nic_data->sw_poll_info.run + 1) % 100;
nic_data->sw_poll_info.running = 1;
}
void nic_sw_period_stop(nic_t *nic_data)
{
nic_data->sw_poll_info.running = 0;
}
HelenOS homepage, sources at GitHub