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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d7dadcb4 was a41b691, checked in by Agnieszka Tabaka <nufcia@…>, 11 years ago

Remove temporary log messages.

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