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

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

Make MAC address change possible on ethip side. Merge mainline changes.

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