| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Radim Vansa
|
|---|
| 3 | * Copyright (c) 2011 Jiri Michalec
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * @addtogroup libnic
|
|---|
| 32 | * @{
|
|---|
| 33 | */
|
|---|
| 34 | /**
|
|---|
| 35 | * @file
|
|---|
| 36 | * @brief Internal implementation of general NIC operations
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include <assert.h>
|
|---|
| 40 | #include <fibril_synch.h>
|
|---|
| 41 | #include <ns.h>
|
|---|
| 42 | #include <stdio.h>
|
|---|
| 43 | #include <str_error.h>
|
|---|
| 44 | #include <sysinfo.h>
|
|---|
| 45 | #include <as.h>
|
|---|
| 46 | #include <ddf/interrupt.h>
|
|---|
| 47 | #include <ops/nic.h>
|
|---|
| 48 | #include <errno.h>
|
|---|
| 49 |
|
|---|
| 50 | #include "nic_driver.h"
|
|---|
| 51 | #include "nic_ev.h"
|
|---|
| 52 | #include "nic_impl.h"
|
|---|
| 53 |
|
|---|
| 54 | #define NIC_GLOBALS_MAX_CACHE_SIZE 16
|
|---|
| 55 |
|
|---|
| 56 | nic_globals_t nic_globals;
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | * Initializes libraries required for NIC framework - logger
|
|---|
| 60 | *
|
|---|
| 61 | * @param name Name of the device/driver (used in logging)
|
|---|
| 62 | */
|
|---|
| 63 | errno_t nic_driver_init(const char *name)
|
|---|
| 64 | {
|
|---|
| 65 | list_initialize(&nic_globals.frame_list_cache);
|
|---|
| 66 | nic_globals.frame_list_cache_size = 0;
|
|---|
| 67 | list_initialize(&nic_globals.frame_cache);
|
|---|
| 68 | nic_globals.frame_cache_size = 0;
|
|---|
| 69 | fibril_mutex_initialize(&nic_globals.lock);
|
|---|
| 70 |
|
|---|
| 71 | char buffer[256];
|
|---|
| 72 | snprintf(buffer, 256, "drv/" DEVICE_CATEGORY_NIC "/%s", name);
|
|---|
| 73 |
|
|---|
| 74 | return EOK;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /** Fill in the default implementations for device options and NIC interface.
|
|---|
| 78 | *
|
|---|
| 79 | * @param driver_ops
|
|---|
| 80 | * @param dev_ops
|
|---|
| 81 | * @param iface
|
|---|
| 82 | */
|
|---|
| 83 | void nic_driver_implement(driver_ops_t *driver_ops, ddf_dev_ops_t *dev_ops,
|
|---|
| 84 | nic_iface_t *iface)
|
|---|
| 85 | {
|
|---|
| 86 | if (dev_ops) {
|
|---|
| 87 | if (!dev_ops->open)
|
|---|
| 88 | dev_ops->open = nic_open_impl;
|
|---|
| 89 | if (!dev_ops->close)
|
|---|
| 90 | dev_ops->close = nic_close_impl;
|
|---|
| 91 | if (!dev_ops->interfaces[NIC_DEV_IFACE])
|
|---|
| 92 | dev_ops->interfaces[NIC_DEV_IFACE] = iface;
|
|---|
| 93 | if (!dev_ops->default_handler)
|
|---|
| 94 | dev_ops->default_handler = nic_default_handler_impl;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | if (iface) {
|
|---|
| 98 | if (!iface->get_state)
|
|---|
| 99 | iface->get_state = nic_get_state_impl;
|
|---|
| 100 | if (!iface->set_state)
|
|---|
| 101 | iface->set_state = nic_set_state_impl;
|
|---|
| 102 | if (!iface->send_frame)
|
|---|
| 103 | iface->send_frame = nic_send_frame_impl;
|
|---|
| 104 | if (!iface->callback_create)
|
|---|
| 105 | iface->callback_create = nic_callback_create_impl;
|
|---|
| 106 | if (!iface->get_address)
|
|---|
| 107 | iface->get_address = nic_get_address_impl;
|
|---|
| 108 | if (!iface->get_stats)
|
|---|
| 109 | iface->get_stats = nic_get_stats_impl;
|
|---|
| 110 | if (!iface->unicast_get_mode)
|
|---|
| 111 | iface->unicast_get_mode = nic_unicast_get_mode_impl;
|
|---|
| 112 | if (!iface->unicast_set_mode)
|
|---|
| 113 | iface->unicast_set_mode = nic_unicast_set_mode_impl;
|
|---|
| 114 | if (!iface->multicast_get_mode)
|
|---|
| 115 | iface->multicast_get_mode = nic_multicast_get_mode_impl;
|
|---|
| 116 | if (!iface->multicast_set_mode)
|
|---|
| 117 | iface->multicast_set_mode = nic_multicast_set_mode_impl;
|
|---|
| 118 | if (!iface->broadcast_get_mode)
|
|---|
| 119 | iface->broadcast_get_mode = nic_broadcast_get_mode_impl;
|
|---|
| 120 | if (!iface->broadcast_set_mode)
|
|---|
| 121 | iface->broadcast_set_mode = nic_broadcast_set_mode_impl;
|
|---|
| 122 | if (!iface->blocked_sources_get)
|
|---|
| 123 | iface->blocked_sources_get = nic_blocked_sources_get_impl;
|
|---|
| 124 | if (!iface->blocked_sources_set)
|
|---|
| 125 | iface->blocked_sources_set = nic_blocked_sources_set_impl;
|
|---|
| 126 | if (!iface->vlan_get_mask)
|
|---|
| 127 | iface->vlan_get_mask = nic_vlan_get_mask_impl;
|
|---|
| 128 | if (!iface->vlan_set_mask)
|
|---|
| 129 | iface->vlan_set_mask = nic_vlan_set_mask_impl;
|
|---|
| 130 | if (!iface->wol_virtue_add)
|
|---|
| 131 | iface->wol_virtue_add = nic_wol_virtue_add_impl;
|
|---|
| 132 | if (!iface->wol_virtue_remove)
|
|---|
| 133 | iface->wol_virtue_remove = nic_wol_virtue_remove_impl;
|
|---|
| 134 | if (!iface->wol_virtue_probe)
|
|---|
| 135 | iface->wol_virtue_probe = nic_wol_virtue_probe_impl;
|
|---|
| 136 | if (!iface->wol_virtue_list)
|
|---|
| 137 | iface->wol_virtue_list = nic_wol_virtue_list_impl;
|
|---|
| 138 | if (!iface->wol_virtue_get_caps)
|
|---|
| 139 | iface->wol_virtue_get_caps = nic_wol_virtue_get_caps_impl;
|
|---|
| 140 | if (!iface->poll_get_mode)
|
|---|
| 141 | iface->poll_get_mode = nic_poll_get_mode_impl;
|
|---|
| 142 | if (!iface->poll_set_mode)
|
|---|
| 143 | iface->poll_set_mode = nic_poll_set_mode_impl;
|
|---|
| 144 | if (!iface->poll_now)
|
|---|
| 145 | iface->poll_now = nic_poll_now_impl;
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * Setup send frame handler. This MUST be called in the add_device handler
|
|---|
| 151 | * if the nic_send_message_impl function is used for sending messages (filled
|
|---|
| 152 | * as send_message member of the nic_iface_t structure). The function must not
|
|---|
| 153 | * be called anywhere else.
|
|---|
| 154 | *
|
|---|
| 155 | * @param nic_data
|
|---|
| 156 | * @param sffunc Function handling the send_frame request
|
|---|
| 157 | */
|
|---|
| 158 | void nic_set_send_frame_handler(nic_t *nic_data, send_frame_handler sffunc)
|
|---|
| 159 | {
|
|---|
| 160 | nic_data->send_frame = sffunc;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * Setup event handlers for transitions between driver states.
|
|---|
| 165 | * This function can be called only in the add_device handler.
|
|---|
| 166 | *
|
|---|
| 167 | * @param on_activating Called when device is going to the ACTIVE state.
|
|---|
| 168 | * @param on_going_down Called when device is going to the DOWN state.
|
|---|
| 169 | * @param on_stopping Called when device is going to the STOP state.
|
|---|
| 170 | */
|
|---|
| 171 | void nic_set_state_change_handlers(nic_t *nic_data,
|
|---|
| 172 | state_change_handler on_activating, state_change_handler on_going_down,
|
|---|
| 173 | state_change_handler on_stopping)
|
|---|
| 174 | {
|
|---|
| 175 | nic_data->on_activating = on_activating;
|
|---|
| 176 | nic_data->on_going_down = on_going_down;
|
|---|
| 177 | nic_data->on_stopping = on_stopping;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | /**
|
|---|
| 181 | * Setup event handlers for changing the filtering modes.
|
|---|
| 182 | * This function can be called only in the add_device handler.
|
|---|
| 183 | *
|
|---|
| 184 | * @param nic_data
|
|---|
| 185 | * @param on_unicast_mode_change
|
|---|
| 186 | * @param on_multicast_mode_change
|
|---|
| 187 | * @param on_broadcast_mode_change
|
|---|
| 188 | * @param on_blocked_sources_change
|
|---|
| 189 | * @param on_vlan_mask_change
|
|---|
| 190 | */
|
|---|
| 191 | void nic_set_filtering_change_handlers(nic_t *nic_data,
|
|---|
| 192 | unicast_mode_change_handler on_unicast_mode_change,
|
|---|
| 193 | multicast_mode_change_handler on_multicast_mode_change,
|
|---|
| 194 | broadcast_mode_change_handler on_broadcast_mode_change,
|
|---|
| 195 | blocked_sources_change_handler on_blocked_sources_change,
|
|---|
| 196 | vlan_mask_change_handler on_vlan_mask_change)
|
|---|
| 197 | {
|
|---|
| 198 | nic_data->on_unicast_mode_change = on_unicast_mode_change;
|
|---|
| 199 | nic_data->on_multicast_mode_change = on_multicast_mode_change;
|
|---|
| 200 | nic_data->on_broadcast_mode_change = on_broadcast_mode_change;
|
|---|
| 201 | nic_data->on_blocked_sources_change = on_blocked_sources_change;
|
|---|
| 202 | nic_data->on_vlan_mask_change = on_vlan_mask_change;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /**
|
|---|
| 206 | * Setup filters for WOL virtues add and removal.
|
|---|
| 207 | * This function can be called only in the add_device handler. Both handlers
|
|---|
| 208 | * must be set or none of them.
|
|---|
| 209 | *
|
|---|
| 210 | * @param on_wv_add Called when a virtue is added
|
|---|
| 211 | * @param on_wv_remove Called when a virtue is removed
|
|---|
| 212 | */
|
|---|
| 213 | void nic_set_wol_virtue_change_handlers(nic_t *nic_data,
|
|---|
| 214 | wol_virtue_add_handler on_wv_add, wol_virtue_remove_handler on_wv_remove)
|
|---|
| 215 | {
|
|---|
| 216 | assert(on_wv_add != NULL && on_wv_remove != NULL);
|
|---|
| 217 | nic_data->on_wol_virtue_add = on_wv_add;
|
|---|
| 218 | nic_data->on_wol_virtue_remove = on_wv_remove;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /**
|
|---|
| 222 | * Setup poll handlers.
|
|---|
| 223 | * This function can be called only in the add_device handler.
|
|---|
| 224 | *
|
|---|
| 225 | * @param on_poll_mode_change Called when the mode is about to be changed
|
|---|
| 226 | * @param on_poll_request Called when poll request is triggered
|
|---|
| 227 | */
|
|---|
| 228 | void nic_set_poll_handlers(nic_t *nic_data,
|
|---|
| 229 | poll_mode_change_handler on_poll_mode_change, poll_request_handler on_poll_req)
|
|---|
| 230 | {
|
|---|
| 231 | nic_data->on_poll_mode_change = on_poll_mode_change;
|
|---|
| 232 | nic_data->on_poll_request = on_poll_req;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | /**
|
|---|
| 236 | * Connect to the parent's driver and get HW resources list in parsed format.
|
|---|
| 237 | * Note: this function should be called only from add_device handler, therefore
|
|---|
| 238 | * we don't need to use locks.
|
|---|
| 239 | *
|
|---|
| 240 | * @param nic_data
|
|---|
| 241 | * @param[out] resources Parsed lists of resources.
|
|---|
| 242 | *
|
|---|
| 243 | * @return EOK or an error code
|
|---|
| 244 | */
|
|---|
| 245 | errno_t nic_get_resources(nic_t *nic_data, hw_res_list_parsed_t *resources)
|
|---|
| 246 | {
|
|---|
| 247 | ddf_dev_t *dev = nic_data->dev;
|
|---|
| 248 | async_sess_t *parent_sess;
|
|---|
| 249 |
|
|---|
| 250 | /* Connect to the parent's driver. */
|
|---|
| 251 | parent_sess = ddf_dev_parent_sess_get(dev);
|
|---|
| 252 | if (parent_sess == NULL)
|
|---|
| 253 | return EIO;
|
|---|
| 254 |
|
|---|
| 255 | return hw_res_get_list_parsed(parent_sess, resources, 0);
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /** Allocate frame
|
|---|
| 259 | *
|
|---|
| 260 | * @param nic_data The NIC driver data
|
|---|
| 261 | * @param size Frame size in bytes
|
|---|
| 262 | * @return pointer to allocated frame if success, NULL otherwise
|
|---|
| 263 | */
|
|---|
| 264 | nic_frame_t *nic_alloc_frame(nic_t *nic_data, size_t size)
|
|---|
| 265 | {
|
|---|
| 266 | nic_frame_t *frame;
|
|---|
| 267 | fibril_mutex_lock(&nic_globals.lock);
|
|---|
| 268 | if (nic_globals.frame_cache_size > 0) {
|
|---|
| 269 | link_t *first = list_first(&nic_globals.frame_cache);
|
|---|
| 270 | list_remove(first);
|
|---|
| 271 | nic_globals.frame_cache_size--;
|
|---|
| 272 | frame = list_get_instance(first, nic_frame_t, link);
|
|---|
| 273 | fibril_mutex_unlock(&nic_globals.lock);
|
|---|
| 274 | } else {
|
|---|
| 275 | fibril_mutex_unlock(&nic_globals.lock);
|
|---|
| 276 | frame = malloc(sizeof(nic_frame_t));
|
|---|
| 277 | if (!frame)
|
|---|
| 278 | return NULL;
|
|---|
| 279 |
|
|---|
| 280 | link_initialize(&frame->link);
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | frame->data = malloc(size);
|
|---|
| 284 | if (frame->data == NULL) {
|
|---|
| 285 | free(frame);
|
|---|
| 286 | return NULL;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | frame->size = size;
|
|---|
| 290 | return frame;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | /** Release frame
|
|---|
| 294 | *
|
|---|
| 295 | * @param nic_data The driver data
|
|---|
| 296 | * @param frame The frame to release
|
|---|
| 297 | */
|
|---|
| 298 | void nic_release_frame(nic_t *nic_data, nic_frame_t *frame)
|
|---|
| 299 | {
|
|---|
| 300 | if (!frame)
|
|---|
| 301 | return;
|
|---|
| 302 |
|
|---|
| 303 | if (frame->data != NULL) {
|
|---|
| 304 | free(frame->data);
|
|---|
| 305 | frame->data = NULL;
|
|---|
| 306 | frame->size = 0;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | fibril_mutex_lock(&nic_globals.lock);
|
|---|
| 310 | if (nic_globals.frame_cache_size >= NIC_GLOBALS_MAX_CACHE_SIZE) {
|
|---|
| 311 | fibril_mutex_unlock(&nic_globals.lock);
|
|---|
| 312 | free(frame);
|
|---|
| 313 | } else {
|
|---|
| 314 | list_prepend(&frame->link, &nic_globals.frame_cache);
|
|---|
| 315 | nic_globals.frame_cache_size++;
|
|---|
| 316 | fibril_mutex_unlock(&nic_globals.lock);
|
|---|
| 317 | }
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | /**
|
|---|
| 321 | * Allocate a new frame list
|
|---|
| 322 | *
|
|---|
| 323 | * @return New frame list or NULL on error.
|
|---|
| 324 | */
|
|---|
| 325 | nic_frame_list_t *nic_alloc_frame_list(void)
|
|---|
| 326 | {
|
|---|
| 327 | nic_frame_list_t *frames;
|
|---|
| 328 | fibril_mutex_lock(&nic_globals.lock);
|
|---|
| 329 |
|
|---|
| 330 | if (nic_globals.frame_list_cache_size > 0) {
|
|---|
| 331 | frames =
|
|---|
| 332 | list_get_instance(list_first(&nic_globals.frame_list_cache),
|
|---|
| 333 | nic_frame_list_t, head);
|
|---|
| 334 | list_remove(&frames->head);
|
|---|
| 335 | list_initialize(frames);
|
|---|
| 336 | nic_globals.frame_list_cache_size--;
|
|---|
| 337 | fibril_mutex_unlock(&nic_globals.lock);
|
|---|
| 338 | } else {
|
|---|
| 339 | fibril_mutex_unlock(&nic_globals.lock);
|
|---|
| 340 |
|
|---|
| 341 | frames = malloc(sizeof (nic_frame_list_t));
|
|---|
| 342 | if (frames != NULL)
|
|---|
| 343 | list_initialize(frames);
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | return frames;
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | static void nic_driver_release_frame_list(nic_frame_list_t *frames)
|
|---|
| 350 | {
|
|---|
| 351 | if (!frames)
|
|---|
| 352 | return;
|
|---|
| 353 | fibril_mutex_lock(&nic_globals.lock);
|
|---|
| 354 | if (nic_globals.frame_list_cache_size >= NIC_GLOBALS_MAX_CACHE_SIZE) {
|
|---|
| 355 | fibril_mutex_unlock(&nic_globals.lock);
|
|---|
| 356 | free(frames);
|
|---|
| 357 | } else {
|
|---|
| 358 | list_prepend(&frames->head, &nic_globals.frame_list_cache);
|
|---|
| 359 | nic_globals.frame_list_cache_size++;
|
|---|
| 360 | fibril_mutex_unlock(&nic_globals.lock);
|
|---|
| 361 | }
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | /**
|
|---|
| 365 | * Append a frame to the frame list
|
|---|
| 366 | *
|
|---|
| 367 | * @param frames Frame list
|
|---|
| 368 | * @param frame Appended frame
|
|---|
| 369 | */
|
|---|
| 370 | void nic_frame_list_append(nic_frame_list_t *frames,
|
|---|
| 371 | nic_frame_t *frame)
|
|---|
| 372 | {
|
|---|
| 373 | assert(frame != NULL && frames != NULL);
|
|---|
| 374 | list_append(&frame->link, frames);
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | /** Get the polling mode information from the device
|
|---|
| 378 | *
|
|---|
| 379 | * The main lock should be locked, otherwise the inconsistency between
|
|---|
| 380 | * mode and period can occure.
|
|---|
| 381 | *
|
|---|
| 382 | * @param nic_data The controller data
|
|---|
| 383 | * @param period [out] The the period. Valid only if mode == NIC_POLL_PERIODIC
|
|---|
| 384 | * @return Current polling mode of the controller
|
|---|
| 385 | */
|
|---|
| 386 | nic_poll_mode_t nic_query_poll_mode(nic_t *nic_data, struct timeval *period)
|
|---|
| 387 | {
|
|---|
| 388 | if (period)
|
|---|
| 389 | *period = nic_data->poll_period;
|
|---|
| 390 | return nic_data->poll_mode;
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | /** Inform the NICF about poll mode
|
|---|
| 394 | *
|
|---|
| 395 | * @param nic_data The controller data
|
|---|
| 396 | * @param mode
|
|---|
| 397 | * @param period [out] The the period. Valid only if mode == NIC_POLL_PERIODIC
|
|---|
| 398 | * @return EOK
|
|---|
| 399 | * @return EINVAL
|
|---|
| 400 | */
|
|---|
| 401 | errno_t nic_report_poll_mode(nic_t *nic_data, nic_poll_mode_t mode,
|
|---|
| 402 | struct timeval *period)
|
|---|
| 403 | {
|
|---|
| 404 | errno_t rc = EOK;
|
|---|
| 405 | fibril_rwlock_write_lock(&nic_data->main_lock);
|
|---|
| 406 | nic_data->poll_mode = mode;
|
|---|
| 407 | nic_data->default_poll_mode = mode;
|
|---|
| 408 | if (mode == NIC_POLL_PERIODIC) {
|
|---|
| 409 | if (period) {
|
|---|
| 410 | memcpy(&nic_data->default_poll_period, period, sizeof(struct timeval));
|
|---|
| 411 | memcpy(&nic_data->poll_period, period, sizeof(struct timeval));
|
|---|
| 412 | } else {
|
|---|
| 413 | rc = EINVAL;
|
|---|
| 414 | }
|
|---|
| 415 | }
|
|---|
| 416 | fibril_rwlock_write_unlock(&nic_data->main_lock);
|
|---|
| 417 | return rc;
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | /** Inform the NICF about device's MAC adress.
|
|---|
| 421 | *
|
|---|
| 422 | * @return EOK On success
|
|---|
| 423 | *
|
|---|
| 424 | */
|
|---|
| 425 | errno_t nic_report_address(nic_t *nic_data, const nic_address_t *address)
|
|---|
| 426 | {
|
|---|
| 427 | assert(nic_data);
|
|---|
| 428 |
|
|---|
| 429 | if (address->address[0] & 1)
|
|---|
| 430 | return EINVAL;
|
|---|
| 431 |
|
|---|
| 432 | fibril_rwlock_write_lock(&nic_data->main_lock);
|
|---|
| 433 |
|
|---|
| 434 | /* Notify NIL layer (and uppper) if bound - not in add_device */
|
|---|
| 435 | if (nic_data->client_session != NULL) {
|
|---|
| 436 | errno_t rc = nic_ev_addr_changed(nic_data->client_session,
|
|---|
| 437 | address);
|
|---|
| 438 |
|
|---|
| 439 | if (rc != EOK) {
|
|---|
| 440 | fibril_rwlock_write_unlock(&nic_data->main_lock);
|
|---|
| 441 | return rc;
|
|---|
| 442 | }
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | fibril_rwlock_write_lock(&nic_data->rxc_lock);
|
|---|
| 446 |
|
|---|
| 447 | /*
|
|---|
| 448 | * The initial address (all zeroes) shouldn't be
|
|---|
| 449 | * there and we will ignore that error -- in next
|
|---|
| 450 | * calls this should not happen.
|
|---|
| 451 | */
|
|---|
| 452 | errno_t rc = nic_rxc_set_addr(&nic_data->rx_control,
|
|---|
| 453 | &nic_data->mac, address);
|
|---|
| 454 |
|
|---|
| 455 | /* For the first time also record the default MAC */
|
|---|
| 456 | if (MAC_IS_ZERO(nic_data->default_mac.address)) {
|
|---|
| 457 | assert(MAC_IS_ZERO(nic_data->mac.address));
|
|---|
| 458 | memcpy(&nic_data->default_mac, address, sizeof(nic_address_t));
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | fibril_rwlock_write_unlock(&nic_data->rxc_lock);
|
|---|
| 462 |
|
|---|
| 463 | if ((rc != EOK) && (rc != ENOENT)) {
|
|---|
| 464 | fibril_rwlock_write_unlock(&nic_data->main_lock);
|
|---|
| 465 | return rc;
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| 468 | memcpy(&nic_data->mac, address, sizeof(nic_address_t));
|
|---|
| 469 |
|
|---|
| 470 | fibril_rwlock_write_unlock(&nic_data->main_lock);
|
|---|
| 471 |
|
|---|
| 472 | return EOK;
|
|---|
| 473 | }
|
|---|
| 474 |
|
|---|
| 475 | /**
|
|---|
| 476 | * Used to obtain devices MAC address.
|
|---|
| 477 | *
|
|---|
| 478 | * The main lock should be locked, otherwise the inconsistent address
|
|---|
| 479 | * can be returend.
|
|---|
| 480 | *
|
|---|
| 481 | * @param nic_data The controller data
|
|---|
| 482 | * @param address The output for address.
|
|---|
| 483 | */
|
|---|
| 484 | void nic_query_address(nic_t *nic_data, nic_address_t *addr)
|
|---|
| 485 | {
|
|---|
| 486 | if (!addr)
|
|---|
| 487 | return;
|
|---|
| 488 |
|
|---|
| 489 | memcpy(addr, &nic_data->mac, sizeof(nic_address_t));
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | /**
|
|---|
| 493 | * The busy flag can be set to 1 only in the send_frame handler, to 0 it can
|
|---|
| 494 | * be set anywhere.
|
|---|
| 495 | *
|
|---|
| 496 | * @param nic_data
|
|---|
| 497 | * @param busy
|
|---|
| 498 | */
|
|---|
| 499 | void nic_set_tx_busy(nic_t *nic_data, int busy)
|
|---|
| 500 | {
|
|---|
| 501 | /*
|
|---|
| 502 | * When the function is called in send_frame handler the main lock is
|
|---|
| 503 | * locked so no race can happen.
|
|---|
| 504 | * Otherwise, when it is unexpectedly set to 0 (even with main lock held
|
|---|
| 505 | * by other fibril) it cannot crash anything.
|
|---|
| 506 | */
|
|---|
| 507 | nic_data->tx_busy = busy;
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 | /**
|
|---|
| 511 | * This is the function that the driver should call when it receives a frame.
|
|---|
| 512 | * The frame is checked by filters and then sent up to the NIL layer or
|
|---|
| 513 | * discarded. The frame is released.
|
|---|
| 514 | *
|
|---|
| 515 | * @param nic_data
|
|---|
| 516 | * @param frame The received frame
|
|---|
| 517 | */
|
|---|
| 518 | void nic_received_frame(nic_t *nic_data, nic_frame_t *frame)
|
|---|
| 519 | {
|
|---|
| 520 | /*
|
|---|
| 521 | * Note: this function must not lock main lock, because loopback driver
|
|---|
| 522 | * calls it inside send_frame handler (with locked main lock)
|
|---|
| 523 | */
|
|---|
| 524 | fibril_rwlock_read_lock(&nic_data->rxc_lock);
|
|---|
| 525 | nic_frame_type_t frame_type;
|
|---|
| 526 | bool check = nic_rxc_check(&nic_data->rx_control, frame->data,
|
|---|
| 527 | frame->size, &frame_type);
|
|---|
| 528 | fibril_rwlock_read_unlock(&nic_data->rxc_lock);
|
|---|
| 529 | /* Update statistics */
|
|---|
| 530 | fibril_rwlock_write_lock(&nic_data->stats_lock);
|
|---|
| 531 |
|
|---|
| 532 | if (nic_data->state == NIC_STATE_ACTIVE && check) {
|
|---|
| 533 | nic_data->stats.receive_packets++;
|
|---|
| 534 | nic_data->stats.receive_bytes += frame->size;
|
|---|
| 535 | switch (frame_type) {
|
|---|
| 536 | case NIC_FRAME_MULTICAST:
|
|---|
| 537 | nic_data->stats.receive_multicast++;
|
|---|
| 538 | break;
|
|---|
| 539 | case NIC_FRAME_BROADCAST:
|
|---|
| 540 | nic_data->stats.receive_broadcast++;
|
|---|
| 541 | break;
|
|---|
| 542 | default:
|
|---|
| 543 | break;
|
|---|
| 544 | }
|
|---|
| 545 | fibril_rwlock_write_unlock(&nic_data->stats_lock);
|
|---|
| 546 | nic_ev_received(nic_data->client_session, frame->data,
|
|---|
| 547 | frame->size);
|
|---|
| 548 | } else {
|
|---|
| 549 | switch (frame_type) {
|
|---|
| 550 | case NIC_FRAME_UNICAST:
|
|---|
| 551 | nic_data->stats.receive_filtered_unicast++;
|
|---|
| 552 | break;
|
|---|
| 553 | case NIC_FRAME_MULTICAST:
|
|---|
| 554 | nic_data->stats.receive_filtered_multicast++;
|
|---|
| 555 | break;
|
|---|
| 556 | case NIC_FRAME_BROADCAST:
|
|---|
| 557 | nic_data->stats.receive_filtered_broadcast++;
|
|---|
| 558 | break;
|
|---|
| 559 | }
|
|---|
| 560 | fibril_rwlock_write_unlock(&nic_data->stats_lock);
|
|---|
| 561 | }
|
|---|
| 562 | nic_release_frame(nic_data, frame);
|
|---|
| 563 | }
|
|---|
| 564 |
|
|---|
| 565 | /**
|
|---|
| 566 | * Some NICs can receive multiple frames during single interrupt. These can
|
|---|
| 567 | * send them in whole list of frames (actually nic_frame_t structures), then
|
|---|
| 568 | * the list is deallocated and each frame is passed to the
|
|---|
| 569 | * nic_received_packet function.
|
|---|
| 570 | *
|
|---|
| 571 | * @param nic_data
|
|---|
| 572 | * @param frames List of received frames
|
|---|
| 573 | */
|
|---|
| 574 | void nic_received_frame_list(nic_t *nic_data, nic_frame_list_t *frames)
|
|---|
| 575 | {
|
|---|
| 576 | if (frames == NULL)
|
|---|
| 577 | return;
|
|---|
| 578 | while (!list_empty(frames)) {
|
|---|
| 579 | nic_frame_t *frame =
|
|---|
| 580 | list_get_instance(list_first(frames), nic_frame_t, link);
|
|---|
| 581 |
|
|---|
| 582 | list_remove(&frame->link);
|
|---|
| 583 | nic_received_frame(nic_data, frame);
|
|---|
| 584 | }
|
|---|
| 585 | nic_driver_release_frame_list(frames);
|
|---|
| 586 | }
|
|---|
| 587 |
|
|---|
| 588 | /** Allocate and initialize the driver data.
|
|---|
| 589 | *
|
|---|
| 590 | * @return Allocated structure or NULL.
|
|---|
| 591 | *
|
|---|
| 592 | */
|
|---|
| 593 | static nic_t *nic_create(ddf_dev_t *dev)
|
|---|
| 594 | {
|
|---|
| 595 | nic_t *nic_data = ddf_dev_data_alloc(dev, sizeof(nic_t));
|
|---|
| 596 | if (nic_data == NULL)
|
|---|
| 597 | return NULL;
|
|---|
| 598 |
|
|---|
| 599 | /* Force zero to all uninitialized fields (e.g. added in future) */
|
|---|
| 600 | if (nic_rxc_init(&nic_data->rx_control) != EOK) {
|
|---|
| 601 | return NULL;
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 604 | if (nic_wol_virtues_init(&nic_data->wol_virtues) != EOK) {
|
|---|
| 605 | return NULL;
|
|---|
| 606 | }
|
|---|
| 607 |
|
|---|
| 608 | nic_data->dev = NULL;
|
|---|
| 609 | nic_data->fun = NULL;
|
|---|
| 610 | nic_data->state = NIC_STATE_STOPPED;
|
|---|
| 611 | nic_data->client_session = NULL;
|
|---|
| 612 | nic_data->poll_mode = NIC_POLL_IMMEDIATE;
|
|---|
| 613 | nic_data->default_poll_mode = NIC_POLL_IMMEDIATE;
|
|---|
| 614 | nic_data->send_frame = NULL;
|
|---|
| 615 | nic_data->on_activating = NULL;
|
|---|
| 616 | nic_data->on_going_down = NULL;
|
|---|
| 617 | nic_data->on_stopping = NULL;
|
|---|
| 618 | nic_data->specific = NULL;
|
|---|
| 619 |
|
|---|
| 620 | fibril_rwlock_initialize(&nic_data->main_lock);
|
|---|
| 621 | fibril_rwlock_initialize(&nic_data->stats_lock);
|
|---|
| 622 | fibril_rwlock_initialize(&nic_data->rxc_lock);
|
|---|
| 623 | fibril_rwlock_initialize(&nic_data->wv_lock);
|
|---|
| 624 |
|
|---|
| 625 | memset(&nic_data->mac, 0, sizeof(nic_address_t));
|
|---|
| 626 | memset(&nic_data->default_mac, 0, sizeof(nic_address_t));
|
|---|
| 627 | memset(&nic_data->stats, 0, sizeof(nic_device_stats_t));
|
|---|
| 628 |
|
|---|
| 629 | return nic_data;
|
|---|
| 630 | }
|
|---|
| 631 |
|
|---|
| 632 | /** Create NIC structure for the device and bind it to dev_fun_t
|
|---|
| 633 | *
|
|---|
| 634 | * The pointer to the created and initialized NIC structure will
|
|---|
| 635 | * be stored in device->nic_data.
|
|---|
| 636 | *
|
|---|
| 637 | * @param device The NIC device structure
|
|---|
| 638 | *
|
|---|
| 639 | * @return Pointer to created nic_t structure or NULL
|
|---|
| 640 | *
|
|---|
| 641 | */
|
|---|
| 642 | nic_t *nic_create_and_bind(ddf_dev_t *device)
|
|---|
| 643 | {
|
|---|
| 644 | nic_t *nic_data = nic_create(device);
|
|---|
| 645 | if (!nic_data)
|
|---|
| 646 | return NULL;
|
|---|
| 647 |
|
|---|
| 648 | nic_data->dev = device;
|
|---|
| 649 |
|
|---|
| 650 | return nic_data;
|
|---|
| 651 | }
|
|---|
| 652 |
|
|---|
| 653 | /**
|
|---|
| 654 | * Hangs up the phones in the structure, deallocates specific data and then
|
|---|
| 655 | * the structure itself.
|
|---|
| 656 | *
|
|---|
| 657 | * @param data
|
|---|
| 658 | */
|
|---|
| 659 | static void nic_destroy(nic_t *nic_data)
|
|---|
| 660 | {
|
|---|
| 661 | free(nic_data->specific);
|
|---|
| 662 | }
|
|---|
| 663 |
|
|---|
| 664 | /**
|
|---|
| 665 | * Unbind and destroy nic_t stored in ddf_dev_t.nic_data.
|
|---|
| 666 | * The ddf_dev_t.nic_data will be set to NULL, specific driver data will be
|
|---|
| 667 | * destroyed.
|
|---|
| 668 | *
|
|---|
| 669 | * @param device The NIC device structure
|
|---|
| 670 | */
|
|---|
| 671 | void nic_unbind_and_destroy(ddf_dev_t *device)
|
|---|
| 672 | {
|
|---|
| 673 | nic_destroy(nic_get_from_ddf_dev(device));
|
|---|
| 674 | return;
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | /**
|
|---|
| 678 | * Set information about current HW filtering.
|
|---|
| 679 | * 1 ... Only those frames we want to receive are passed through HW
|
|---|
| 680 | * 0 ... The HW filtering is imperfect
|
|---|
| 681 | * -1 ... Don't change the setting
|
|---|
| 682 | * Can be called only from the on_*_change handler.
|
|---|
| 683 | *
|
|---|
| 684 | * @param nic_data
|
|---|
| 685 | * @param unicast_exact Unicast frames
|
|---|
| 686 | * @param mcast_exact Multicast frames
|
|---|
| 687 | * @param vlan_exact VLAN tags
|
|---|
| 688 | */
|
|---|
| 689 | void nic_report_hw_filtering(nic_t *nic_data,
|
|---|
| 690 | int unicast_exact, int multicast_exact, int vlan_exact)
|
|---|
| 691 | {
|
|---|
| 692 | nic_rxc_hw_filtering(&nic_data->rx_control,
|
|---|
| 693 | unicast_exact, multicast_exact, vlan_exact);
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | /**
|
|---|
| 697 | * Computes hash for the address list based on standard multicast address
|
|---|
| 698 | * hashing.
|
|---|
| 699 | *
|
|---|
| 700 | * @param address_list
|
|---|
| 701 | * @param count
|
|---|
| 702 | *
|
|---|
| 703 | * @return Multicast hash
|
|---|
| 704 | *
|
|---|
| 705 | * @see multicast_hash
|
|---|
| 706 | */
|
|---|
| 707 | uint64_t nic_mcast_hash(const nic_address_t *list, size_t count)
|
|---|
| 708 | {
|
|---|
| 709 | return nic_rxc_mcast_hash(list, count);
|
|---|
| 710 | }
|
|---|
| 711 |
|
|---|
| 712 | /**
|
|---|
| 713 | * Computes hash for multicast addresses currently set up in the RX multicast
|
|---|
| 714 | * filtering. For promiscuous mode returns all ones, for blocking all zeroes.
|
|---|
| 715 | * Can be called only from the state change handlers (on_activating,
|
|---|
| 716 | * on_going_down and on_stopping).
|
|---|
| 717 | *
|
|---|
| 718 | * @param nic_data
|
|---|
| 719 | *
|
|---|
| 720 | * @return Multicast hash
|
|---|
| 721 | *
|
|---|
| 722 | * @see multicast_hash
|
|---|
| 723 | */
|
|---|
| 724 | uint64_t nic_query_mcast_hash(nic_t *nic_data)
|
|---|
| 725 | {
|
|---|
| 726 | fibril_rwlock_read_lock(&nic_data->rxc_lock);
|
|---|
| 727 | uint64_t hash = nic_rxc_multicast_get_hash(&nic_data->rx_control);
|
|---|
| 728 | fibril_rwlock_read_unlock(&nic_data->rxc_lock);
|
|---|
| 729 | return hash;
|
|---|
| 730 | }
|
|---|
| 731 |
|
|---|
| 732 | /**
|
|---|
| 733 | * Queries the current mode of unicast frames receiving.
|
|---|
| 734 | * Can be called only from the on_*_change handler.
|
|---|
| 735 | *
|
|---|
| 736 | * @param nic_data
|
|---|
| 737 | * @param mode The new unicast mode
|
|---|
| 738 | * @param max_count Max number of addresses that can be written into the
|
|---|
| 739 | * address_list.
|
|---|
| 740 | * @param address_list List of MAC addresses or NULL.
|
|---|
| 741 | * @param address_count Number of addresses in the list
|
|---|
| 742 | */
|
|---|
| 743 | void nic_query_unicast(const nic_t *nic_data,
|
|---|
| 744 | nic_unicast_mode_t *mode,
|
|---|
| 745 | size_t max_count, nic_address_t *address_list, size_t *address_count)
|
|---|
| 746 | {
|
|---|
| 747 | assert(mode != NULL);
|
|---|
| 748 | nic_rxc_unicast_get_mode(&nic_data->rx_control, mode,
|
|---|
| 749 | max_count, address_list, address_count);
|
|---|
| 750 | }
|
|---|
| 751 |
|
|---|
| 752 | /**
|
|---|
| 753 | * Queries the current mode of multicast frames receiving.
|
|---|
| 754 | * Can be called only from the on_*_change handler.
|
|---|
| 755 | *
|
|---|
| 756 | * @param nic_data
|
|---|
| 757 | * @param mode The current multicast mode
|
|---|
| 758 | * @param max_count Max number of addresses that can be written into the
|
|---|
| 759 | * address_list.
|
|---|
| 760 | * @param address_list List of MAC addresses or NULL.
|
|---|
| 761 | * @param address_count Number of addresses in the list
|
|---|
| 762 | */
|
|---|
| 763 | void nic_query_multicast(const nic_t *nic_data,
|
|---|
| 764 | nic_multicast_mode_t *mode,
|
|---|
| 765 | size_t max_count, nic_address_t *address_list, size_t *address_count)
|
|---|
| 766 | {
|
|---|
| 767 | assert(mode != NULL);
|
|---|
| 768 | nic_rxc_multicast_get_mode(&nic_data->rx_control, mode,
|
|---|
| 769 | max_count, address_list, address_count);
|
|---|
| 770 | }
|
|---|
| 771 |
|
|---|
| 772 | /**
|
|---|
| 773 | * Queries the current mode of broadcast frames receiving.
|
|---|
| 774 | * Can be called only from the on_*_change handler.
|
|---|
| 775 | *
|
|---|
| 776 | * @param nic_data
|
|---|
| 777 | * @param mode The new broadcast mode
|
|---|
| 778 | */
|
|---|
| 779 | void nic_query_broadcast(const nic_t *nic_data,
|
|---|
| 780 | nic_broadcast_mode_t *mode)
|
|---|
| 781 | {
|
|---|
| 782 | assert(mode != NULL);
|
|---|
| 783 | nic_rxc_broadcast_get_mode(&nic_data->rx_control, mode);
|
|---|
| 784 | }
|
|---|
| 785 |
|
|---|
| 786 | /**
|
|---|
| 787 | * Queries the current blocked source addresses.
|
|---|
| 788 | * Can be called only from the on_*_change handler.
|
|---|
| 789 | *
|
|---|
| 790 | * @param nic_data
|
|---|
| 791 | * @param max_count Max number of addresses that can be written into the
|
|---|
| 792 | * address_list.
|
|---|
| 793 | * @param address_list List of MAC addresses or NULL.
|
|---|
| 794 | * @param address_count Number of addresses in the list
|
|---|
| 795 | */
|
|---|
| 796 | void nic_query_blocked_sources(const nic_t *nic_data,
|
|---|
| 797 | size_t max_count, nic_address_t *address_list, size_t *address_count)
|
|---|
| 798 | {
|
|---|
| 799 | nic_rxc_blocked_sources_get(&nic_data->rx_control,
|
|---|
| 800 | max_count, address_list, address_count);
|
|---|
| 801 | }
|
|---|
| 802 |
|
|---|
| 803 | /**
|
|---|
| 804 | * Query mask used for filtering according to the VLAN tags.
|
|---|
| 805 | * Can be called only from the on_*_change handler.
|
|---|
| 806 | *
|
|---|
| 807 | * @param nic_data
|
|---|
| 808 | * @param mask Must be 512 bytes long
|
|---|
| 809 | *
|
|---|
| 810 | * @return EOK
|
|---|
| 811 | * @return ENOENT
|
|---|
| 812 | */
|
|---|
| 813 | errno_t nic_query_vlan_mask(const nic_t *nic_data, nic_vlan_mask_t *mask)
|
|---|
| 814 | {
|
|---|
| 815 | assert(mask);
|
|---|
| 816 | return nic_rxc_vlan_get_mask(&nic_data->rx_control, mask);
|
|---|
| 817 | }
|
|---|
| 818 |
|
|---|
| 819 | /**
|
|---|
| 820 | * Query maximum number of WOL virtues of specified type allowed on the device.
|
|---|
| 821 | * Can be called only from add_device and on_wol_virtue_* handlers.
|
|---|
| 822 | *
|
|---|
| 823 | * @param nic_data
|
|---|
| 824 | * @param type The type of the WOL virtues
|
|---|
| 825 | *
|
|---|
| 826 | * @return Maximal number of allowed virtues of this type. -1 means this type
|
|---|
| 827 | * is not supported at all.
|
|---|
| 828 | */
|
|---|
| 829 | int nic_query_wol_max_caps(const nic_t *nic_data, nic_wv_type_t type)
|
|---|
| 830 | {
|
|---|
| 831 | return nic_data->wol_virtues.caps_max[type];
|
|---|
| 832 | }
|
|---|
| 833 |
|
|---|
| 834 | /**
|
|---|
| 835 | * Sets maximum number of WOL virtues of specified type allowed on the device.
|
|---|
| 836 | * Can be called only from add_device and on_wol_virtue_* handlers.
|
|---|
| 837 | *
|
|---|
| 838 | * @param nic_data
|
|---|
| 839 | * @param type The type of the WOL virtues
|
|---|
| 840 | * @param count Maximal number of allowed virtues of this type. -1 means
|
|---|
| 841 | * this type is not supported at all.
|
|---|
| 842 | */
|
|---|
| 843 | void nic_set_wol_max_caps(nic_t *nic_data, nic_wv_type_t type, int count)
|
|---|
| 844 | {
|
|---|
| 845 | nic_data->wol_virtues.caps_max[type] = count;
|
|---|
| 846 | }
|
|---|
| 847 |
|
|---|
| 848 | /**
|
|---|
| 849 | * @param nic_data
|
|---|
| 850 | * @return The driver-specific structure for this NIC.
|
|---|
| 851 | */
|
|---|
| 852 | void *nic_get_specific(nic_t *nic_data)
|
|---|
| 853 | {
|
|---|
| 854 | return nic_data->specific;
|
|---|
| 855 | }
|
|---|
| 856 |
|
|---|
| 857 | /**
|
|---|
| 858 | * @param nic_data
|
|---|
| 859 | * @param specific The driver-specific structure for this NIC.
|
|---|
| 860 | */
|
|---|
| 861 | void nic_set_specific(nic_t *nic_data, void *specific)
|
|---|
| 862 | {
|
|---|
| 863 | nic_data->specific = specific;
|
|---|
| 864 | }
|
|---|
| 865 |
|
|---|
| 866 | /**
|
|---|
| 867 | * You can call the function only from one of the state change handlers.
|
|---|
| 868 | * @param nic_data
|
|---|
| 869 | * @return Current state of the NIC, prior to the actually executed change
|
|---|
| 870 | */
|
|---|
| 871 | nic_device_state_t nic_query_state(nic_t *nic_data)
|
|---|
| 872 | {
|
|---|
| 873 | return nic_data->state;
|
|---|
| 874 | }
|
|---|
| 875 |
|
|---|
| 876 | /**
|
|---|
| 877 | * @param nic_data
|
|---|
| 878 | * @return DDF device associated with this NIC.
|
|---|
| 879 | */
|
|---|
| 880 | ddf_dev_t *nic_get_ddf_dev(nic_t *nic_data)
|
|---|
| 881 | {
|
|---|
| 882 | return nic_data->dev;
|
|---|
| 883 | }
|
|---|
| 884 |
|
|---|
| 885 | /**
|
|---|
| 886 | * @param nic_data
|
|---|
| 887 | * @return DDF function associated with this NIC.
|
|---|
| 888 | */
|
|---|
| 889 | ddf_fun_t *nic_get_ddf_fun(nic_t *nic_data)
|
|---|
| 890 | {
|
|---|
| 891 | return nic_data->fun;
|
|---|
| 892 | }
|
|---|
| 893 |
|
|---|
| 894 | /**
|
|---|
| 895 | * @param nic_data
|
|---|
| 896 | * @param fun
|
|---|
| 897 | */
|
|---|
| 898 | void nic_set_ddf_fun(nic_t *nic_data, ddf_fun_t *fun)
|
|---|
| 899 | {
|
|---|
| 900 | nic_data->fun = fun;
|
|---|
| 901 | }
|
|---|
| 902 |
|
|---|
| 903 | /**
|
|---|
| 904 | * @param dev DDF device associated with NIC
|
|---|
| 905 | * @return The associated NIC structure
|
|---|
| 906 | */
|
|---|
| 907 | nic_t *nic_get_from_ddf_dev(ddf_dev_t *dev)
|
|---|
| 908 | {
|
|---|
| 909 | return (nic_t *) ddf_dev_data_get(dev);
|
|---|
| 910 | }
|
|---|
| 911 |
|
|---|
| 912 | /**
|
|---|
| 913 | * @param dev DDF function associated with NIC
|
|---|
| 914 | * @return The associated NIC structure
|
|---|
| 915 | */
|
|---|
| 916 | nic_t *nic_get_from_ddf_fun(ddf_fun_t *fun)
|
|---|
| 917 | {
|
|---|
| 918 | return (nic_t *) ddf_dev_data_get(ddf_fun_get_dev(fun));
|
|---|
| 919 | }
|
|---|
| 920 |
|
|---|
| 921 | /**
|
|---|
| 922 | * Raises the send_packets and send_bytes in device statistics.
|
|---|
| 923 | *
|
|---|
| 924 | * @param nic_data
|
|---|
| 925 | * @param packets Number of received packets
|
|---|
| 926 | * @param bytes Number of received bytes
|
|---|
| 927 | */
|
|---|
| 928 | void nic_report_send_ok(nic_t *nic_data, size_t packets, size_t bytes)
|
|---|
| 929 | {
|
|---|
| 930 | fibril_rwlock_write_lock(&nic_data->stats_lock);
|
|---|
| 931 | nic_data->stats.send_packets += packets;
|
|---|
| 932 | nic_data->stats.send_bytes += bytes;
|
|---|
| 933 | fibril_rwlock_write_unlock(&nic_data->stats_lock);
|
|---|
| 934 | }
|
|---|
| 935 |
|
|---|
| 936 | /**
|
|---|
| 937 | * Raises total error counter (send_errors) and the concrete send error counter
|
|---|
| 938 | * determined by the cause argument.
|
|---|
| 939 | *
|
|---|
| 940 | * @param nic_data
|
|---|
| 941 | * @param cause The concrete error cause.
|
|---|
| 942 | */
|
|---|
| 943 | void nic_report_send_error(nic_t *nic_data, nic_send_error_cause_t cause,
|
|---|
| 944 | unsigned count)
|
|---|
| 945 | {
|
|---|
| 946 | if (count == 0)
|
|---|
| 947 | return;
|
|---|
| 948 |
|
|---|
| 949 | fibril_rwlock_write_lock(&nic_data->stats_lock);
|
|---|
| 950 | nic_data->stats.send_errors += count;
|
|---|
| 951 | switch (cause) {
|
|---|
| 952 | case NIC_SEC_BUFFER_FULL:
|
|---|
| 953 | nic_data->stats.send_dropped += count;
|
|---|
| 954 | break;
|
|---|
| 955 | case NIC_SEC_ABORTED:
|
|---|
| 956 | nic_data->stats.send_aborted_errors += count;
|
|---|
| 957 | break;
|
|---|
| 958 | case NIC_SEC_CARRIER_LOST:
|
|---|
| 959 | nic_data->stats.send_carrier_errors += count;
|
|---|
| 960 | break;
|
|---|
| 961 | case NIC_SEC_FIFO_OVERRUN:
|
|---|
| 962 | nic_data->stats.send_fifo_errors += count;
|
|---|
| 963 | break;
|
|---|
| 964 | case NIC_SEC_HEARTBEAT:
|
|---|
| 965 | nic_data->stats.send_heartbeat_errors += count;
|
|---|
| 966 | break;
|
|---|
| 967 | case NIC_SEC_WINDOW_ERROR:
|
|---|
| 968 | nic_data->stats.send_window_errors += count;
|
|---|
| 969 | break;
|
|---|
| 970 | case NIC_SEC_OTHER:
|
|---|
| 971 | break;
|
|---|
| 972 | }
|
|---|
| 973 | fibril_rwlock_write_unlock(&nic_data->stats_lock);
|
|---|
| 974 | }
|
|---|
| 975 |
|
|---|
| 976 | /**
|
|---|
| 977 | * Raises total error counter (receive_errors) and the concrete receive error
|
|---|
| 978 | * counter determined by the cause argument.
|
|---|
| 979 | *
|
|---|
| 980 | * @param nic_data
|
|---|
| 981 | * @param cause The concrete error cause
|
|---|
| 982 | */
|
|---|
| 983 | void nic_report_receive_error(nic_t *nic_data,
|
|---|
| 984 | nic_receive_error_cause_t cause, unsigned count)
|
|---|
| 985 | {
|
|---|
| 986 | fibril_rwlock_write_lock(&nic_data->stats_lock);
|
|---|
| 987 | nic_data->stats.receive_errors += count;
|
|---|
| 988 | switch (cause) {
|
|---|
| 989 | case NIC_REC_BUFFER_FULL:
|
|---|
| 990 | nic_data->stats.receive_dropped += count;
|
|---|
| 991 | break;
|
|---|
| 992 | case NIC_REC_LENGTH:
|
|---|
| 993 | nic_data->stats.receive_length_errors += count;
|
|---|
| 994 | break;
|
|---|
| 995 | case NIC_REC_BUFFER_OVERFLOW:
|
|---|
| 996 | nic_data->stats.receive_dropped += count;
|
|---|
| 997 | break;
|
|---|
| 998 | case NIC_REC_CRC:
|
|---|
| 999 | nic_data->stats.receive_crc_errors += count;
|
|---|
| 1000 | break;
|
|---|
| 1001 | case NIC_REC_FRAME_ALIGNMENT:
|
|---|
| 1002 | nic_data->stats.receive_frame_errors += count;
|
|---|
| 1003 | break;
|
|---|
| 1004 | case NIC_REC_FIFO_OVERRUN:
|
|---|
| 1005 | nic_data->stats.receive_fifo_errors += count;
|
|---|
| 1006 | break;
|
|---|
| 1007 | case NIC_REC_MISSED:
|
|---|
| 1008 | nic_data->stats.receive_missed_errors += count;
|
|---|
| 1009 | break;
|
|---|
| 1010 | case NIC_REC_OTHER:
|
|---|
| 1011 | break;
|
|---|
| 1012 | }
|
|---|
| 1013 | fibril_rwlock_write_unlock(&nic_data->stats_lock);
|
|---|
| 1014 | }
|
|---|
| 1015 |
|
|---|
| 1016 | /**
|
|---|
| 1017 | * Raises the collisions counter in device statistics.
|
|---|
| 1018 | */
|
|---|
| 1019 | void nic_report_collisions(nic_t *nic_data, unsigned count)
|
|---|
| 1020 | {
|
|---|
| 1021 | fibril_rwlock_write_lock(&nic_data->stats_lock);
|
|---|
| 1022 | nic_data->stats.collisions += count;
|
|---|
| 1023 | fibril_rwlock_write_unlock(&nic_data->stats_lock);
|
|---|
| 1024 | }
|
|---|
| 1025 |
|
|---|
| 1026 | /** Just wrapper for checking nonzero time interval
|
|---|
| 1027 | *
|
|---|
| 1028 | * @oaram t The interval to check
|
|---|
| 1029 | * @returns Zero if the t is nonzero interval
|
|---|
| 1030 | * @returns Nonzero if t is zero interval
|
|---|
| 1031 | */
|
|---|
| 1032 | static int timeval_nonpositive(struct timeval t)
|
|---|
| 1033 | {
|
|---|
| 1034 | return (t.tv_sec <= 0) && (t.tv_usec <= 0);
|
|---|
| 1035 | }
|
|---|
| 1036 |
|
|---|
| 1037 | /** Main function of software period fibrill
|
|---|
| 1038 | *
|
|---|
| 1039 | * Just calls poll() in the nic->poll_period period
|
|---|
| 1040 | *
|
|---|
| 1041 | * @param data The NIC structure pointer
|
|---|
| 1042 | *
|
|---|
| 1043 | * @return 0, never reached
|
|---|
| 1044 | */
|
|---|
| 1045 | static errno_t period_fibril_fun(void *data)
|
|---|
| 1046 | {
|
|---|
| 1047 | nic_t *nic = data;
|
|---|
| 1048 | struct sw_poll_info *info = &nic->sw_poll_info;
|
|---|
| 1049 | while (true) {
|
|---|
| 1050 | fibril_rwlock_read_lock(&nic->main_lock);
|
|---|
| 1051 | int run = info->run;
|
|---|
| 1052 | int running = info->running;
|
|---|
| 1053 | struct timeval remaining = nic->poll_period;
|
|---|
| 1054 | fibril_rwlock_read_unlock(&nic->main_lock);
|
|---|
| 1055 |
|
|---|
| 1056 | if (!running) {
|
|---|
| 1057 | remaining.tv_sec = 5;
|
|---|
| 1058 | remaining.tv_usec = 0;
|
|---|
| 1059 | }
|
|---|
| 1060 |
|
|---|
| 1061 | /* Wait the period (keep attention to overflows) */
|
|---|
| 1062 | while (!timeval_nonpositive(remaining)) {
|
|---|
| 1063 | suseconds_t wait = 0;
|
|---|
| 1064 | if (remaining.tv_sec > 0) {
|
|---|
| 1065 | time_t wait_sec = remaining.tv_sec;
|
|---|
| 1066 | /*
|
|---|
| 1067 | * wait maximaly 5 seconds to get reasonable reaction time
|
|---|
| 1068 | * when period is reset
|
|---|
| 1069 | */
|
|---|
| 1070 | if (wait_sec > 5)
|
|---|
| 1071 | wait_sec = 5;
|
|---|
| 1072 |
|
|---|
| 1073 | wait = (suseconds_t) wait_sec * 1000000;
|
|---|
| 1074 |
|
|---|
| 1075 | remaining.tv_sec -= wait_sec;
|
|---|
| 1076 | } else {
|
|---|
| 1077 | wait = remaining.tv_usec;
|
|---|
| 1078 |
|
|---|
| 1079 | if (wait > 5 * 1000000) {
|
|---|
| 1080 | wait = 5 * 1000000;
|
|---|
| 1081 | }
|
|---|
| 1082 |
|
|---|
| 1083 | remaining.tv_usec -= wait;
|
|---|
| 1084 | }
|
|---|
| 1085 | async_usleep(wait);
|
|---|
| 1086 |
|
|---|
| 1087 | /* Check if the period was not reset */
|
|---|
| 1088 | if (info->run != run)
|
|---|
| 1089 | break;
|
|---|
| 1090 | }
|
|---|
| 1091 |
|
|---|
| 1092 | /* Provide polling if the period finished */
|
|---|
| 1093 | fibril_rwlock_read_lock(&nic->main_lock);
|
|---|
| 1094 | if (info->running && info->run == run) {
|
|---|
| 1095 | nic->on_poll_request(nic);
|
|---|
| 1096 | }
|
|---|
| 1097 | fibril_rwlock_read_unlock(&nic->main_lock);
|
|---|
| 1098 | }
|
|---|
| 1099 | return EOK;
|
|---|
| 1100 | }
|
|---|
| 1101 |
|
|---|
| 1102 | /** Starts software periodic polling
|
|---|
| 1103 | *
|
|---|
| 1104 | * Reset to new period if the original period was running
|
|---|
| 1105 | *
|
|---|
| 1106 | * @param nic_data Nic data structure
|
|---|
| 1107 | */
|
|---|
| 1108 | void nic_sw_period_start(nic_t *nic_data)
|
|---|
| 1109 | {
|
|---|
| 1110 | /* Create the fibril if it is not crated */
|
|---|
| 1111 | if (nic_data->sw_poll_info.fibril == 0) {
|
|---|
| 1112 | nic_data->sw_poll_info.fibril = fibril_create(period_fibril_fun,
|
|---|
| 1113 | nic_data);
|
|---|
| 1114 | nic_data->sw_poll_info.running = 0;
|
|---|
| 1115 | nic_data->sw_poll_info.run = 0;
|
|---|
| 1116 |
|
|---|
| 1117 | /* Start fibril */
|
|---|
| 1118 | fibril_add_ready(nic_data->sw_poll_info.fibril);
|
|---|
| 1119 | }
|
|---|
| 1120 |
|
|---|
| 1121 | /* Inform fibril about running with new period */
|
|---|
| 1122 | nic_data->sw_poll_info.run = (nic_data->sw_poll_info.run + 1) % 100;
|
|---|
| 1123 | nic_data->sw_poll_info.running = 1;
|
|---|
| 1124 | }
|
|---|
| 1125 |
|
|---|
| 1126 | /** Stops software periodic polling
|
|---|
| 1127 | *
|
|---|
| 1128 | * @param nic_data Nic data structure
|
|---|
| 1129 | */
|
|---|
| 1130 | void nic_sw_period_stop(nic_t *nic_data)
|
|---|
| 1131 | {
|
|---|
| 1132 | nic_data->sw_poll_info.running = 0;
|
|---|
| 1133 | }
|
|---|
| 1134 |
|
|---|
| 1135 | /** @}
|
|---|
| 1136 | */
|
|---|