source: mainline/uspace/lib/nic/src/nic_impl.c@ 612af1a0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 612af1a0 was 00d7e1b, checked in by Martin Decky <martin@…>, 14 years ago

networking improvements

  • start the networking stack from init
  • add loopback network interface driver (cherrypicked and sanitized from lp:~helenos-nicf/helenos/nicf)
  • add libnic and various small pieces from lp:~helenos-nicf/helenos/nicf
  • fix client side of NIC_GET_ADDRESS
  • net binary overhaul

Note: "ping 127.0.0.1" works, but the first three pings timeout for some reason

  • Property mode set to 100644
File size: 25.4 KB
Line 
1/*
2 * Copyright (c) 2011 Radim Vansa
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * @addtogroup libnic
31 * @{
32 */
33/**
34 * @file
35 * @brief Default DDF NIC interface methods implementations
36 */
37
38#include <str_error.h>
39#include <ipc/services.h>
40#include <ns.h>
41#include <packet_client.h>
42#include <packet_remote.h>
43#include "nic_driver.h"
44#include "nic_impl.h"
45
46/**
47 * Default implementation of the set_state method. Trivial.
48 *
49 * @param fun
50 * @param[out] state
51 *
52 * @return EOK always.
53 */
54int nic_get_state_impl(ddf_fun_t *fun, nic_device_state_t *state)
55{
56 nic_t *nic_data = (nic_t *) fun->driver_data;
57 fibril_rwlock_read_lock(&nic_data->main_lock);
58 *state = nic_data->state;
59 fibril_rwlock_read_unlock(&nic_data->main_lock);
60 return EOK;
61}
62
63/**
64 * Default implementation of the set_state method. Changes the internal
65 * driver's state, calls the appropriate callback and notifies the NIL service
66 * about this change.
67 *
68 * @param fun
69 * @param state The new device's state
70 *
71 * @return EOK If the state was changed
72 * @return EINVAL If the state cannot be changed
73 */
74int nic_set_state_impl(ddf_fun_t *fun, nic_device_state_t state)
75{
76 if (state >= NIC_STATE_MAX) {
77 return EINVAL;
78 }
79
80 nic_t *nic_data = (nic_t *) fun->driver_data;
81
82 fibril_rwlock_write_lock(&nic_data->main_lock);
83 if (nic_data->state == state) {
84 /* No change, nothing to do */
85 fibril_rwlock_write_unlock(&nic_data->main_lock);
86 return EOK;
87 }
88 if (state == NIC_STATE_ACTIVE) {
89 if (nic_data->nil_session == NULL || nic_data->net_session == NULL
90 || nic_data->device_id < 0) {
91 fibril_rwlock_write_unlock(&nic_data->main_lock);
92 return EINVAL;
93 }
94 }
95
96 state_change_handler event_handler = NULL;
97 switch (state) {
98 case NIC_STATE_STOPPED:
99 event_handler = nic_data->on_stopping;
100 break;
101 case NIC_STATE_DOWN:
102 event_handler = nic_data->on_going_down;
103 break;
104 case NIC_STATE_ACTIVE:
105 event_handler = nic_data->on_activating;
106 break;
107 default:
108 break;
109 }
110 if (event_handler != NULL) {
111 int rc = event_handler(nic_data);
112 if (rc != EOK) {
113 fibril_rwlock_write_unlock(&nic_data->main_lock);
114 return EINVAL;
115 }
116 }
117
118 if (state == NIC_STATE_STOPPED) {
119 /* Notify upper layers that we are reseting the MAC */
120 int rc = nil_addr_changed_msg(nic_data->nil_session,
121 nic_data->device_id, &nic_data->default_mac);
122 nic_data->poll_mode = nic_data->default_poll_mode;
123 memcpy(&nic_data->poll_period, &nic_data->default_poll_period,
124 sizeof (struct timeval));
125 if (rc != EOK) {
126 /* We have already ran the on stopped handler, even if we
127 * terminated the state change we would end up in undefined state.
128 * Therefore we just log the problem. */
129 }
130
131 fibril_rwlock_write_lock(&nic_data->stats_lock);
132 bzero(&nic_data->stats, sizeof (nic_device_stats_t));
133 fibril_rwlock_write_unlock(&nic_data->stats_lock);
134
135 fibril_rwlock_write_lock(&nic_data->rxc_lock);
136 nic_rxc_clear(&nic_data->rx_control);
137 /* Reinsert device's default MAC */
138 nic_rxc_set_addr(&nic_data->rx_control, NULL,
139 &nic_data->default_mac);
140 fibril_rwlock_write_unlock(&nic_data->rxc_lock);
141 memcpy(&nic_data->mac, &nic_data->default_mac, sizeof (nic_address_t));
142
143 fibril_rwlock_write_lock(&nic_data->wv_lock);
144 nic_wol_virtues_clear(&nic_data->wol_virtues);
145 fibril_rwlock_write_unlock(&nic_data->wv_lock);
146
147 /* Ensure stopping period of NIC_POLL_SOFTWARE_PERIODIC */
148 nic_sw_period_stop(nic_data);
149 }
150
151 nic_data->state = state;
152
153 nil_device_state_msg(nic_data->nil_session, nic_data->device_id, state);
154
155 fibril_rwlock_write_unlock(&nic_data->main_lock);
156
157 return EOK;
158}
159
160/**
161 * Default implementation of the send_message method.
162 * Send messages to the network.
163 *
164 * @param fun
165 * @param packet_id ID of the first packet in a queue of sent packets
166 *
167 * @return EOK If the message was sent
168 * @return EBUSY If the device is not in state when the packet can be set.
169 * @return EINVAL If the packet ID is invalid
170 */
171int nic_send_message_impl(ddf_fun_t *fun, packet_id_t packet_id)
172{
173 nic_t *nic_data = (nic_t *) fun->driver_data;
174 packet_t *packet, *next;
175
176 fibril_rwlock_read_lock(&nic_data->main_lock);
177 if (nic_data->state != NIC_STATE_ACTIVE || nic_data->tx_busy) {
178 fibril_rwlock_read_unlock(&nic_data->main_lock);
179 pq_release_remote(nic_data->net_session, packet_id);
180 return EBUSY;
181 }
182
183 int rc = packet_translate_remote(nic_data->net_session, &packet, packet_id);
184
185 if (rc != EOK) {
186 fibril_rwlock_read_unlock(&nic_data->main_lock);
187 return EINVAL;
188 }
189
190 /*
191 * Process the packet queue. Each sent packet must be detached from the
192 * queue and destroyed. This is why the cycle differs from loopback's
193 * cycle, where the packets are immediately used in upper layers and
194 * therefore they must not be destroyed (released).
195 */
196 assert(nic_data->write_packet != NULL);
197 do {
198 next = pq_detach(packet);
199 nic_data->write_packet(nic_data, packet);
200 packet = next;
201 } while (packet);
202 fibril_rwlock_read_unlock(&nic_data->main_lock);
203 return EOK;
204}
205
206/**
207 * Default implementation of the connect_to_nil method.
208 * Connects the driver to the NIL service.
209 *
210 * @param fun
211 * @param nil_service ID of the server implementing the NIL service
212 * @param device_id ID of the device as used in higher layers
213 *
214 * @return EOK If the services were bound
215 * @return Negative error code from service_connect_blocking
216 */
217int nic_connect_to_nil_impl(ddf_fun_t *fun, services_t nil_service,
218 nic_device_id_t device_id)
219{
220 nic_t *nic_data = (nic_t *) fun->driver_data;
221 fibril_rwlock_write_lock(&nic_data->main_lock);
222
223 nic_data->device_id = device_id;
224
225 nic_data->nil_session = service_connect_blocking(EXCHANGE_SERIALIZE,
226 nil_service, 0, 0);
227 if (nic_data->nil_session != NULL) {
228 fibril_rwlock_write_unlock(&nic_data->main_lock);
229 return EOK;
230 }
231
232 fibril_rwlock_write_unlock(&nic_data->main_lock);
233 return EHANGUP;
234}
235
236/**
237 * Default implementation of the get_address method.
238 * Retrieves the NIC's physical address.
239 *
240 * @param fun
241 * @param address Pointer to the structure where the address will be stored.
242 *
243 * @return EOK If the services were bound
244 * @return ELIMIT If the buffer is too short
245 */
246int nic_get_address_impl(ddf_fun_t *fun, nic_address_t *address)
247{
248 assert(address);
249 nic_t *nic_data = (nic_t *) fun->driver_data;
250 fibril_rwlock_read_lock(&nic_data->main_lock);
251 memcpy(address, &nic_data->mac, sizeof (nic_address_t));
252 fibril_rwlock_read_unlock(&nic_data->main_lock);
253 return EOK;
254}
255
256/**
257 * Default implementation of the get_stats method. Copies the statistics from
258 * the drivers data to supplied buffer.
259 *
260 * @param fun
261 * @param[out] stats The buffer for statistics
262 *
263 * @return EOK (cannot fail)
264 */
265int nic_get_stats_impl(ddf_fun_t *fun, nic_device_stats_t *stats)
266{
267 nic_t *nic_data = (nic_t *) fun->driver_data;
268 assert (stats != NULL);
269 fibril_rwlock_read_lock(&nic_data->stats_lock);
270 memcpy(stats, &nic_data->stats, sizeof (nic_device_stats_t));
271 fibril_rwlock_read_unlock(&nic_data->stats_lock);
272 return EOK;
273}
274
275/**
276 * Default implementation of unicast_get_mode method.
277 *
278 * @param fun
279 * @param[out] mode Current operation mode
280 * @param[in] max_count Max number of addresses that can be written into the
281 * buffer (addr_list).
282 * @param[out] addr_list Buffer for addresses
283 * @param[out] addr_count Number of addresses written into the list
284 *
285 * @return EOK
286 */
287int nic_unicast_get_mode_impl(ddf_fun_t *fun, nic_unicast_mode_t *mode,
288 size_t max_count, nic_address_t *addr_list, size_t *addr_count)
289{
290 nic_t *nic_data = (nic_t *) fun->driver_data;
291 fibril_rwlock_read_lock(&nic_data->rxc_lock);
292 nic_rxc_unicast_get_mode(&nic_data->rx_control, mode, max_count,
293 addr_list, addr_count);
294 fibril_rwlock_read_unlock(&nic_data->rxc_lock);
295 return EOK;
296}
297
298/**
299 * Default implementation of unicast_set_mode method.
300 *
301 * @param fun
302 * @param[in] mode New operation mode
303 * @param[in] addr_list List of unicast addresses
304 * @param[in] addr_count Number of addresses in the list
305 *
306 * @return EOK
307 * @return EINVAL
308 * @return ENOTSUP
309 * @return ENOMEM
310 */
311int nic_unicast_set_mode_impl(ddf_fun_t *fun,
312 nic_unicast_mode_t mode, const nic_address_t *addr_list, size_t addr_count)
313{
314 assert((addr_count == 0 && addr_list == NULL)
315 || (addr_count != 0 && addr_list != NULL));
316 size_t i;
317 for (i = 0; i < addr_count; ++i) {
318 if (addr_list[i].address[0] & 1)
319 return EINVAL;
320 }
321
322 nic_t *nic_data = (nic_t *) fun->driver_data;
323 fibril_rwlock_write_lock(&nic_data->rxc_lock);
324 int rc = ENOTSUP;
325 if (nic_data->on_unicast_mode_change) {
326 rc = nic_data->on_unicast_mode_change(nic_data,
327 mode, addr_list, addr_count);
328 }
329 if (rc == EOK) {
330 rc = nic_rxc_unicast_set_mode(&nic_data->rx_control, mode,
331 addr_list, addr_count);
332 /* After changing the mode the addr db gets cleared, therefore we have
333 * to reinsert also the physical address of NIC.
334 */
335 nic_rxc_set_addr(&nic_data->rx_control, NULL, &nic_data->mac);
336 }
337 fibril_rwlock_write_unlock(&nic_data->rxc_lock);
338 return rc;
339}
340
341
342/**
343 * Default implementation of multicast_get_mode method.
344 *
345 * @param fun
346 * @param[out] mode Current operation mode
347 * @param[in] max_count Max number of addresses that can be written into the
348 * buffer (addr_list).
349 * @param[out] addr_list Buffer for addresses
350 * @param[out] addr_count Number of addresses written into the list
351 *
352 * @return EOK
353 */
354int nic_multicast_get_mode_impl(ddf_fun_t *fun, nic_multicast_mode_t *mode,
355 size_t max_count, nic_address_t *addr_list, size_t *addr_count)
356{
357 nic_t *nic_data = (nic_t *) fun->driver_data;
358 fibril_rwlock_read_lock(&nic_data->rxc_lock);
359 nic_rxc_multicast_get_mode(&nic_data->rx_control, mode, max_count,
360 addr_list, addr_count);
361 fibril_rwlock_read_unlock(&nic_data->rxc_lock);
362 return EOK;
363}
364
365/**
366 * Default implementation of multicast_set_mode method.
367 *
368 * @param fun
369 * @param[in] mode New operation mode
370 * @param[in] addr_list List of multicast addresses
371 * @param[in] addr_count Number of addresses in the list
372 *
373 * @return EOK
374 * @return EINVAL
375 * @return ENOTSUP
376 * @return ENOMEM
377 */
378int nic_multicast_set_mode_impl(ddf_fun_t *fun, nic_multicast_mode_t mode,
379 const nic_address_t *addr_list, size_t addr_count)
380{
381 assert((addr_count == 0 && addr_list == NULL)
382 || (addr_count != 0 && addr_list != NULL));
383 size_t i;
384 for (i = 0; i < addr_count; ++i) {
385 if (!(addr_list[i].address[0] & 1))
386 return EINVAL;
387 }
388
389 nic_t *nic_data = (nic_t *) fun->dev->driver_data;
390 fibril_rwlock_write_lock(&nic_data->rxc_lock);
391 int rc = ENOTSUP;
392 if (nic_data->on_multicast_mode_change) {
393 rc = nic_data->on_multicast_mode_change(nic_data, mode, addr_list, addr_count);
394 }
395 if (rc == EOK) {
396 rc = nic_rxc_multicast_set_mode(&nic_data->rx_control, mode,
397 addr_list, addr_count);
398 }
399 fibril_rwlock_write_unlock(&nic_data->rxc_lock);
400 return rc;
401}
402
403/**
404 * Default implementation of broadcast_get_mode method.
405 *
406 * @param fun
407 * @param[out] mode Current operation mode
408 *
409 * @return EOK
410 */
411int nic_broadcast_get_mode_impl(ddf_fun_t *fun, nic_broadcast_mode_t *mode)
412{
413 nic_t *nic_data = (nic_t *) fun->driver_data;
414 fibril_rwlock_read_lock(&nic_data->rxc_lock);
415 nic_rxc_broadcast_get_mode(&nic_data->rx_control, mode);
416 fibril_rwlock_read_unlock(&nic_data->rxc_lock);
417 return EOK;
418}
419
420/**
421 * Default implementation of broadcast_set_mode method.
422 *
423 * @param fun
424 * @param[in] mode New operation mode
425 *
426 * @return EOK
427 * @return EINVAL
428 * @return ENOTSUP
429 * @return ENOMEM
430 */
431int nic_broadcast_set_mode_impl(ddf_fun_t *fun, nic_broadcast_mode_t mode)
432{
433 nic_t *nic_data = (nic_t *) fun->driver_data;
434 fibril_rwlock_write_lock(&nic_data->rxc_lock);
435 int rc = ENOTSUP;
436 if (nic_data->on_broadcast_mode_change) {
437 rc = nic_data->on_broadcast_mode_change(nic_data, mode);
438 }
439 if (rc == EOK) {
440 rc = nic_rxc_broadcast_set_mode(&nic_data->rx_control, mode);
441 }
442 fibril_rwlock_write_unlock(&nic_data->rxc_lock);
443 return rc;
444}
445
446/**
447 * Default implementation of blocked_sources_get method.
448 *
449 * @param fun
450 * @param[in] max_count Max number of addresses that can be written into the
451 * buffer (addr_list).
452 * @param[out] addr_list Buffer for addresses
453 * @param[out] addr_count Number of addresses written into the list
454 *
455 * @return EOK
456 */
457int nic_blocked_sources_get_impl(ddf_fun_t *fun,
458 size_t max_count, nic_address_t *addr_list, size_t *addr_count)
459{
460 nic_t *nic_data = (nic_t *) fun->driver_data;
461 fibril_rwlock_read_lock(&nic_data->rxc_lock);
462 nic_rxc_blocked_sources_get(&nic_data->rx_control,
463 max_count, addr_list, addr_count);
464 fibril_rwlock_read_unlock(&nic_data->rxc_lock);
465 return EOK;
466}
467
468/**
469 * Default implementation of blocked_sources_set method.
470 *
471 * @param fun
472 * @param[in] addr_list List of blocked addresses
473 * @param[in] addr_count Number of addresses in the list
474 *
475 * @return EOK
476 * @return EINVAL
477 * @return ENOTSUP
478 * @return ENOMEM
479 */
480int nic_blocked_sources_set_impl(ddf_fun_t *fun,
481 const nic_address_t *addr_list, size_t addr_count)
482{
483 nic_t *nic_data = (nic_t *) fun->driver_data;
484 fibril_rwlock_write_lock(&nic_data->rxc_lock);
485 if (nic_data->on_blocked_sources_change) {
486 nic_data->on_blocked_sources_change(nic_data, addr_list, addr_count);
487 }
488 int rc = nic_rxc_blocked_sources_set(&nic_data->rx_control,
489 addr_list, addr_count);
490 fibril_rwlock_write_unlock(&nic_data->rxc_lock);
491 return rc;
492}
493
494/**
495 * Default implementation of vlan_get_mask method.
496 *
497 * @param fun
498 * @param[out] mask Current VLAN mask
499 *
500 * @return EOK
501 * @return ENOENT If the mask is not set
502 */
503int nic_vlan_get_mask_impl(ddf_fun_t *fun, nic_vlan_mask_t *mask)
504{
505 nic_t *nic_data = (nic_t *) fun->driver_data;
506 fibril_rwlock_read_lock(&nic_data->rxc_lock);
507 int rc = nic_rxc_vlan_get_mask(&nic_data->rx_control, mask);
508 fibril_rwlock_read_unlock(&nic_data->rxc_lock);
509 return rc;
510}
511
512/**
513 * Default implementation of vlan_set_mask method.
514 *
515 * @param fun
516 * @param[in] mask The new VLAN mask
517 *
518 * @return EOK
519 * @return ENOMEM
520 */
521int nic_vlan_set_mask_impl(ddf_fun_t *fun, const nic_vlan_mask_t *mask)
522{
523 nic_t *nic_data = (nic_t *) fun->driver_data;
524 fibril_rwlock_write_lock(&nic_data->rxc_lock);
525 int rc = nic_rxc_vlan_set_mask(&nic_data->rx_control, mask);
526 if (rc == EOK && nic_data->on_vlan_mask_change) {
527 nic_data->on_vlan_mask_change(nic_data, mask);
528 }
529 fibril_rwlock_write_unlock(&nic_data->rxc_lock);
530 return rc;
531}
532
533/**
534 * Default implementation of the wol_virtue_add method.
535 * Create a new WOL virtue.
536 *
537 * @param[in] fun
538 * @param[in] type Type of the virtue
539 * @param[in] data Data required for this virtue (depends on type)
540 * @param[in] length Length of the data
541 * @param[out] filter Identifier of the new virtue
542 *
543 * @return EOK If the operation was successfully completed
544 * @return EINVAL If virtue type is not supported or the data are invalid
545 * @return ELIMIT If the driver does not allow to create more virtues
546 * @return ENOMEM If there was not enough memory to complete the operation
547 */
548int nic_wol_virtue_add_impl(ddf_fun_t *fun, nic_wv_type_t type,
549 const void *data, size_t length, nic_wv_id_t *new_id)
550{
551 nic_t *nic_data = (nic_t *) fun->driver_data;
552 if (nic_data->on_wol_virtue_add == NULL
553 || nic_data->on_wol_virtue_remove == NULL) {
554 return ENOTSUP;
555 }
556 if (type == NIC_WV_NONE || type >= NIC_WV_MAX) {
557 return EINVAL;
558 }
559 if (nic_wol_virtues_verify(type, data, length) != EOK) {
560 return EINVAL;
561 }
562 nic_wol_virtue_t *virtue = malloc(sizeof (nic_wol_virtue_t));
563 if (virtue == NULL) {
564 return ENOMEM;
565 }
566 bzero(virtue, sizeof (nic_wol_virtue_t));
567 if (length != 0) {
568 virtue->data = malloc(length);
569 if (virtue->data == NULL) {
570 free(virtue);
571 return ENOMEM;
572 }
573 memcpy((void *) virtue->data, data, length);
574 }
575 virtue->type = type;
576 virtue->length = length;
577
578 fibril_rwlock_write_lock(&nic_data->wv_lock);
579 /* Check if we haven't reached the maximum */
580 if (nic_data->wol_virtues.caps_max[type] < 0) {
581 fibril_rwlock_write_unlock(&nic_data->wv_lock);
582 return EINVAL;
583 }
584 if ((int) nic_data->wol_virtues.lists_sizes[type] >=
585 nic_data->wol_virtues.caps_max[type]) {
586 fibril_rwlock_write_unlock(&nic_data->wv_lock);
587 return ELIMIT;
588 }
589 /* Call the user-defined add callback */
590 int rc = nic_data->on_wol_virtue_add(nic_data, virtue);
591 if (rc != EOK) {
592 free(virtue->data);
593 free(virtue);
594 fibril_rwlock_write_unlock(&nic_data->wv_lock);
595 return rc;
596 }
597 rc = nic_wol_virtues_add(&nic_data->wol_virtues, virtue);
598 if (rc != EOK) {
599 /* If the adding fails, call user-defined remove callback */
600 nic_data->on_wol_virtue_remove(nic_data, virtue);
601 fibril_rwlock_write_unlock(&nic_data->wv_lock);
602 free(virtue->data);
603 free(virtue);
604 return rc;
605 } else {
606 *new_id = virtue->id;
607 fibril_rwlock_write_unlock(&nic_data->wv_lock);
608 }
609 return EOK;
610}
611
612/**
613 * Default implementation of the wol_virtue_remove method.
614 * Destroys the WOL virtue.
615 *
616 * @param[in] fun
617 * @param[in] id WOL virtue identification
618 *
619 * @return EOK If the operation was successfully completed
620 * @return ENOTSUP If the function is not supported by the driver or device
621 * @return ENOENT If the virtue identifier is not valid.
622 */
623int nic_wol_virtue_remove_impl(ddf_fun_t *fun, nic_wv_id_t id)
624{
625 nic_t *nic_data = (nic_t *) fun->driver_data;
626 if (nic_data->on_wol_virtue_add == NULL
627 || nic_data->on_wol_virtue_remove == NULL) {
628 return ENOTSUP;
629 }
630 fibril_rwlock_write_lock(&nic_data->wv_lock);
631 nic_wol_virtue_t *virtue =
632 nic_wol_virtues_remove(&nic_data->wol_virtues, id);
633 if (virtue == NULL) {
634 fibril_rwlock_write_unlock(&nic_data->wv_lock);
635 return ENOENT;
636 }
637 /* The event handler is called after the filter was removed */
638 nic_data->on_wol_virtue_remove(nic_data, virtue);
639 fibril_rwlock_write_unlock(&nic_data->wv_lock);
640 free(virtue->data);
641 free(virtue);
642 return EOK;
643}
644
645/**
646 * Default implementation of the wol_virtue_probe method.
647 * Queries the type and data of the virtue.
648 *
649 * @param[in] fun
650 * @param[in] id Virtue identifier
651 * @param[out] type Type of the virtue. Can be NULL.
652 * @param[out] data Data used when the virtue was created. Can be NULL.
653 * @param[out] length Length of the data. Can be NULL.
654 *
655 * @return EOK If the operation was successfully completed
656 * @return ENOENT If the virtue identifier is not valid.
657 * @return ENOMEM If there was not enough memory to complete the operation
658 */
659int nic_wol_virtue_probe_impl(ddf_fun_t *fun, nic_wv_id_t id,
660 nic_wv_type_t *type, size_t max_length, void *data, size_t *length)
661{
662 nic_t *nic_data = (nic_t *) fun->driver_data;
663 fibril_rwlock_read_lock(&nic_data->wv_lock);
664 const nic_wol_virtue_t *virtue =
665 nic_wol_virtues_find(&nic_data->wol_virtues, id);
666 if (virtue == NULL) {
667 *type = NIC_WV_NONE;
668 *length = 0;
669 fibril_rwlock_read_unlock(&nic_data->wv_lock);
670 return ENOENT;
671 } else {
672 *type = virtue->type;
673 if (max_length > virtue->length) {
674 max_length = virtue->length;
675 }
676 memcpy(data, virtue->data, max_length);
677 *length = virtue->length;
678 fibril_rwlock_read_unlock(&nic_data->wv_lock);
679 return EOK;
680 }
681}
682
683/**
684 * Default implementation of the wol_virtue_list method.
685 * List filters of the specified type. If NIC_WV_NONE is the type, it lists all
686 * filters.
687 *
688 * @param[in] fun
689 * @param[in] type Type of the virtues
690 * @param[out] virtues Vector of virtue ID's.
691 * @param[out] count Length of the data. Can be NULL.
692 *
693 * @return EOK If the operation was successfully completed
694 * @return ENOENT If the filter identification is not valid.
695 * @return ENOMEM If there was not enough memory to complete the operation
696 */
697int nic_wol_virtue_list_impl(ddf_fun_t *fun, nic_wv_type_t type,
698 size_t max_count, nic_wv_id_t *id_list, size_t *id_count)
699{
700 nic_t *nic_data = (nic_t *) fun->driver_data;
701 fibril_rwlock_read_lock(&nic_data->wv_lock);
702 int rc = nic_wol_virtues_list(&nic_data->wol_virtues, type,
703 max_count, id_list, id_count);
704 fibril_rwlock_read_unlock(&nic_data->wv_lock);
705 return rc;
706}
707
708/**
709 * Default implementation of the wol_virtue_get_caps method.
710 * Queries for the current capabilities for some type of filter.
711 *
712 * @param[in] fun
713 * @param[in] type Type of the virtues
714 * @param[out] count Number of virtues of this type that can be currently set
715 *
716 * @return EOK If the operation was successfully completed
717 */
718int nic_wol_virtue_get_caps_impl(ddf_fun_t *fun, nic_wv_type_t type, int *count)
719{
720 nic_t *nic_data = (nic_t *) fun->driver_data;
721 fibril_rwlock_read_lock(&nic_data->wv_lock);
722 *count = nic_data->wol_virtues.caps_max[type]
723 - (int) nic_data->wol_virtues.lists_sizes[type];
724 fibril_rwlock_read_unlock(&nic_data->wv_lock);
725 return EOK;
726}
727
728/**
729 * Default implementation of the poll_get_mode method.
730 * Queries the current interrupt/poll mode of the NIC
731 *
732 * @param[in] fun
733 * @param[out] mode Current poll mode
734 * @param[out] period Period used in periodic polling. Can be NULL.
735 *
736 * @return EOK If the operation was successfully completed
737 * @return ENOTSUP This function is not supported.
738 * @return EPARTY Error in communication protocol
739 */
740int nic_poll_get_mode_impl(ddf_fun_t *fun,
741 nic_poll_mode_t *mode, struct timeval *period)
742{
743 nic_t *nic_data = (nic_t *) fun->driver_data;
744 fibril_rwlock_read_lock(&nic_data->main_lock);
745 *mode = nic_data->poll_mode;
746 memcpy(period, &nic_data->poll_period, sizeof (struct timeval));
747 fibril_rwlock_read_unlock(&nic_data->main_lock);
748 return EOK;
749}
750
751/**
752 * Default implementation of the poll_set_mode_impl method.
753 * Sets the interrupt/poll mode of the NIC.
754 *
755 * @param[in] fun
756 * @param[in] mode The new poll mode
757 * @param[in] period Period used in periodic polling. Can be NULL.
758 *
759 * @return EOK If the operation was successfully completed
760 * @return ENOTSUP This operation is not supported.
761 * @return EPARTY Error in communication protocol
762 */
763int nic_poll_set_mode_impl(ddf_fun_t *fun,
764 nic_poll_mode_t mode, const struct timeval *period)
765{
766 nic_t *nic_data = (nic_t *) fun->driver_data;
767 /* If the driver does not implement the poll mode change handler it cannot
768 * switch off interrupts and this is not supported. */
769 if (nic_data->on_poll_mode_change == NULL)
770 return ENOTSUP;
771
772 if ((mode == NIC_POLL_ON_DEMAND) && nic_data->on_poll_request == NULL)
773 return ENOTSUP;
774
775 if (mode == NIC_POLL_PERIODIC || mode == NIC_POLL_SOFTWARE_PERIODIC) {
776 if (period == NULL)
777 return EINVAL;
778 if (period->tv_sec == 0 && period->tv_usec == 0)
779 return EINVAL;
780 if (period->tv_sec < 0 || period->tv_usec < 0)
781 return EINVAL;
782 }
783 fibril_rwlock_write_lock(&nic_data->main_lock);
784 int rc = nic_data->on_poll_mode_change(nic_data, mode, period);
785 assert(rc == EOK || rc == ENOTSUP || rc == EINVAL);
786 if (rc == ENOTSUP && (nic_data->on_poll_request != NULL) &&
787 (mode == NIC_POLL_PERIODIC || mode == NIC_POLL_SOFTWARE_PERIODIC) ) {
788
789 rc = nic_data->on_poll_mode_change(nic_data, NIC_POLL_ON_DEMAND, NULL);
790 assert(rc == EOK || rc == ENOTSUP);
791 if (rc == EOK)
792 nic_sw_period_start(nic_data);
793 }
794 if (rc == EOK) {
795 nic_data->poll_mode = mode;
796 if (period)
797 nic_data->poll_period = *period;
798 }
799 fibril_rwlock_write_unlock(&nic_data->main_lock);
800 return rc;
801}
802
803/**
804 * Default implementation of the poll_now method.
805 * Wrapper for the actual poll implementation.
806 *
807 * @param[in] fun
808 *
809 * @return EOK If the NIC was polled
810 * @return ENOTSUP If the function is not supported
811 * @return EINVAL If the NIC is not in state where it allows on demand polling
812 */
813int nic_poll_now_impl(ddf_fun_t *fun) {
814 nic_t *nic_data = (nic_t *) fun->driver_data;
815 fibril_rwlock_read_lock(&nic_data->main_lock);
816 if (nic_data->poll_mode != NIC_POLL_ON_DEMAND) {
817 fibril_rwlock_read_unlock(&nic_data->main_lock);
818 return EINVAL;
819 }
820 if (nic_data->on_poll_request != NULL) {
821 nic_data->on_poll_request(nic_data);
822 fibril_rwlock_read_unlock(&nic_data->main_lock);
823 return EOK;
824 } else {
825 fibril_rwlock_read_unlock(&nic_data->main_lock);
826 return ENOTSUP;
827 }
828}
829
830/** Default implementation of the device_added method
831 *
832 * Just calls nic_ready.
833 *
834 * @param dev
835 *
836 */
837void nic_device_added_impl(ddf_dev_t *dev)
838{
839 nic_ready((nic_t *) dev->driver_data);
840}
841
842/**
843 * Default handler for unknown methods (outside of the NIC interface).
844 * Logs a warning message and returns ENOTSUP to the caller.
845 *
846 * @param fun The DDF function where the method should be called.
847 * @param callid IPC call identifier
848 * @param call IPC call data
849 */
850void nic_default_handler_impl(ddf_fun_t *fun, ipc_callid_t callid,
851 ipc_call_t *call)
852{
853 async_answer_0(callid, ENOTSUP);
854}
855
856/**
857 * Default (empty) OPEN function implementation.
858 *
859 * @param fun The DDF function
860 *
861 * @return EOK always.
862 */
863int nic_open_impl(ddf_fun_t *fun)
864{
865 return EOK;
866}
867
868/**
869 * Default (empty) OPEN function implementation.
870 *
871 * @param fun The DDF function
872 */
873void nic_close_impl(ddf_fun_t *fun)
874{
875}
876
877/** @}
878 */
Note: See TracBrowser for help on using the repository browser.