HelenOS sources
This source file includes following definitions.
- nic_get_state_impl
- nic_set_state_impl
- nic_send_frame_impl
- nic_callback_create_impl
- nic_get_address_impl
- nic_get_stats_impl
- nic_unicast_get_mode_impl
- nic_unicast_set_mode_impl
- nic_multicast_get_mode_impl
- nic_multicast_set_mode_impl
- nic_broadcast_get_mode_impl
- nic_broadcast_set_mode_impl
- nic_blocked_sources_get_impl
- nic_blocked_sources_set_impl
- nic_vlan_get_mask_impl
- nic_vlan_set_mask_impl
- nic_wol_virtue_add_impl
- nic_wol_virtue_remove_impl
- nic_wol_virtue_probe_impl
- nic_wol_virtue_list_impl
- nic_wol_virtue_get_caps_impl
- nic_poll_get_mode_impl
- nic_poll_set_mode_impl
- nic_poll_now_impl
- nic_default_handler_impl
- nic_open_impl
- nic_close_impl
#include <errno.h>
#include <str_error.h>
#include <ipc/services.h>
#include <ns.h>
#include "nic_driver.h"
#include "nic_ev.h"
#include "nic_impl.h"
errno_t nic_get_state_impl(ddf_fun_t *fun, nic_device_state_t *state)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_read_lock(&nic_data->main_lock);
*state = nic_data->state;
fibril_rwlock_read_unlock(&nic_data->main_lock);
return EOK;
}
errno_t nic_set_state_impl(ddf_fun_t *fun, nic_device_state_t state)
{
if (state >= NIC_STATE_MAX) {
return EINVAL;
}
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_write_lock(&nic_data->main_lock);
if (nic_data->state == state) {
fibril_rwlock_write_unlock(&nic_data->main_lock);
return EOK;
}
if (state == NIC_STATE_ACTIVE) {
if (nic_data->client_session == NULL) {
fibril_rwlock_write_unlock(&nic_data->main_lock);
return EINVAL;
}
}
state_change_handler event_handler = NULL;
switch (state) {
case NIC_STATE_STOPPED:
event_handler = nic_data->on_stopping;
break;
case NIC_STATE_DOWN:
event_handler = nic_data->on_going_down;
break;
case NIC_STATE_ACTIVE:
event_handler = nic_data->on_activating;
break;
default:
break;
}
if (event_handler != NULL) {
errno_t rc = event_handler(nic_data);
if (rc != EOK) {
fibril_rwlock_write_unlock(&nic_data->main_lock);
return EINVAL;
}
}
if (state == NIC_STATE_STOPPED) {
errno_t rc = nic_ev_addr_changed(nic_data->client_session,
&nic_data->default_mac);
nic_data->poll_mode = nic_data->default_poll_mode;
memcpy(&nic_data->poll_period, &nic_data->default_poll_period,
sizeof(struct timespec));
if (rc != EOK) {
}
fibril_rwlock_write_lock(&nic_data->stats_lock);
memset(&nic_data->stats, 0, sizeof(nic_device_stats_t));
fibril_rwlock_write_unlock(&nic_data->stats_lock);
fibril_rwlock_write_lock(&nic_data->rxc_lock);
nic_rxc_clear(&nic_data->rx_control);
nic_rxc_set_addr(&nic_data->rx_control, NULL,
&nic_data->default_mac);
fibril_rwlock_write_unlock(&nic_data->rxc_lock);
memcpy(&nic_data->mac, &nic_data->default_mac, sizeof (nic_address_t));
fibril_rwlock_write_lock(&nic_data->wv_lock);
nic_wol_virtues_clear(&nic_data->wol_virtues);
fibril_rwlock_write_unlock(&nic_data->wv_lock);
nic_sw_period_stop(nic_data);
}
nic_data->state = state;
nic_ev_device_state(nic_data->client_session, state);
fibril_rwlock_write_unlock(&nic_data->main_lock);
return EOK;
}
errno_t nic_send_frame_impl(ddf_fun_t *fun, void *data, size_t size)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_read_lock(&nic_data->main_lock);
if (nic_data->state != NIC_STATE_ACTIVE || nic_data->tx_busy) {
fibril_rwlock_read_unlock(&nic_data->main_lock);
return EBUSY;
}
nic_data->send_frame(nic_data, data, size);
fibril_rwlock_read_unlock(&nic_data->main_lock);
return EOK;
}
errno_t nic_callback_create_impl(ddf_fun_t *fun)
{
nic_t *nic = nic_get_from_ddf_fun(fun);
fibril_rwlock_write_lock(&nic->main_lock);
nic->client_session = async_callback_receive(EXCHANGE_SERIALIZE);
if (nic->client_session == NULL) {
fibril_rwlock_write_unlock(&nic->main_lock);
return ENOMEM;
}
fibril_rwlock_write_unlock(&nic->main_lock);
return EOK;
}
errno_t nic_get_address_impl(ddf_fun_t *fun, nic_address_t *address)
{
assert(address);
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_read_lock(&nic_data->main_lock);
memcpy(address, &nic_data->mac, sizeof (nic_address_t));
fibril_rwlock_read_unlock(&nic_data->main_lock);
return EOK;
}
errno_t nic_get_stats_impl(ddf_fun_t *fun, nic_device_stats_t *stats)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
assert (stats != NULL);
fibril_rwlock_read_lock(&nic_data->stats_lock);
memcpy(stats, &nic_data->stats, sizeof (nic_device_stats_t));
fibril_rwlock_read_unlock(&nic_data->stats_lock);
return EOK;
}
errno_t nic_unicast_get_mode_impl(ddf_fun_t *fun, nic_unicast_mode_t *mode,
size_t max_count, nic_address_t *addr_list, size_t *addr_count)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_read_lock(&nic_data->rxc_lock);
nic_rxc_unicast_get_mode(&nic_data->rx_control, mode, max_count,
addr_list, addr_count);
fibril_rwlock_read_unlock(&nic_data->rxc_lock);
return EOK;
}
errno_t nic_unicast_set_mode_impl(ddf_fun_t *fun,
nic_unicast_mode_t mode, const nic_address_t *addr_list, size_t addr_count)
{
assert((addr_count == 0 && addr_list == NULL) ||
(addr_count != 0 && addr_list != NULL));
size_t i;
for (i = 0; i < addr_count; ++i) {
if (addr_list[i].address[0] & 1)
return EINVAL;
}
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_write_lock(&nic_data->rxc_lock);
errno_t rc = ENOTSUP;
if (nic_data->on_unicast_mode_change) {
rc = nic_data->on_unicast_mode_change(nic_data,
mode, addr_list, addr_count);
}
if (rc == EOK) {
rc = nic_rxc_unicast_set_mode(&nic_data->rx_control, mode,
addr_list, addr_count);
nic_rxc_set_addr(&nic_data->rx_control, NULL, &nic_data->mac);
}
fibril_rwlock_write_unlock(&nic_data->rxc_lock);
return rc;
}
errno_t nic_multicast_get_mode_impl(ddf_fun_t *fun, nic_multicast_mode_t *mode,
size_t max_count, nic_address_t *addr_list, size_t *addr_count)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_read_lock(&nic_data->rxc_lock);
nic_rxc_multicast_get_mode(&nic_data->rx_control, mode, max_count,
addr_list, addr_count);
fibril_rwlock_read_unlock(&nic_data->rxc_lock);
return EOK;
}
errno_t nic_multicast_set_mode_impl(ddf_fun_t *fun, nic_multicast_mode_t mode,
const nic_address_t *addr_list, size_t addr_count)
{
assert((addr_count == 0 && addr_list == NULL) ||
(addr_count != 0 && addr_list != NULL));
size_t i;
for (i = 0; i < addr_count; ++i) {
if (!(addr_list[i].address[0] & 1))
return EINVAL;
}
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_write_lock(&nic_data->rxc_lock);
errno_t rc = ENOTSUP;
if (nic_data->on_multicast_mode_change) {
rc = nic_data->on_multicast_mode_change(nic_data, mode, addr_list, addr_count);
}
if (rc == EOK) {
rc = nic_rxc_multicast_set_mode(&nic_data->rx_control, mode,
addr_list, addr_count);
}
fibril_rwlock_write_unlock(&nic_data->rxc_lock);
return rc;
}
errno_t nic_broadcast_get_mode_impl(ddf_fun_t *fun, nic_broadcast_mode_t *mode)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_read_lock(&nic_data->rxc_lock);
nic_rxc_broadcast_get_mode(&nic_data->rx_control, mode);
fibril_rwlock_read_unlock(&nic_data->rxc_lock);
return EOK;
}
errno_t nic_broadcast_set_mode_impl(ddf_fun_t *fun, nic_broadcast_mode_t mode)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_write_lock(&nic_data->rxc_lock);
errno_t rc = ENOTSUP;
if (nic_data->on_broadcast_mode_change) {
rc = nic_data->on_broadcast_mode_change(nic_data, mode);
}
if (rc == EOK) {
rc = nic_rxc_broadcast_set_mode(&nic_data->rx_control, mode);
}
fibril_rwlock_write_unlock(&nic_data->rxc_lock);
return rc;
}
errno_t nic_blocked_sources_get_impl(ddf_fun_t *fun,
size_t max_count, nic_address_t *addr_list, size_t *addr_count)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_read_lock(&nic_data->rxc_lock);
nic_rxc_blocked_sources_get(&nic_data->rx_control,
max_count, addr_list, addr_count);
fibril_rwlock_read_unlock(&nic_data->rxc_lock);
return EOK;
}
errno_t nic_blocked_sources_set_impl(ddf_fun_t *fun,
const nic_address_t *addr_list, size_t addr_count)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_write_lock(&nic_data->rxc_lock);
if (nic_data->on_blocked_sources_change) {
nic_data->on_blocked_sources_change(nic_data, addr_list, addr_count);
}
errno_t rc = nic_rxc_blocked_sources_set(&nic_data->rx_control,
addr_list, addr_count);
fibril_rwlock_write_unlock(&nic_data->rxc_lock);
return rc;
}
errno_t nic_vlan_get_mask_impl(ddf_fun_t *fun, nic_vlan_mask_t *mask)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_read_lock(&nic_data->rxc_lock);
errno_t rc = nic_rxc_vlan_get_mask(&nic_data->rx_control, mask);
fibril_rwlock_read_unlock(&nic_data->rxc_lock);
return rc;
}
errno_t nic_vlan_set_mask_impl(ddf_fun_t *fun, const nic_vlan_mask_t *mask)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_write_lock(&nic_data->rxc_lock);
errno_t rc = nic_rxc_vlan_set_mask(&nic_data->rx_control, mask);
if (rc == EOK && nic_data->on_vlan_mask_change) {
nic_data->on_vlan_mask_change(nic_data, mask);
}
fibril_rwlock_write_unlock(&nic_data->rxc_lock);
return rc;
}
errno_t nic_wol_virtue_add_impl(ddf_fun_t *fun, nic_wv_type_t type,
const void *data, size_t length, nic_wv_id_t *new_id)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
if (nic_data->on_wol_virtue_add == NULL ||
nic_data->on_wol_virtue_remove == NULL) {
return ENOTSUP;
}
if (type == NIC_WV_NONE || type >= NIC_WV_MAX) {
return EINVAL;
}
if (nic_wol_virtues_verify(type, data, length) != EOK) {
return EINVAL;
}
nic_wol_virtue_t *virtue = malloc(sizeof (nic_wol_virtue_t));
if (virtue == NULL) {
return ENOMEM;
}
memset(virtue, 0, sizeof(nic_wol_virtue_t));
if (length != 0) {
virtue->data = malloc(length);
if (virtue->data == NULL) {
free(virtue);
return ENOMEM;
}
memcpy((void *) virtue->data, data, length);
}
virtue->type = type;
virtue->length = length;
fibril_rwlock_write_lock(&nic_data->wv_lock);
if (nic_data->wol_virtues.caps_max[type] < 0) {
fibril_rwlock_write_unlock(&nic_data->wv_lock);
free(virtue->data);
free(virtue);
return EINVAL;
}
if ((int) nic_data->wol_virtues.lists_sizes[type] >=
nic_data->wol_virtues.caps_max[type]) {
fibril_rwlock_write_unlock(&nic_data->wv_lock);
free(virtue->data);
free(virtue);
return ELIMIT;
}
errno_t rc = nic_data->on_wol_virtue_add(nic_data, virtue);
if (rc != EOK) {
free(virtue->data);
free(virtue);
fibril_rwlock_write_unlock(&nic_data->wv_lock);
return rc;
}
rc = nic_wol_virtues_add(&nic_data->wol_virtues, virtue);
if (rc != EOK) {
nic_data->on_wol_virtue_remove(nic_data, virtue);
fibril_rwlock_write_unlock(&nic_data->wv_lock);
free(virtue->data);
free(virtue);
return rc;
} else {
*new_id = virtue->id;
fibril_rwlock_write_unlock(&nic_data->wv_lock);
}
return EOK;
}
errno_t nic_wol_virtue_remove_impl(ddf_fun_t *fun, nic_wv_id_t id)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
if (nic_data->on_wol_virtue_add == NULL ||
nic_data->on_wol_virtue_remove == NULL) {
return ENOTSUP;
}
fibril_rwlock_write_lock(&nic_data->wv_lock);
nic_wol_virtue_t *virtue =
nic_wol_virtues_remove(&nic_data->wol_virtues, id);
if (virtue == NULL) {
fibril_rwlock_write_unlock(&nic_data->wv_lock);
return ENOENT;
}
nic_data->on_wol_virtue_remove(nic_data, virtue);
fibril_rwlock_write_unlock(&nic_data->wv_lock);
free(virtue->data);
free(virtue);
return EOK;
}
errno_t nic_wol_virtue_probe_impl(ddf_fun_t *fun, nic_wv_id_t id,
nic_wv_type_t *type, size_t max_length, void *data, size_t *length)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_read_lock(&nic_data->wv_lock);
const nic_wol_virtue_t *virtue =
nic_wol_virtues_find(&nic_data->wol_virtues, id);
if (virtue == NULL) {
*type = NIC_WV_NONE;
*length = 0;
fibril_rwlock_read_unlock(&nic_data->wv_lock);
return ENOENT;
} else {
*type = virtue->type;
if (max_length > virtue->length) {
max_length = virtue->length;
}
memcpy(data, virtue->data, max_length);
*length = virtue->length;
fibril_rwlock_read_unlock(&nic_data->wv_lock);
return EOK;
}
}
errno_t nic_wol_virtue_list_impl(ddf_fun_t *fun, nic_wv_type_t type,
size_t max_count, nic_wv_id_t *id_list, size_t *id_count)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_read_lock(&nic_data->wv_lock);
errno_t rc = nic_wol_virtues_list(&nic_data->wol_virtues, type,
max_count, id_list, id_count);
fibril_rwlock_read_unlock(&nic_data->wv_lock);
return rc;
}
errno_t nic_wol_virtue_get_caps_impl(ddf_fun_t *fun, nic_wv_type_t type, int *count)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_read_lock(&nic_data->wv_lock);
*count = nic_data->wol_virtues.caps_max[type] -
(int) nic_data->wol_virtues.lists_sizes[type];
fibril_rwlock_read_unlock(&nic_data->wv_lock);
return EOK;
}
errno_t nic_poll_get_mode_impl(ddf_fun_t *fun,
nic_poll_mode_t *mode, struct timespec *period)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_read_lock(&nic_data->main_lock);
*mode = nic_data->poll_mode;
memcpy(period, &nic_data->poll_period, sizeof(struct timespec));
fibril_rwlock_read_unlock(&nic_data->main_lock);
return EOK;
}
errno_t nic_poll_set_mode_impl(ddf_fun_t *fun,
nic_poll_mode_t mode, const struct timespec *period)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
if (nic_data->on_poll_mode_change == NULL)
return ENOTSUP;
if ((mode == NIC_POLL_ON_DEMAND) && nic_data->on_poll_request == NULL)
return ENOTSUP;
if (mode == NIC_POLL_PERIODIC || mode == NIC_POLL_SOFTWARE_PERIODIC) {
if (period == NULL)
return EINVAL;
if (period->tv_sec == 0 && period->tv_nsec == 0)
return EINVAL;
if (period->tv_sec < 0 || period->tv_nsec < 0)
return EINVAL;
}
fibril_rwlock_write_lock(&nic_data->main_lock);
errno_t rc = nic_data->on_poll_mode_change(nic_data, mode, period);
assert(rc == EOK || rc == ENOTSUP || rc == EINVAL);
if (rc == ENOTSUP && (nic_data->on_poll_request != NULL) &&
(mode == NIC_POLL_PERIODIC || mode == NIC_POLL_SOFTWARE_PERIODIC)) {
rc = nic_data->on_poll_mode_change(nic_data, NIC_POLL_ON_DEMAND, NULL);
assert(rc == EOK || rc == ENOTSUP);
if (rc == EOK)
nic_sw_period_start(nic_data);
}
if (rc == EOK) {
nic_data->poll_mode = mode;
if (period)
nic_data->poll_period = *period;
}
fibril_rwlock_write_unlock(&nic_data->main_lock);
return rc;
}
errno_t nic_poll_now_impl(ddf_fun_t *fun)
{
nic_t *nic_data = nic_get_from_ddf_fun(fun);
fibril_rwlock_read_lock(&nic_data->main_lock);
if (nic_data->poll_mode != NIC_POLL_ON_DEMAND) {
fibril_rwlock_read_unlock(&nic_data->main_lock);
return EINVAL;
}
if (nic_data->on_poll_request != NULL) {
nic_data->on_poll_request(nic_data);
fibril_rwlock_read_unlock(&nic_data->main_lock);
return EOK;
} else {
fibril_rwlock_read_unlock(&nic_data->main_lock);
return ENOTSUP;
}
}
void nic_default_handler_impl(ddf_fun_t *fun, ipc_call_t *call)
{
async_answer_0(call, ENOTSUP);
}
errno_t nic_open_impl(ddf_fun_t *fun)
{
return EOK;
}
void nic_close_impl(ddf_fun_t *fun)
{
}
HelenOS homepage, sources at GitHub