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

Last change on this file since 8765039 was 8765039, checked in by Nataliia Korop <n.corop08@…>, 8 months ago

ugly address

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