2 * WiMedia Logical Link Control Protocol (WLP)
4 * Copyright (C) 2005-2006 Intel Corporation
5 * Reinette Chatre <reinette.chatre@intel.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include <linux/wlp.h>
25 #include <linux/slab.h>
27 #include "wlp-internal.h"
30 void wlp_neighbor_init(struct wlp_neighbor_e
*neighbor
)
32 INIT_LIST_HEAD(&neighbor
->wssid
);
36 * Create area for device information storage
38 * wlp->mutex must be held
40 int __wlp_alloc_device_info(struct wlp
*wlp
)
42 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
43 BUG_ON(wlp
->dev_info
!= NULL
);
44 wlp
->dev_info
= kzalloc(sizeof(struct wlp_device_info
), GFP_KERNEL
);
45 if (wlp
->dev_info
== NULL
) {
46 dev_err(dev
, "WLP: Unable to allocate memory for "
47 "device information.\n");
55 * Fill in device information using function provided by driver
57 * wlp->mutex must be held
60 void __wlp_fill_device_info(struct wlp
*wlp
)
62 wlp
->fill_device_info(wlp
, wlp
->dev_info
);
66 * Setup device information
68 * Allocate area for device information and populate it.
70 * wlp->mutex must be held
72 int __wlp_setup_device_info(struct wlp
*wlp
)
75 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
77 result
= __wlp_alloc_device_info(wlp
);
79 dev_err(dev
, "WLP: Unable to allocate area for "
80 "device information.\n");
83 __wlp_fill_device_info(wlp
);
88 * Remove information about neighbor stored temporarily
90 * Information learned during discovey should only be stored when the
91 * device enrolls in the neighbor's WSS. We do need to store this
92 * information temporarily in order to present it to the user.
94 * We are only interested in keeping neighbor WSS information if that
95 * neighbor is accepting enrollment.
97 * should be called with wlp->nbmutex held
99 void wlp_remove_neighbor_tmp_info(struct wlp_neighbor_e
*neighbor
)
101 struct wlp_wssid_e
*wssid_e
, *next
;
103 if (!list_empty(&neighbor
->wssid
)) {
104 list_for_each_entry_safe(wssid_e
, next
, &neighbor
->wssid
,
106 if (wssid_e
->info
!= NULL
) {
107 keep
= wssid_e
->info
->accept_enroll
;
108 kfree(wssid_e
->info
);
109 wssid_e
->info
= NULL
;
111 list_del(&wssid_e
->node
);
117 if (neighbor
->info
!= NULL
) {
118 kfree(neighbor
->info
);
119 neighbor
->info
= NULL
;
124 * Populate WLP neighborhood cache with neighbor information
126 * A new neighbor is found. If it is discoverable then we add it to the
127 * neighborhood cache.
131 int wlp_add_neighbor(struct wlp
*wlp
, struct uwb_dev
*dev
)
135 struct wlp_neighbor_e
*neighbor
;
139 * Use contents of WLP IE found in beacon cache to determine if
140 * neighbor is discoverable.
141 * The device does not support WLP IE yet so this still needs to be
142 * done. Until then we assume all devices are discoverable.
144 discoverable
= 1; /* will be changed when FIXME disappears */
146 /* Add neighbor to cache for discovery */
147 neighbor
= kzalloc(sizeof(*neighbor
), GFP_KERNEL
);
148 if (neighbor
== NULL
) {
149 dev_err(&dev
->dev
, "Unable to create memory for "
154 wlp_neighbor_init(neighbor
);
156 neighbor
->uwb_dev
= dev
;
157 list_add(&neighbor
->node
, &wlp
->neighbors
);
164 * Remove one neighbor from cache
167 void __wlp_neighbor_release(struct wlp_neighbor_e
*neighbor
)
169 struct wlp_wssid_e
*wssid_e
, *next_wssid_e
;
171 list_for_each_entry_safe(wssid_e
, next_wssid_e
,
172 &neighbor
->wssid
, node
) {
173 list_del(&wssid_e
->node
);
176 uwb_dev_put(neighbor
->uwb_dev
);
177 list_del(&neighbor
->node
);
182 * Clear entire neighborhood cache.
185 void __wlp_neighbors_release(struct wlp
*wlp
)
187 struct wlp_neighbor_e
*neighbor
, *next
;
188 if (list_empty(&wlp
->neighbors
))
190 list_for_each_entry_safe(neighbor
, next
, &wlp
->neighbors
, node
) {
191 __wlp_neighbor_release(neighbor
);
196 void wlp_neighbors_release(struct wlp
*wlp
)
198 mutex_lock(&wlp
->nbmutex
);
199 __wlp_neighbors_release(wlp
);
200 mutex_unlock(&wlp
->nbmutex
);
206 * Send D1 message to neighbor, receive D2 message
208 * @neighbor: neighbor to which D1 message will be sent
209 * @wss: if not NULL, it is an enrollment request for this WSS
210 * @wssid: if wss not NULL, this is the wssid of the WSS in which we
213 * A D1/D2 exchange is done for one of two reasons: discovery or
214 * enrollment. If done for discovery the D1 message is sent to the neighbor
215 * and the contents of the D2 response is stored in a temporary cache.
216 * If done for enrollment the @wss and @wssid are provided also. In this
217 * case the D1 message is sent to the neighbor, the D2 response is parsed
218 * for enrollment of the WSS with wssid.
220 * &wss->mutex is held
223 int wlp_d1d2_exchange(struct wlp
*wlp
, struct wlp_neighbor_e
*neighbor
,
224 struct wlp_wss
*wss
, struct wlp_uuid
*wssid
)
227 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
228 DECLARE_COMPLETION_ONSTACK(completion
);
229 struct wlp_session session
;
231 struct wlp_frame_assoc
*resp
;
232 struct uwb_dev_addr
*dev_addr
= &neighbor
->uwb_dev
->dev_addr
;
234 mutex_lock(&wlp
->mutex
);
235 if (!wlp_uuid_is_set(&wlp
->uuid
)) {
236 dev_err(dev
, "WLP: UUID is not set. Set via sysfs to "
241 /* Send D1 association frame */
242 result
= wlp_send_assoc_frame(wlp
, wss
, dev_addr
, WLP_ASSOC_D1
);
244 dev_err(dev
, "Unable to send D1 frame to neighbor "
245 "%02x:%02x (%d)\n", dev_addr
->data
[1],
246 dev_addr
->data
[0], result
);
249 /* Create session, wait for response */
250 session
.exp_message
= WLP_ASSOC_D2
;
251 session
.cb
= wlp_session_cb
;
252 session
.cb_priv
= &completion
;
253 session
.neighbor_addr
= *dev_addr
;
254 BUG_ON(wlp
->session
!= NULL
);
255 wlp
->session
= &session
;
256 /* Wait for D2/F0 frame */
257 result
= wait_for_completion_interruptible_timeout(&completion
,
258 WLP_PER_MSG_TIMEOUT
* HZ
);
261 dev_err(dev
, "Timeout while sending D1 to neighbor "
262 "%02x:%02x.\n", dev_addr
->data
[1],
267 dev_err(dev
, "Unable to discover/enroll neighbor %02x:%02x.\n",
268 dev_addr
->data
[1], dev_addr
->data
[0]);
271 /* Parse message in session->data: it will be either D2 or F0 */
273 resp
= (void *) skb
->data
;
275 if (resp
->type
== WLP_ASSOC_F0
) {
276 result
= wlp_parse_f0(wlp
, skb
);
278 dev_err(dev
, "WLP: Unable to parse F0 from neighbor "
279 "%02x:%02x.\n", dev_addr
->data
[1],
282 goto error_resp_parse
;
286 result
= wlp_parse_d2_frame_to_cache(wlp
, skb
, neighbor
);
288 dev_err(dev
, "WLP: Unable to parse D2 message from "
289 "neighbor %02x:%02x for discovery.\n",
290 dev_addr
->data
[1], dev_addr
->data
[0]);
291 goto error_resp_parse
;
295 result
= wlp_parse_d2_frame_to_enroll(wss
, skb
, neighbor
,
298 dev_err(dev
, "WLP: Unable to parse D2 message from "
299 "neighbor %02x:%02x for enrollment.\n",
300 dev_addr
->data
[1], dev_addr
->data
[0]);
301 goto error_resp_parse
;
309 mutex_unlock(&wlp
->mutex
);
314 * Enroll into WSS of provided WSSID by using neighbor as registrar
316 * &wss->mutex is held
318 int wlp_enroll_neighbor(struct wlp
*wlp
, struct wlp_neighbor_e
*neighbor
,
319 struct wlp_wss
*wss
, struct wlp_uuid
*wssid
)
322 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
323 char buf
[WLP_WSS_UUID_STRSIZE
];
324 struct uwb_dev_addr
*dev_addr
= &neighbor
->uwb_dev
->dev_addr
;
326 wlp_wss_uuid_print(buf
, sizeof(buf
), wssid
);
328 result
= wlp_d1d2_exchange(wlp
, neighbor
, wss
, wssid
);
330 dev_err(dev
, "WLP: D1/D2 message exchange for enrollment "
331 "failed. result = %d \n", result
);
334 if (wss
->state
!= WLP_WSS_STATE_PART_ENROLLED
) {
335 dev_err(dev
, "WLP: Unable to enroll into WSS %s using "
336 "neighbor %02x:%02x. \n", buf
,
337 dev_addr
->data
[1], dev_addr
->data
[0]);
341 if (wss
->secure_status
== WLP_WSS_SECURE
) {
342 dev_err(dev
, "FIXME: need to complete secure enrollment.\n");
346 wss
->state
= WLP_WSS_STATE_ENROLLED
;
347 dev_dbg(dev
, "WLP: Success Enrollment into unsecure WSS "
348 "%s using neighbor %02x:%02x. \n",
349 buf
, dev_addr
->data
[1], dev_addr
->data
[0]);
359 * Discover WSS information of neighbor's active WSS
362 int wlp_discover_neighbor(struct wlp
*wlp
,
363 struct wlp_neighbor_e
*neighbor
)
365 return wlp_d1d2_exchange(wlp
, neighbor
, NULL
, NULL
);
370 * Each neighbor in the neighborhood cache is discoverable. Discover it.
372 * Discovery is done through sending of D1 association frame and parsing
373 * the D2 association frame response. Only wssid from D2 will be included
374 * in neighbor cache, rest is just displayed to user and forgotten.
376 * The discovery is not done in parallel. This is simple and enables us to
377 * maintain only one association context.
379 * The discovery of one neighbor does not affect the other, but if the
380 * discovery of a neighbor fails it is removed from the neighborhood cache.
383 int wlp_discover_all_neighbors(struct wlp
*wlp
)
386 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
387 struct wlp_neighbor_e
*neighbor
, *next
;
389 list_for_each_entry_safe(neighbor
, next
, &wlp
->neighbors
, node
) {
390 result
= wlp_discover_neighbor(wlp
, neighbor
);
392 dev_err(dev
, "WLP: Unable to discover neighbor "
393 "%02x:%02x, removing from neighborhood. \n",
394 neighbor
->uwb_dev
->dev_addr
.data
[1],
395 neighbor
->uwb_dev
->dev_addr
.data
[0]);
396 __wlp_neighbor_release(neighbor
);
402 static int wlp_add_neighbor_helper(struct device
*dev
, void *priv
)
404 struct wlp
*wlp
= priv
;
405 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
407 return wlp_add_neighbor(wlp
, uwb_dev
);
411 * Discover WLP neighborhood
413 * Will send D1 association frame to all devices in beacon group that have
414 * discoverable bit set in WLP IE. D2 frames will be received, information
415 * displayed to user in @buf. Partial information (from D2 association
416 * frame) will be cached to assist with future association
419 * The discovery of the WLP neighborhood is triggered by the user. This
420 * should occur infrequently and we thus free current cache and re-allocate
423 * If one neighbor fails during initial discovery (determining if it is a
424 * neighbor or not), we fail all - note that interaction with neighbor has
425 * not occured at this point so if a failure occurs we know something went wrong
426 * locally. We thus undo everything.
428 ssize_t
wlp_discover(struct wlp
*wlp
)
431 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
433 mutex_lock(&wlp
->nbmutex
);
434 /* Clear current neighborhood cache. */
435 __wlp_neighbors_release(wlp
);
436 /* Determine which devices in neighborhood. Repopulate cache. */
437 result
= uwb_dev_for_each(wlp
->rc
, wlp_add_neighbor_helper
, wlp
);
439 /* May have partial neighbor information, release all. */
440 __wlp_neighbors_release(wlp
);
441 goto error_dev_for_each
;
443 /* Discover the properties of devices in neighborhood. */
444 result
= wlp_discover_all_neighbors(wlp
);
445 /* In case of failure we still print our partial results. */
447 dev_err(dev
, "Unable to fully discover neighborhood. \n");
451 mutex_unlock(&wlp
->nbmutex
);
456 * Handle events from UWB stack
458 * We handle events conservatively. If a neighbor goes off the air we
459 * remove it from the neighborhood. If an association process is in
460 * progress this function will block waiting for the nbmutex to become
461 * free. The association process will thus be allowed to complete before it
465 void wlp_uwb_notifs_cb(void *_wlp
, struct uwb_dev
*uwb_dev
,
466 enum uwb_notifs event
)
468 struct wlp
*wlp
= _wlp
;
469 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
470 struct wlp_neighbor_e
*neighbor
, *next
;
473 case UWB_NOTIF_ONAIR
:
474 result
= wlp_eda_create_node(&wlp
->eda
,
475 uwb_dev
->mac_addr
.data
,
478 dev_err(dev
, "WLP: Unable to add new neighbor "
479 "%02x:%02x to EDA cache.\n",
480 uwb_dev
->dev_addr
.data
[1],
481 uwb_dev
->dev_addr
.data
[0]);
483 case UWB_NOTIF_OFFAIR
:
484 wlp_eda_rm_node(&wlp
->eda
, &uwb_dev
->dev_addr
);
485 mutex_lock(&wlp
->nbmutex
);
486 list_for_each_entry_safe(neighbor
, next
, &wlp
->neighbors
, node
) {
487 if (neighbor
->uwb_dev
== uwb_dev
)
488 __wlp_neighbor_release(neighbor
);
490 mutex_unlock(&wlp
->nbmutex
);
493 dev_err(dev
, "don't know how to handle event %d from uwb\n",
498 static void wlp_channel_changed(struct uwb_pal
*pal
, int channel
)
500 struct wlp
*wlp
= container_of(pal
, struct wlp
, pal
);
503 netif_carrier_off(wlp
->ndev
);
505 netif_carrier_on(wlp
->ndev
);
508 int wlp_setup(struct wlp
*wlp
, struct uwb_rc
*rc
, struct net_device
*ndev
)
512 BUG_ON(wlp
->fill_device_info
== NULL
);
513 BUG_ON(wlp
->xmit_frame
== NULL
);
514 BUG_ON(wlp
->stop_queue
== NULL
);
515 BUG_ON(wlp
->start_queue
== NULL
);
519 wlp_eda_init(&wlp
->eda
);/* Set up address cache */
520 wlp
->uwb_notifs_handler
.cb
= wlp_uwb_notifs_cb
;
521 wlp
->uwb_notifs_handler
.data
= wlp
;
522 uwb_notifs_register(rc
, &wlp
->uwb_notifs_handler
);
524 uwb_pal_init(&wlp
->pal
);
526 wlp
->pal
.channel_changed
= wlp_channel_changed
;
527 result
= uwb_pal_register(&wlp
->pal
);
529 uwb_notifs_deregister(wlp
->rc
, &wlp
->uwb_notifs_handler
);
533 EXPORT_SYMBOL_GPL(wlp_setup
);
535 void wlp_remove(struct wlp
*wlp
)
537 wlp_neighbors_release(wlp
);
538 uwb_pal_unregister(&wlp
->pal
);
539 uwb_notifs_deregister(wlp
->rc
, &wlp
->uwb_notifs_handler
);
540 wlp_eda_release(&wlp
->eda
);
541 mutex_lock(&wlp
->mutex
);
542 if (wlp
->dev_info
!= NULL
)
543 kfree(wlp
->dev_info
);
544 mutex_unlock(&wlp
->mutex
);
547 EXPORT_SYMBOL_GPL(wlp_remove
);
550 * wlp_reset_all - reset the WLP hardware
551 * @wlp: the WLP device to reset.
553 * This schedules a full hardware reset of the WLP device. The radio
554 * controller and any other PALs will also be reset.
556 void wlp_reset_all(struct wlp
*wlp
)
558 uwb_rc_reset_all(wlp
->rc
);
560 EXPORT_SYMBOL_GPL(wlp_reset_all
);