2 * Copyright (c) 2012 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
41 #include <fibril_synch.h>
42 #include <inet/iplink_srv.h>
45 #include <device/nic.h>
49 #include "ethip_nic.h"
52 static int ethip_nic_open(service_id_t sid
);
53 static void ethip_nic_cb_conn(ipc_callid_t iid
, ipc_call_t
*icall
, void *arg
);
55 static LIST_INITIALIZE(ethip_nic_list
);
56 static FIBRIL_MUTEX_INITIALIZE(ethip_discovery_lock
);
58 static int ethip_nic_check_new(void)
61 category_id_t iplink_cat
;
66 fibril_mutex_lock(ðip_discovery_lock
);
68 rc
= loc_category_get_id("nic", &iplink_cat
, IPC_FLAG_BLOCKING
);
70 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed resolving category 'nic'.");
71 fibril_mutex_unlock(ðip_discovery_lock
);
75 rc
= loc_category_get_svcs(iplink_cat
, &svcs
, &count
);
77 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed getting list of IP links.");
78 fibril_mutex_unlock(ðip_discovery_lock
);
82 for (i
= 0; i
< count
; i
++) {
83 already_known
= false;
85 list_foreach(ethip_nic_list
, nic_link
) {
86 ethip_nic_t
*nic
= list_get_instance(nic_link
,
87 ethip_nic_t
, nic_list
);
88 if (nic
->svc_id
== svcs
[i
]) {
95 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "Found NIC '%lu'",
96 (unsigned long) svcs
[i
]);
97 rc
= ethip_nic_open(svcs
[i
]);
99 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Could not open NIC.");
103 fibril_mutex_unlock(ðip_discovery_lock
);
107 static ethip_nic_t
*ethip_nic_new(void)
109 ethip_nic_t
*nic
= calloc(1, sizeof(ethip_nic_t
));
112 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed allocating NIC structure. "
117 link_initialize(&nic
->nic_list
);
118 list_initialize(&nic
->addr_list
);
123 static ethip_link_addr_t
*ethip_nic_addr_new(uint32_t addr
)
125 ethip_link_addr_t
*laddr
= calloc(1, sizeof(ethip_link_addr_t
));
127 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed allocating NIC address structure. "
132 link_initialize(&laddr
->addr_list
);
138 static void ethip_nic_delete(ethip_nic_t
*nic
)
140 if (nic
->svc_name
!= NULL
)
145 static void ethip_link_addr_delete(ethip_link_addr_t
*laddr
)
150 static int ethip_nic_open(service_id_t sid
)
152 bool in_list
= false;
153 nic_address_t nic_address
;
155 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethip_nic_open()");
156 ethip_nic_t
*nic
= ethip_nic_new();
160 int rc
= loc_service_get_name(sid
, &nic
->svc_name
);
162 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed getting service name.");
166 nic
->sess
= loc_service_connect(EXCHANGE_SERIALIZE
, sid
, 0);
167 if (nic
->sess
== NULL
) {
168 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed connecting '%s'", nic
->svc_name
);
174 rc
= nic_callback_create(nic
->sess
, ethip_nic_cb_conn
, nic
);
176 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed creating callback connection "
177 "from '%s'", nic
->svc_name
);
181 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "Opened NIC '%s'", nic
->svc_name
);
182 list_append(&nic
->nic_list
, ðip_nic_list
);
185 rc
= ethip_iplink_init(nic
);
189 rc
= nic_get_address(nic
->sess
, &nic_address
);
191 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Error getting MAC address of NIC '%s'.",
196 mac48_decode(nic_address
.address
, &nic
->mac_addr
);
198 rc
= nic_set_state(nic
->sess
, NIC_STATE_ACTIVE
);
200 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Error activating NIC '%s'.",
205 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "Initialized IP link service, MAC = 0x%" PRIx64
,
212 list_remove(&nic
->nic_list
);
213 if (nic
->sess
!= NULL
)
214 async_hangup(nic
->sess
);
215 ethip_nic_delete(nic
);
219 static void ethip_nic_cat_change_cb(void)
221 (void) ethip_nic_check_new();
224 static void ethip_nic_addr_changed(ethip_nic_t
*nic
, ipc_callid_t callid
,
227 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethip_nic_addr_changed()");
228 async_answer_0(callid
, ENOTSUP
);
231 static void ethip_nic_received(ethip_nic_t
*nic
, ipc_callid_t callid
,
238 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethip_nic_received() nic=%p", nic
);
240 rc
= async_data_write_accept(&data
, false, 0, 0, 0, &size
);
242 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "data_write_accept() failed");
246 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "Ethernet PDU contents (%zu bytes)",
249 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "call ethip_received");
250 rc
= ethip_received(&nic
->iplink
, data
, size
);
251 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "free data");
254 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethip_nic_received() done, rc=%d", rc
);
255 async_answer_0(callid
, rc
);
258 static void ethip_nic_device_state(ethip_nic_t
*nic
, ipc_callid_t callid
,
261 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethip_nic_device_state()");
262 async_answer_0(callid
, ENOTSUP
);
265 static void ethip_nic_cb_conn(ipc_callid_t iid
, ipc_call_t
*icall
, void *arg
)
267 ethip_nic_t
*nic
= (ethip_nic_t
*)arg
;
269 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethnip_nic_cb_conn()");
273 ipc_callid_t callid
= async_get_call(&call
);
275 if (!IPC_GET_IMETHOD(call
)) {
276 /* TODO: Handle hangup */
280 switch (IPC_GET_IMETHOD(call
)) {
281 case NIC_EV_ADDR_CHANGED
:
282 ethip_nic_addr_changed(nic
, callid
, &call
);
284 case NIC_EV_RECEIVED
:
285 ethip_nic_received(nic
, callid
, &call
);
287 case NIC_EV_DEVICE_STATE
:
288 ethip_nic_device_state(nic
, callid
, &call
);
291 async_answer_0(callid
, ENOTSUP
);
296 int ethip_nic_discovery_start(void)
298 int rc
= loc_register_cat_change_cb(ethip_nic_cat_change_cb
);
300 log_msg(LOG_DEFAULT
, LVL_ERROR
, "Failed registering callback for NIC "
301 "discovery (%d).", rc
);
305 return ethip_nic_check_new();
308 ethip_nic_t
*ethip_nic_find_by_iplink_sid(service_id_t iplink_sid
)
310 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethip_nic_find_by_iplink_sid(%u)",
311 (unsigned) iplink_sid
);
313 list_foreach(ethip_nic_list
, link
) {
314 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethip_nic_find_by_iplink_sid - element");
315 ethip_nic_t
*nic
= list_get_instance(link
, ethip_nic_t
,
318 if (nic
->iplink_sid
== iplink_sid
) {
319 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethip_nic_find_by_iplink_sid - found %p", nic
);
324 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethip_nic_find_by_iplink_sid - not found");
328 int ethip_nic_send(ethip_nic_t
*nic
, void *data
, size_t size
)
331 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethip_nic_send(size=%zu)", size
);
332 rc
= nic_send_frame(nic
->sess
, data
, size
);
333 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "nic_send_frame -> %d", rc
);
337 int ethip_nic_addr_add(ethip_nic_t
*nic
, uint32_t addr
)
339 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethip_nic_addr_add()");
341 ethip_link_addr_t
*laddr
= ethip_nic_addr_new(addr
);
345 list_append(&laddr
->addr_list
, &nic
->addr_list
);
349 int ethip_nic_addr_remove(ethip_nic_t
*nic
, uint32_t addr
)
351 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethip_nic_addr_remove()");
353 ethip_link_addr_t
*laddr
= ethip_nic_addr_find(nic
, addr
);
357 list_remove(&laddr
->addr_list
);
358 ethip_link_addr_delete(laddr
);
362 ethip_link_addr_t
*ethip_nic_addr_find(ethip_nic_t
*nic
,
365 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "ethip_nic_addr_find()");
367 list_foreach(nic
->addr_list
, link
) {
368 ethip_link_addr_t
*laddr
= list_get_instance(link
,
369 ethip_link_addr_t
, addr_list
);
371 if (addr
== laddr
->addr
)