source: mainline/uspace/lib/nic/src/nic_driver.c@ 87a3df7f

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

Convert NIC drivers away from DDF_DATA_IMPLANT.

  • Property mode set to 100644
File size: 30.5 KB
Line 
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
56nic_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 */
63int 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 */
83void 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 */
158void 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 */
171void 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 */
191void 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 */
213void 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 */
228void 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 negative error code
244 */
245int 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_create(dev, EXCHANGE_SERIALIZE);
252 if (parent_sess == NULL)
253 return EPARTY;
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 */
264nic_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 */
298void 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 */
325nic_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
349static 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 */
370void 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 */
386nic_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 */
401int nic_report_poll_mode(nic_t *nic_data, nic_poll_mode_t mode,
402 struct timeval *period)
403{
404 int 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 */
425int 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 int rc = nic_ev_addr_changed(nic_data->client_session,
437 address);
438 if (rc != EOK) {
439 fibril_rwlock_write_unlock(&nic_data->main_lock);
440 return rc;
441 }
442 }
443
444 fibril_rwlock_write_lock(&nic_data->rxc_lock);
445
446 /*
447 * The initial address (all zeroes) shouldn't be
448 * there and we will ignore that error -- in next
449 * calls this should not happen.
450 */
451 int rc = nic_rxc_set_addr(&nic_data->rx_control,
452 &nic_data->mac, address);
453
454 /* For the first time also record the default MAC */
455 if (MAC_IS_ZERO(nic_data->default_mac.address)) {
456 assert(MAC_IS_ZERO(nic_data->mac.address));
457 memcpy(&nic_data->default_mac, address, sizeof(nic_address_t));
458 }
459
460 fibril_rwlock_write_unlock(&nic_data->rxc_lock);
461
462 if ((rc != EOK) && (rc != ENOENT)) {
463 fibril_rwlock_write_unlock(&nic_data->main_lock);
464 return rc;
465 }
466
467 memcpy(&nic_data->mac, address, sizeof(nic_address_t));
468
469 fibril_rwlock_write_unlock(&nic_data->main_lock);
470
471 return EOK;
472}
473
474/**
475 * Used to obtain devices MAC address.
476 *
477 * The main lock should be locked, otherwise the inconsistent address
478 * can be returend.
479 *
480 * @param nic_data The controller data
481 * @param address The output for address.
482 */
483void nic_query_address(nic_t *nic_data, nic_address_t *addr) {
484 if (!addr)
485 return;
486 if (!nic_data)
487 memset(addr, 0, sizeof(nic_address_t));
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 */
499void 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 */
518void nic_received_frame(nic_t *nic_data, nic_frame_t *frame)
519{
520 /* Note: this function must not lock main lock, because loopback driver
521 * calls it inside send_frame handler (with locked main lock) */
522 fibril_rwlock_read_lock(&nic_data->rxc_lock);
523 nic_frame_type_t frame_type;
524 int check = nic_rxc_check(&nic_data->rx_control, frame->data,
525 frame->size, &frame_type);
526 fibril_rwlock_read_unlock(&nic_data->rxc_lock);
527 /* Update statistics */
528 fibril_rwlock_write_lock(&nic_data->stats_lock);
529
530 if (nic_data->state == NIC_STATE_ACTIVE && check) {
531 nic_data->stats.receive_packets++;
532 nic_data->stats.receive_bytes += frame->size;
533 switch (frame_type) {
534 case NIC_FRAME_MULTICAST:
535 nic_data->stats.receive_multicast++;
536 break;
537 case NIC_FRAME_BROADCAST:
538 nic_data->stats.receive_broadcast++;
539 break;
540 default:
541 break;
542 }
543 fibril_rwlock_write_unlock(&nic_data->stats_lock);
544 nic_ev_received(nic_data->client_session, frame->data,
545 frame->size);
546 } else {
547 switch (frame_type) {
548 case NIC_FRAME_UNICAST:
549 nic_data->stats.receive_filtered_unicast++;
550 break;
551 case NIC_FRAME_MULTICAST:
552 nic_data->stats.receive_filtered_multicast++;
553 break;
554 case NIC_FRAME_BROADCAST:
555 nic_data->stats.receive_filtered_broadcast++;
556 break;
557 }
558 fibril_rwlock_write_unlock(&nic_data->stats_lock);
559 }
560 nic_release_frame(nic_data, frame);
561}
562
563/**
564 * Some NICs can receive multiple frames during single interrupt. These can
565 * send them in whole list of frames (actually nic_frame_t structures), then
566 * the list is deallocated and each frame is passed to the
567 * nic_received_packet function.
568 *
569 * @param nic_data
570 * @param frames List of received frames
571 */
572void nic_received_frame_list(nic_t *nic_data, nic_frame_list_t *frames)
573{
574 if (frames == NULL)
575 return;
576 while (!list_empty(frames)) {
577 nic_frame_t *frame =
578 list_get_instance(list_first(frames), nic_frame_t, link);
579
580 list_remove(&frame->link);
581 nic_received_frame(nic_data, frame);
582 }
583 nic_driver_release_frame_list(frames);
584}
585
586/** Allocate and initialize the driver data.
587 *
588 * @return Allocated structure or NULL.
589 *
590 */
591static nic_t *nic_create(ddf_dev_t *dev)
592{
593 nic_t *nic_data = ddf_dev_data_alloc(dev, sizeof(nic_t));
594 if (nic_data == NULL)
595 return NULL;
596
597 /* Force zero to all uninitialized fields (e.g. added in future) */
598 if (nic_rxc_init(&nic_data->rx_control) != EOK) {
599 return NULL;
600 }
601
602 if (nic_wol_virtues_init(&nic_data->wol_virtues) != EOK) {
603 return NULL;
604 }
605
606 nic_data->dev = NULL;
607 nic_data->fun = NULL;
608 nic_data->state = NIC_STATE_STOPPED;
609 nic_data->client_session = NULL;
610 nic_data->poll_mode = NIC_POLL_IMMEDIATE;
611 nic_data->default_poll_mode = NIC_POLL_IMMEDIATE;
612 nic_data->send_frame = NULL;
613 nic_data->on_activating = NULL;
614 nic_data->on_going_down = NULL;
615 nic_data->on_stopping = NULL;
616 nic_data->specific = NULL;
617
618 fibril_rwlock_initialize(&nic_data->main_lock);
619 fibril_rwlock_initialize(&nic_data->stats_lock);
620 fibril_rwlock_initialize(&nic_data->rxc_lock);
621 fibril_rwlock_initialize(&nic_data->wv_lock);
622
623 memset(&nic_data->mac, 0, sizeof(nic_address_t));
624 memset(&nic_data->default_mac, 0, sizeof(nic_address_t));
625 memset(&nic_data->stats, 0, sizeof(nic_device_stats_t));
626
627 return nic_data;
628}
629
630/** Create NIC structure for the device and bind it to dev_fun_t
631 *
632 * The pointer to the created and initialized NIC structure will
633 * be stored in device->nic_data.
634 *
635 * @param device The NIC device structure
636 *
637 * @return Pointer to created nic_t structure or NULL
638 *
639 */
640nic_t *nic_create_and_bind(ddf_dev_t *device)
641{
642 nic_t *nic_data = nic_create(device);
643 if (!nic_data)
644 return NULL;
645
646 nic_data->dev = device;
647
648 return nic_data;
649}
650
651/**
652 * Hangs up the phones in the structure, deallocates specific data and then
653 * the structure itself.
654 *
655 * @param data
656 */
657static void nic_destroy(nic_t *nic_data)
658{
659 free(nic_data->specific);
660}
661
662/**
663 * Unbind and destroy nic_t stored in ddf_dev_t.nic_data.
664 * The ddf_dev_t.nic_data will be set to NULL, specific driver data will be
665 * destroyed.
666 *
667 * @param device The NIC device structure
668 */
669void nic_unbind_and_destroy(ddf_dev_t *device)
670{
671 nic_destroy(nic_get_from_ddf_dev(device));
672 return;
673}
674
675/**
676 * Set information about current HW filtering.
677 * 1 ... Only those frames we want to receive are passed through HW
678 * 0 ... The HW filtering is imperfect
679 * -1 ... Don't change the setting
680 * Can be called only from the on_*_change handler.
681 *
682 * @param nic_data
683 * @param unicast_exact Unicast frames
684 * @param mcast_exact Multicast frames
685 * @param vlan_exact VLAN tags
686 */
687void nic_report_hw_filtering(nic_t *nic_data,
688 int unicast_exact, int multicast_exact, int vlan_exact)
689{
690 nic_rxc_hw_filtering(&nic_data->rx_control,
691 unicast_exact, multicast_exact, vlan_exact);
692}
693
694/**
695 * Computes hash for the address list based on standard multicast address
696 * hashing.
697 *
698 * @param address_list
699 * @param count
700 *
701 * @return Multicast hash
702 *
703 * @see multicast_hash
704 */
705uint64_t nic_mcast_hash(const nic_address_t *list, size_t count)
706{
707 return nic_rxc_mcast_hash(list, count);
708}
709
710/**
711 * Computes hash for multicast addresses currently set up in the RX multicast
712 * filtering. For promiscuous mode returns all ones, for blocking all zeroes.
713 * Can be called only from the state change handlers (on_activating,
714 * on_going_down and on_stopping).
715 *
716 * @param nic_data
717 *
718 * @return Multicast hash
719 *
720 * @see multicast_hash
721 */
722uint64_t nic_query_mcast_hash(nic_t *nic_data)
723{
724 fibril_rwlock_read_lock(&nic_data->rxc_lock);
725 uint64_t hash = nic_rxc_multicast_get_hash(&nic_data->rx_control);
726 fibril_rwlock_read_unlock(&nic_data->rxc_lock);
727 return hash;
728}
729
730/**
731 * Queries the current mode of unicast frames receiving.
732 * Can be called only from the on_*_change handler.
733 *
734 * @param nic_data
735 * @param mode The new unicast mode
736 * @param max_count Max number of addresses that can be written into the
737 * address_list.
738 * @param address_list List of MAC addresses or NULL.
739 * @param address_count Number of addresses in the list
740 */
741void nic_query_unicast(const nic_t *nic_data,
742 nic_unicast_mode_t *mode,
743 size_t max_count, nic_address_t *address_list, size_t *address_count)
744{
745 assert(mode != NULL);
746 nic_rxc_unicast_get_mode(&nic_data->rx_control, mode,
747 max_count, address_list, address_count);
748}
749
750/**
751 * Queries the current mode of multicast frames receiving.
752 * Can be called only from the on_*_change handler.
753 *
754 * @param nic_data
755 * @param mode The current multicast mode
756 * @param max_count Max number of addresses that can be written into the
757 * address_list.
758 * @param address_list List of MAC addresses or NULL.
759 * @param address_count Number of addresses in the list
760 */
761void nic_query_multicast(const nic_t *nic_data,
762 nic_multicast_mode_t *mode,
763 size_t max_count, nic_address_t *address_list, size_t *address_count)
764{
765 assert(mode != NULL);
766 nic_rxc_multicast_get_mode(&nic_data->rx_control, mode,
767 max_count, address_list, address_count);
768}
769
770/**
771 * Queries the current mode of broadcast frames receiving.
772 * Can be called only from the on_*_change handler.
773 *
774 * @param nic_data
775 * @param mode The new broadcast mode
776 */
777void nic_query_broadcast(const nic_t *nic_data,
778 nic_broadcast_mode_t *mode)
779{
780 assert(mode != NULL);
781 nic_rxc_broadcast_get_mode(&nic_data->rx_control, mode);
782}
783
784/**
785 * Queries the current blocked source addresses.
786 * Can be called only from the on_*_change handler.
787 *
788 * @param nic_data
789 * @param max_count Max number of addresses that can be written into the
790 * address_list.
791 * @param address_list List of MAC addresses or NULL.
792 * @param address_count Number of addresses in the list
793 */
794void nic_query_blocked_sources(const nic_t *nic_data,
795 size_t max_count, nic_address_t *address_list, size_t *address_count)
796{
797 nic_rxc_blocked_sources_get(&nic_data->rx_control,
798 max_count, address_list, address_count);
799}
800
801/**
802 * Query mask used for filtering according to the VLAN tags.
803 * Can be called only from the on_*_change handler.
804 *
805 * @param nic_data
806 * @param mask Must be 512 bytes long
807 *
808 * @return EOK
809 * @return ENOENT
810 */
811int nic_query_vlan_mask(const nic_t *nic_data, nic_vlan_mask_t *mask)
812{
813 assert(mask);
814 return nic_rxc_vlan_get_mask(&nic_data->rx_control, mask);
815}
816
817/**
818 * Query maximum number of WOL virtues of specified type allowed on the device.
819 * Can be called only from add_device and on_wol_virtue_* handlers.
820 *
821 * @param nic_data
822 * @param type The type of the WOL virtues
823 *
824 * @return Maximal number of allowed virtues of this type. -1 means this type
825 * is not supported at all.
826 */
827int nic_query_wol_max_caps(const nic_t *nic_data, nic_wv_type_t type)
828{
829 return nic_data->wol_virtues.caps_max[type];
830}
831
832/**
833 * Sets maximum number of WOL virtues of specified type allowed on the device.
834 * Can be called only from add_device and on_wol_virtue_* handlers.
835 *
836 * @param nic_data
837 * @param type The type of the WOL virtues
838 * @param count Maximal number of allowed virtues of this type. -1 means
839 * this type is not supported at all.
840 */
841void nic_set_wol_max_caps(nic_t *nic_data, nic_wv_type_t type, int count)
842{
843 nic_data->wol_virtues.caps_max[type] = count;
844}
845
846/**
847 * @param nic_data
848 * @return The driver-specific structure for this NIC.
849 */
850void *nic_get_specific(nic_t *nic_data)
851{
852 return nic_data->specific;
853}
854
855/**
856 * @param nic_data
857 * @param specific The driver-specific structure for this NIC.
858 */
859void nic_set_specific(nic_t *nic_data, void *specific)
860{
861 nic_data->specific = specific;
862}
863
864/**
865 * You can call the function only from one of the state change handlers.
866 * @param nic_data
867 * @return Current state of the NIC, prior to the actually executed change
868 */
869nic_device_state_t nic_query_state(nic_t *nic_data)
870{
871 return nic_data->state;
872}
873
874/**
875 * @param nic_data
876 * @return DDF device associated with this NIC.
877 */
878ddf_dev_t *nic_get_ddf_dev(nic_t *nic_data)
879{
880 return nic_data->dev;
881}
882
883/**
884 * @param nic_data
885 * @return DDF function associated with this NIC.
886 */
887ddf_fun_t *nic_get_ddf_fun(nic_t *nic_data)
888{
889 return nic_data->fun;
890}
891
892/**
893 * @param nic_data
894 * @param fun
895 */
896void nic_set_ddf_fun(nic_t *nic_data, ddf_fun_t *fun)
897{
898 nic_data->fun = fun;
899}
900
901/**
902 * @param dev DDF device associated with NIC
903 * @return The associated NIC structure
904 */
905nic_t *nic_get_from_ddf_dev(ddf_dev_t *dev)
906{
907 return (nic_t *) ddf_dev_data_get(dev);
908}
909
910/**
911 * @param dev DDF function associated with NIC
912 * @return The associated NIC structure
913 */
914nic_t *nic_get_from_ddf_fun(ddf_fun_t *fun)
915{
916 return (nic_t *) ddf_dev_data_get(ddf_fun_get_dev(fun));
917}
918
919/**
920 * Raises the send_packets and send_bytes in device statistics.
921 *
922 * @param nic_data
923 * @param packets Number of received packets
924 * @param bytes Number of received bytes
925 */
926void nic_report_send_ok(nic_t *nic_data, size_t packets, size_t bytes)
927{
928 fibril_rwlock_write_lock(&nic_data->stats_lock);
929 nic_data->stats.send_packets += packets;
930 nic_data->stats.send_bytes += bytes;
931 fibril_rwlock_write_unlock(&nic_data->stats_lock);
932}
933
934/**
935 * Raises total error counter (send_errors) and the concrete send error counter
936 * determined by the cause argument.
937 *
938 * @param nic_data
939 * @param cause The concrete error cause.
940 */
941void nic_report_send_error(nic_t *nic_data, nic_send_error_cause_t cause,
942 unsigned count)
943{
944 if (count == 0)
945 return;
946
947 fibril_rwlock_write_lock(&nic_data->stats_lock);
948 nic_data->stats.send_errors += count;
949 switch (cause) {
950 case NIC_SEC_BUFFER_FULL:
951 nic_data->stats.send_dropped += count;
952 break;
953 case NIC_SEC_ABORTED:
954 nic_data->stats.send_aborted_errors += count;
955 break;
956 case NIC_SEC_CARRIER_LOST:
957 nic_data->stats.send_carrier_errors += count;
958 break;
959 case NIC_SEC_FIFO_OVERRUN:
960 nic_data->stats.send_fifo_errors += count;
961 break;
962 case NIC_SEC_HEARTBEAT:
963 nic_data->stats.send_heartbeat_errors += count;
964 break;
965 case NIC_SEC_WINDOW_ERROR:
966 nic_data->stats.send_window_errors += count;
967 break;
968 case NIC_SEC_OTHER:
969 break;
970 }
971 fibril_rwlock_write_unlock(&nic_data->stats_lock);
972}
973
974/**
975 * Raises total error counter (receive_errors) and the concrete receive error
976 * counter determined by the cause argument.
977 *
978 * @param nic_data
979 * @param cause The concrete error cause
980 */
981void nic_report_receive_error(nic_t *nic_data,
982 nic_receive_error_cause_t cause, unsigned count)
983{
984 fibril_rwlock_write_lock(&nic_data->stats_lock);
985 nic_data->stats.receive_errors += count;
986 switch (cause) {
987 case NIC_REC_BUFFER_FULL:
988 nic_data->stats.receive_dropped += count;
989 break;
990 case NIC_REC_LENGTH:
991 nic_data->stats.receive_length_errors += count;
992 break;
993 case NIC_REC_BUFFER_OVERFLOW:
994 nic_data->stats.receive_dropped += count;
995 break;
996 case NIC_REC_CRC:
997 nic_data->stats.receive_crc_errors += count;
998 break;
999 case NIC_REC_FRAME_ALIGNMENT:
1000 nic_data->stats.receive_frame_errors += count;
1001 break;
1002 case NIC_REC_FIFO_OVERRUN:
1003 nic_data->stats.receive_fifo_errors += count;
1004 break;
1005 case NIC_REC_MISSED:
1006 nic_data->stats.receive_missed_errors += count;
1007 break;
1008 case NIC_REC_OTHER:
1009 break;
1010 }
1011 fibril_rwlock_write_unlock(&nic_data->stats_lock);
1012}
1013
1014/**
1015 * Raises the collisions counter in device statistics.
1016 */
1017void nic_report_collisions(nic_t *nic_data, unsigned count)
1018{
1019 fibril_rwlock_write_lock(&nic_data->stats_lock);
1020 nic_data->stats.collisions += count;
1021 fibril_rwlock_write_unlock(&nic_data->stats_lock);
1022}
1023
1024/** Just wrapper for checking nonzero time interval
1025 *
1026 * @oaram t The interval to check
1027 * @returns Zero if the t is nonzero interval
1028 * @returns Nonzero if t is zero interval
1029 */
1030static int timeval_nonpositive(struct timeval t) {
1031 return (t.tv_sec <= 0) && (t.tv_usec <= 0);
1032}
1033
1034/** Main function of software period fibrill
1035 *
1036 * Just calls poll() in the nic->poll_period period
1037 *
1038 * @param data The NIC structure pointer
1039 *
1040 * @return 0, never reached
1041 */
1042static int period_fibril_fun(void *data)
1043{
1044 nic_t *nic = data;
1045 struct sw_poll_info *info = &nic->sw_poll_info;
1046 while (true) {
1047 fibril_rwlock_read_lock(&nic->main_lock);
1048 int run = info->run;
1049 int running = info->running;
1050 struct timeval remaining = nic->poll_period;
1051 fibril_rwlock_read_unlock(&nic->main_lock);
1052
1053 if (!running) {
1054 remaining.tv_sec = 5;
1055 remaining.tv_usec = 0;
1056 }
1057
1058 /* Wait the period (keep attention to overflows) */
1059 while (!timeval_nonpositive(remaining)) {
1060 suseconds_t wait = 0;
1061 if (remaining.tv_sec > 0) {
1062 time_t wait_sec = remaining.tv_sec;
1063 /* wait maximaly 5 seconds to get reasonable reaction time
1064 * when period is reset
1065 */
1066 if (wait_sec > 5)
1067 wait_sec = 5;
1068
1069 wait = (suseconds_t) wait_sec * 1000000;
1070
1071 remaining.tv_sec -= wait_sec;
1072 } else {
1073 wait = remaining.tv_usec;
1074
1075 if (wait > 5 * 1000000) {
1076 wait = 5 * 1000000;
1077 }
1078
1079 remaining.tv_usec -= wait;
1080 }
1081 async_usleep(wait);
1082
1083 /* Check if the period was not reset */
1084 if (info->run != run)
1085 break;
1086 }
1087
1088 /* Provide polling if the period finished */
1089 fibril_rwlock_read_lock(&nic->main_lock);
1090 if (info->running && info->run == run) {
1091 nic->on_poll_request(nic);
1092 }
1093 fibril_rwlock_read_unlock(&nic->main_lock);
1094 }
1095 return 0;
1096}
1097
1098/** Starts software periodic polling
1099 *
1100 * Reset to new period if the original period was running
1101 *
1102 * @param nic_data Nic data structure
1103 */
1104void nic_sw_period_start(nic_t *nic_data)
1105{
1106 /* Create the fibril if it is not crated */
1107 if (nic_data->sw_poll_info.fibril == 0) {
1108 nic_data->sw_poll_info.fibril = fibril_create(period_fibril_fun,
1109 nic_data);
1110 nic_data->sw_poll_info.running = 0;
1111 nic_data->sw_poll_info.run = 0;
1112
1113 /* Start fibril */
1114 fibril_add_ready(nic_data->sw_poll_info.fibril);
1115 }
1116
1117 /* Inform fibril about running with new period */
1118 nic_data->sw_poll_info.run = (nic_data->sw_poll_info.run + 1) % 100;
1119 nic_data->sw_poll_info.running = 1;
1120}
1121
1122/** Stops software periodic polling
1123 *
1124 * @param nic_data Nic data structure
1125 */
1126void nic_sw_period_stop(nic_t *nic_data)
1127{
1128 nic_data->sw_poll_info.running = 0;
1129}
1130
1131/** @}
1132 */
Note: See TracBrowser for help on using the repository browser.