headers_check fix: linux/virtio_net.h
[linux-2.6/mini2440.git] / drivers / uwb / rsv.c
blobec6eecb32f30de583a7920450075004861665158
1 /*
2 * UWB reservation management.
4 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/kernel.h>
19 #include <linux/uwb.h>
20 #include <linux/random.h>
22 #include "uwb-internal.h"
24 static void uwb_rsv_timer(unsigned long arg);
26 static const char *rsv_states[] = {
27 [UWB_RSV_STATE_NONE] = "none ",
28 [UWB_RSV_STATE_O_INITIATED] = "o initiated ",
29 [UWB_RSV_STATE_O_PENDING] = "o pending ",
30 [UWB_RSV_STATE_O_MODIFIED] = "o modified ",
31 [UWB_RSV_STATE_O_ESTABLISHED] = "o established ",
32 [UWB_RSV_STATE_O_TO_BE_MOVED] = "o to be moved ",
33 [UWB_RSV_STATE_O_MOVE_EXPANDING] = "o move expanding",
34 [UWB_RSV_STATE_O_MOVE_COMBINING] = "o move combining",
35 [UWB_RSV_STATE_O_MOVE_REDUCING] = "o move reducing ",
36 [UWB_RSV_STATE_T_ACCEPTED] = "t accepted ",
37 [UWB_RSV_STATE_T_CONFLICT] = "t conflict ",
38 [UWB_RSV_STATE_T_PENDING] = "t pending ",
39 [UWB_RSV_STATE_T_DENIED] = "t denied ",
40 [UWB_RSV_STATE_T_RESIZED] = "t resized ",
41 [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = "t expanding acc ",
42 [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = "t expanding conf",
43 [UWB_RSV_STATE_T_EXPANDING_PENDING] = "t expanding pend",
44 [UWB_RSV_STATE_T_EXPANDING_DENIED] = "t expanding den ",
47 static const char *rsv_types[] = {
48 [UWB_DRP_TYPE_ALIEN_BP] = "alien-bp",
49 [UWB_DRP_TYPE_HARD] = "hard",
50 [UWB_DRP_TYPE_SOFT] = "soft",
51 [UWB_DRP_TYPE_PRIVATE] = "private",
52 [UWB_DRP_TYPE_PCA] = "pca",
55 bool uwb_rsv_has_two_drp_ies(struct uwb_rsv *rsv)
57 static const bool has_two_drp_ies[] = {
58 [UWB_RSV_STATE_O_INITIATED] = false,
59 [UWB_RSV_STATE_O_PENDING] = false,
60 [UWB_RSV_STATE_O_MODIFIED] = false,
61 [UWB_RSV_STATE_O_ESTABLISHED] = false,
62 [UWB_RSV_STATE_O_TO_BE_MOVED] = false,
63 [UWB_RSV_STATE_O_MOVE_COMBINING] = false,
64 [UWB_RSV_STATE_O_MOVE_REDUCING] = false,
65 [UWB_RSV_STATE_O_MOVE_EXPANDING] = true,
66 [UWB_RSV_STATE_T_ACCEPTED] = false,
67 [UWB_RSV_STATE_T_CONFLICT] = false,
68 [UWB_RSV_STATE_T_PENDING] = false,
69 [UWB_RSV_STATE_T_DENIED] = false,
70 [UWB_RSV_STATE_T_RESIZED] = false,
71 [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = true,
72 [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = true,
73 [UWB_RSV_STATE_T_EXPANDING_PENDING] = true,
74 [UWB_RSV_STATE_T_EXPANDING_DENIED] = true,
77 return has_two_drp_ies[rsv->state];
80 /**
81 * uwb_rsv_state_str - return a string for a reservation state
82 * @state: the reservation state.
84 const char *uwb_rsv_state_str(enum uwb_rsv_state state)
86 if (state < UWB_RSV_STATE_NONE || state >= UWB_RSV_STATE_LAST)
87 return "unknown";
88 return rsv_states[state];
90 EXPORT_SYMBOL_GPL(uwb_rsv_state_str);
92 /**
93 * uwb_rsv_type_str - return a string for a reservation type
94 * @type: the reservation type
96 const char *uwb_rsv_type_str(enum uwb_drp_type type)
98 if (type < UWB_DRP_TYPE_ALIEN_BP || type > UWB_DRP_TYPE_PCA)
99 return "invalid";
100 return rsv_types[type];
102 EXPORT_SYMBOL_GPL(uwb_rsv_type_str);
104 void uwb_rsv_dump(char *text, struct uwb_rsv *rsv)
106 struct device *dev = &rsv->rc->uwb_dev.dev;
107 struct uwb_dev_addr devaddr;
108 char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE];
110 uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr);
111 if (rsv->target.type == UWB_RSV_TARGET_DEV)
112 devaddr = rsv->target.dev->dev_addr;
113 else
114 devaddr = rsv->target.devaddr;
115 uwb_dev_addr_print(target, sizeof(target), &devaddr);
117 dev_dbg(dev, "rsv %s -> %s: %s\n", owner, target, uwb_rsv_state_str(rsv->state));
120 static void uwb_rsv_release(struct kref *kref)
122 struct uwb_rsv *rsv = container_of(kref, struct uwb_rsv, kref);
124 kfree(rsv);
127 void uwb_rsv_get(struct uwb_rsv *rsv)
129 kref_get(&rsv->kref);
132 void uwb_rsv_put(struct uwb_rsv *rsv)
134 kref_put(&rsv->kref, uwb_rsv_release);
138 * Get a free stream index for a reservation.
140 * If the target is a DevAddr (e.g., a WUSB cluster reservation) then
141 * the stream is allocated from a pool of per-RC stream indexes,
142 * otherwise a unique stream index for the target is selected.
144 static int uwb_rsv_get_stream(struct uwb_rsv *rsv)
146 struct uwb_rc *rc = rsv->rc;
147 struct device *dev = &rc->uwb_dev.dev;
148 unsigned long *streams_bm;
149 int stream;
151 switch (rsv->target.type) {
152 case UWB_RSV_TARGET_DEV:
153 streams_bm = rsv->target.dev->streams;
154 break;
155 case UWB_RSV_TARGET_DEVADDR:
156 streams_bm = rc->uwb_dev.streams;
157 break;
158 default:
159 return -EINVAL;
162 stream = find_first_zero_bit(streams_bm, UWB_NUM_STREAMS);
163 if (stream >= UWB_NUM_STREAMS)
164 return -EBUSY;
166 rsv->stream = stream;
167 set_bit(stream, streams_bm);
169 dev_dbg(dev, "get stream %d\n", rsv->stream);
171 return 0;
174 static void uwb_rsv_put_stream(struct uwb_rsv *rsv)
176 struct uwb_rc *rc = rsv->rc;
177 struct device *dev = &rc->uwb_dev.dev;
178 unsigned long *streams_bm;
180 switch (rsv->target.type) {
181 case UWB_RSV_TARGET_DEV:
182 streams_bm = rsv->target.dev->streams;
183 break;
184 case UWB_RSV_TARGET_DEVADDR:
185 streams_bm = rc->uwb_dev.streams;
186 break;
187 default:
188 return;
191 clear_bit(rsv->stream, streams_bm);
193 dev_dbg(dev, "put stream %d\n", rsv->stream);
196 void uwb_rsv_backoff_win_timer(unsigned long arg)
198 struct uwb_drp_backoff_win *bow = (struct uwb_drp_backoff_win *)arg;
199 struct uwb_rc *rc = container_of(bow, struct uwb_rc, bow);
200 struct device *dev = &rc->uwb_dev.dev;
202 bow->can_reserve_extra_mases = true;
203 if (bow->total_expired <= 4) {
204 bow->total_expired++;
205 } else {
206 /* after 4 backoff window has expired we can exit from
207 * the backoff procedure */
208 bow->total_expired = 0;
209 bow->window = UWB_DRP_BACKOFF_WIN_MIN >> 1;
211 dev_dbg(dev, "backoff_win_timer total_expired=%d, n=%d\n: ", bow->total_expired, bow->n);
213 /* try to relocate all the "to be moved" relocations */
214 uwb_rsv_handle_drp_avail_change(rc);
217 void uwb_rsv_backoff_win_increment(struct uwb_rc *rc)
219 struct uwb_drp_backoff_win *bow = &rc->bow;
220 struct device *dev = &rc->uwb_dev.dev;
221 unsigned timeout_us;
223 dev_dbg(dev, "backoff_win_increment: window=%d\n", bow->window);
225 bow->can_reserve_extra_mases = false;
227 if((bow->window << 1) == UWB_DRP_BACKOFF_WIN_MAX)
228 return;
230 bow->window <<= 1;
231 bow->n = random32() & (bow->window - 1);
232 dev_dbg(dev, "new_window=%d, n=%d\n: ", bow->window, bow->n);
234 /* reset the timer associated variables */
235 timeout_us = bow->n * UWB_SUPERFRAME_LENGTH_US;
236 bow->total_expired = 0;
237 mod_timer(&bow->timer, jiffies + usecs_to_jiffies(timeout_us));
240 static void uwb_rsv_stroke_timer(struct uwb_rsv *rsv)
242 int sframes = UWB_MAX_LOST_BEACONS;
245 * Multicast reservations can become established within 1
246 * super frame and should not be terminated if no response is
247 * received.
249 if (rsv->is_multicast) {
250 if (rsv->state == UWB_RSV_STATE_O_INITIATED
251 || rsv->state == UWB_RSV_STATE_O_MOVE_EXPANDING
252 || rsv->state == UWB_RSV_STATE_O_MOVE_COMBINING
253 || rsv->state == UWB_RSV_STATE_O_MOVE_REDUCING)
254 sframes = 1;
255 if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED)
256 sframes = 0;
260 if (sframes > 0) {
262 * Add an additional 2 superframes to account for the
263 * time to send the SET DRP IE command.
265 unsigned timeout_us = (sframes + 2) * UWB_SUPERFRAME_LENGTH_US;
266 mod_timer(&rsv->timer, jiffies + usecs_to_jiffies(timeout_us));
267 } else
268 del_timer(&rsv->timer);
272 * Update a reservations state, and schedule an update of the
273 * transmitted DRP IEs.
275 static void uwb_rsv_state_update(struct uwb_rsv *rsv,
276 enum uwb_rsv_state new_state)
278 rsv->state = new_state;
279 rsv->ie_valid = false;
281 uwb_rsv_dump("SU", rsv);
283 uwb_rsv_stroke_timer(rsv);
284 uwb_rsv_sched_update(rsv->rc);
287 static void uwb_rsv_callback(struct uwb_rsv *rsv)
289 if (rsv->callback)
290 rsv->callback(rsv);
293 void uwb_rsv_set_state(struct uwb_rsv *rsv, enum uwb_rsv_state new_state)
295 struct uwb_rsv_move *mv = &rsv->mv;
297 if (rsv->state == new_state) {
298 switch (rsv->state) {
299 case UWB_RSV_STATE_O_ESTABLISHED:
300 case UWB_RSV_STATE_O_MOVE_EXPANDING:
301 case UWB_RSV_STATE_O_MOVE_COMBINING:
302 case UWB_RSV_STATE_O_MOVE_REDUCING:
303 case UWB_RSV_STATE_T_ACCEPTED:
304 case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
305 case UWB_RSV_STATE_T_RESIZED:
306 case UWB_RSV_STATE_NONE:
307 uwb_rsv_stroke_timer(rsv);
308 break;
309 default:
310 /* Expecting a state transition so leave timer
311 as-is. */
312 break;
314 return;
317 uwb_rsv_dump("SC", rsv);
319 switch (new_state) {
320 case UWB_RSV_STATE_NONE:
321 uwb_rsv_state_update(rsv, UWB_RSV_STATE_NONE);
322 uwb_rsv_callback(rsv);
323 break;
324 case UWB_RSV_STATE_O_INITIATED:
325 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_INITIATED);
326 break;
327 case UWB_RSV_STATE_O_PENDING:
328 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_PENDING);
329 break;
330 case UWB_RSV_STATE_O_MODIFIED:
331 /* in the companion there are the MASes to drop */
332 bitmap_andnot(rsv->mas.bm, rsv->mas.bm, mv->companion_mas.bm, UWB_NUM_MAS);
333 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MODIFIED);
334 break;
335 case UWB_RSV_STATE_O_ESTABLISHED:
336 if (rsv->state == UWB_RSV_STATE_O_MODIFIED
337 || rsv->state == UWB_RSV_STATE_O_MOVE_REDUCING) {
338 uwb_drp_avail_release(rsv->rc, &mv->companion_mas);
339 rsv->needs_release_companion_mas = false;
341 uwb_drp_avail_reserve(rsv->rc, &rsv->mas);
342 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_ESTABLISHED);
343 uwb_rsv_callback(rsv);
344 break;
345 case UWB_RSV_STATE_O_MOVE_EXPANDING:
346 rsv->needs_release_companion_mas = true;
347 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_EXPANDING);
348 break;
349 case UWB_RSV_STATE_O_MOVE_COMBINING:
350 rsv->needs_release_companion_mas = false;
351 uwb_drp_avail_reserve(rsv->rc, &mv->companion_mas);
352 bitmap_or(rsv->mas.bm, rsv->mas.bm, mv->companion_mas.bm, UWB_NUM_MAS);
353 rsv->mas.safe += mv->companion_mas.safe;
354 rsv->mas.unsafe += mv->companion_mas.unsafe;
355 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_COMBINING);
356 break;
357 case UWB_RSV_STATE_O_MOVE_REDUCING:
358 bitmap_andnot(mv->companion_mas.bm, rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS);
359 rsv->needs_release_companion_mas = true;
360 rsv->mas.safe = mv->final_mas.safe;
361 rsv->mas.unsafe = mv->final_mas.unsafe;
362 bitmap_copy(rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS);
363 bitmap_copy(rsv->mas.unsafe_bm, mv->final_mas.unsafe_bm, UWB_NUM_MAS);
364 uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_REDUCING);
365 break;
366 case UWB_RSV_STATE_T_ACCEPTED:
367 case UWB_RSV_STATE_T_RESIZED:
368 rsv->needs_release_companion_mas = false;
369 uwb_drp_avail_reserve(rsv->rc, &rsv->mas);
370 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_ACCEPTED);
371 uwb_rsv_callback(rsv);
372 break;
373 case UWB_RSV_STATE_T_DENIED:
374 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_DENIED);
375 break;
376 case UWB_RSV_STATE_T_CONFLICT:
377 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_CONFLICT);
378 break;
379 case UWB_RSV_STATE_T_PENDING:
380 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_PENDING);
381 break;
382 case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
383 rsv->needs_release_companion_mas = true;
384 uwb_drp_avail_reserve(rsv->rc, &mv->companion_mas);
385 uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_EXPANDING_ACCEPTED);
386 break;
387 default:
388 dev_err(&rsv->rc->uwb_dev.dev, "unhandled state: %s (%d)\n",
389 uwb_rsv_state_str(new_state), new_state);
393 static void uwb_rsv_handle_timeout_work(struct work_struct *work)
395 struct uwb_rsv *rsv = container_of(work, struct uwb_rsv,
396 handle_timeout_work);
397 struct uwb_rc *rc = rsv->rc;
399 mutex_lock(&rc->rsvs_mutex);
401 uwb_rsv_dump("TO", rsv);
403 switch (rsv->state) {
404 case UWB_RSV_STATE_O_INITIATED:
405 if (rsv->is_multicast) {
406 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
407 goto unlock;
409 break;
410 case UWB_RSV_STATE_O_MOVE_EXPANDING:
411 if (rsv->is_multicast) {
412 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_COMBINING);
413 goto unlock;
415 break;
416 case UWB_RSV_STATE_O_MOVE_COMBINING:
417 if (rsv->is_multicast) {
418 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_REDUCING);
419 goto unlock;
421 break;
422 case UWB_RSV_STATE_O_MOVE_REDUCING:
423 if (rsv->is_multicast) {
424 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
425 goto unlock;
427 break;
428 case UWB_RSV_STATE_O_ESTABLISHED:
429 if (rsv->is_multicast)
430 goto unlock;
431 break;
432 case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
434 * The time out could be for the main or of the
435 * companion DRP, assume it's for the companion and
436 * drop that first. A further time out is required to
437 * drop the main.
439 uwb_rsv_set_state(rsv, UWB_RSV_STATE_T_ACCEPTED);
440 uwb_drp_avail_release(rsv->rc, &rsv->mv.companion_mas);
441 goto unlock;
442 default:
443 break;
446 uwb_rsv_remove(rsv);
448 unlock:
449 mutex_unlock(&rc->rsvs_mutex);
452 static struct uwb_rsv *uwb_rsv_alloc(struct uwb_rc *rc)
454 struct uwb_rsv *rsv;
456 rsv = kzalloc(sizeof(struct uwb_rsv), GFP_KERNEL);
457 if (!rsv)
458 return NULL;
460 INIT_LIST_HEAD(&rsv->rc_node);
461 INIT_LIST_HEAD(&rsv->pal_node);
462 kref_init(&rsv->kref);
463 init_timer(&rsv->timer);
464 rsv->timer.function = uwb_rsv_timer;
465 rsv->timer.data = (unsigned long)rsv;
467 rsv->rc = rc;
468 INIT_WORK(&rsv->handle_timeout_work, uwb_rsv_handle_timeout_work);
470 return rsv;
474 * uwb_rsv_create - allocate and initialize a UWB reservation structure
475 * @rc: the radio controller
476 * @cb: callback to use when the reservation completes or terminates
477 * @pal_priv: data private to the PAL to be passed in the callback
479 * The callback is called when the state of the reservation changes from:
481 * - pending to accepted
482 * - pending to denined
483 * - accepted to terminated
484 * - pending to terminated
486 struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb, void *pal_priv)
488 struct uwb_rsv *rsv;
490 rsv = uwb_rsv_alloc(rc);
491 if (!rsv)
492 return NULL;
494 rsv->callback = cb;
495 rsv->pal_priv = pal_priv;
497 return rsv;
499 EXPORT_SYMBOL_GPL(uwb_rsv_create);
501 void uwb_rsv_remove(struct uwb_rsv *rsv)
503 uwb_rsv_dump("RM", rsv);
505 if (rsv->state != UWB_RSV_STATE_NONE)
506 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
508 if (rsv->needs_release_companion_mas)
509 uwb_drp_avail_release(rsv->rc, &rsv->mv.companion_mas);
510 uwb_drp_avail_release(rsv->rc, &rsv->mas);
512 if (uwb_rsv_is_owner(rsv))
513 uwb_rsv_put_stream(rsv);
515 del_timer_sync(&rsv->timer);
516 uwb_dev_put(rsv->owner);
517 if (rsv->target.type == UWB_RSV_TARGET_DEV)
518 uwb_dev_put(rsv->target.dev);
520 list_del_init(&rsv->rc_node);
521 uwb_rsv_put(rsv);
525 * uwb_rsv_destroy - free a UWB reservation structure
526 * @rsv: the reservation to free
528 * The reservation must already be terminated.
530 void uwb_rsv_destroy(struct uwb_rsv *rsv)
532 uwb_rsv_put(rsv);
534 EXPORT_SYMBOL_GPL(uwb_rsv_destroy);
537 * usb_rsv_establish - start a reservation establishment
538 * @rsv: the reservation
540 * The PAL should fill in @rsv's owner, target, type, max_mas,
541 * min_mas, max_interval and is_multicast fields. If the target is a
542 * uwb_dev it must be referenced.
544 * The reservation's callback will be called when the reservation is
545 * accepted, denied or times out.
547 int uwb_rsv_establish(struct uwb_rsv *rsv)
549 struct uwb_rc *rc = rsv->rc;
550 struct uwb_mas_bm available;
551 int ret;
553 mutex_lock(&rc->rsvs_mutex);
554 ret = uwb_rsv_get_stream(rsv);
555 if (ret)
556 goto out;
558 rsv->tiebreaker = random32() & 1;
559 /* get available mas bitmap */
560 uwb_drp_available(rc, &available);
562 ret = uwb_rsv_find_best_allocation(rsv, &available, &rsv->mas);
563 if (ret == UWB_RSV_ALLOC_NOT_FOUND) {
564 ret = -EBUSY;
565 uwb_rsv_put_stream(rsv);
566 goto out;
569 ret = uwb_drp_avail_reserve_pending(rc, &rsv->mas);
570 if (ret != 0) {
571 uwb_rsv_put_stream(rsv);
572 goto out;
575 uwb_rsv_get(rsv);
576 list_add_tail(&rsv->rc_node, &rc->reservations);
577 rsv->owner = &rc->uwb_dev;
578 uwb_dev_get(rsv->owner);
579 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_INITIATED);
580 out:
581 mutex_unlock(&rc->rsvs_mutex);
582 return ret;
584 EXPORT_SYMBOL_GPL(uwb_rsv_establish);
587 * uwb_rsv_modify - modify an already established reservation
588 * @rsv: the reservation to modify
589 * @max_mas: new maximum MAS to reserve
590 * @min_mas: new minimum MAS to reserve
591 * @max_interval: new max_interval to use
593 * FIXME: implement this once there are PALs that use it.
595 int uwb_rsv_modify(struct uwb_rsv *rsv, int max_mas, int min_mas, int max_interval)
597 return -ENOSYS;
599 EXPORT_SYMBOL_GPL(uwb_rsv_modify);
602 * move an already established reservation (rc->rsvs_mutex must to be
603 * taken when tis function is called)
605 int uwb_rsv_try_move(struct uwb_rsv *rsv, struct uwb_mas_bm *available)
607 struct uwb_rc *rc = rsv->rc;
608 struct uwb_drp_backoff_win *bow = &rc->bow;
609 struct device *dev = &rc->uwb_dev.dev;
610 struct uwb_rsv_move *mv;
611 int ret = 0;
613 if (bow->can_reserve_extra_mases == false)
614 return -EBUSY;
616 mv = &rsv->mv;
618 if (uwb_rsv_find_best_allocation(rsv, available, &mv->final_mas) == UWB_RSV_ALLOC_FOUND) {
620 if (!bitmap_equal(rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS)) {
621 /* We want to move the reservation */
622 bitmap_andnot(mv->companion_mas.bm, mv->final_mas.bm, rsv->mas.bm, UWB_NUM_MAS);
623 uwb_drp_avail_reserve_pending(rc, &mv->companion_mas);
624 uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_EXPANDING);
626 } else {
627 dev_dbg(dev, "new allocation not found\n");
630 return ret;
633 /* It will try to move every reservation in state O_ESTABLISHED giving
634 * to the MAS allocator algorithm an availability that is the real one
635 * plus the allocation already established from the reservation. */
636 void uwb_rsv_handle_drp_avail_change(struct uwb_rc *rc)
638 struct uwb_drp_backoff_win *bow = &rc->bow;
639 struct uwb_rsv *rsv;
640 struct uwb_mas_bm mas;
642 if (bow->can_reserve_extra_mases == false)
643 return;
645 list_for_each_entry(rsv, &rc->reservations, rc_node) {
646 if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED ||
647 rsv->state == UWB_RSV_STATE_O_TO_BE_MOVED) {
648 uwb_drp_available(rc, &mas);
649 bitmap_or(mas.bm, mas.bm, rsv->mas.bm, UWB_NUM_MAS);
650 uwb_rsv_try_move(rsv, &mas);
657 * uwb_rsv_terminate - terminate an established reservation
658 * @rsv: the reservation to terminate
660 * A reservation is terminated by removing the DRP IE from the beacon,
661 * the other end will consider the reservation to be terminated when
662 * it does not see the DRP IE for at least mMaxLostBeacons.
664 * If applicable, the reference to the target uwb_dev will be released.
666 void uwb_rsv_terminate(struct uwb_rsv *rsv)
668 struct uwb_rc *rc = rsv->rc;
670 mutex_lock(&rc->rsvs_mutex);
672 if (rsv->state != UWB_RSV_STATE_NONE)
673 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
675 mutex_unlock(&rc->rsvs_mutex);
677 EXPORT_SYMBOL_GPL(uwb_rsv_terminate);
680 * uwb_rsv_accept - accept a new reservation from a peer
681 * @rsv: the reservation
682 * @cb: call back for reservation changes
683 * @pal_priv: data to be passed in the above call back
685 * Reservation requests from peers are denied unless a PAL accepts it
686 * by calling this function.
688 * The PAL call uwb_rsv_destroy() for all accepted reservations before
689 * calling uwb_pal_unregister().
691 void uwb_rsv_accept(struct uwb_rsv *rsv, uwb_rsv_cb_f cb, void *pal_priv)
693 uwb_rsv_get(rsv);
695 rsv->callback = cb;
696 rsv->pal_priv = pal_priv;
697 rsv->state = UWB_RSV_STATE_T_ACCEPTED;
699 EXPORT_SYMBOL_GPL(uwb_rsv_accept);
702 * Is a received DRP IE for this reservation?
704 static bool uwb_rsv_match(struct uwb_rsv *rsv, struct uwb_dev *src,
705 struct uwb_ie_drp *drp_ie)
707 struct uwb_dev_addr *rsv_src;
708 int stream;
710 stream = uwb_ie_drp_stream_index(drp_ie);
712 if (rsv->stream != stream)
713 return false;
715 switch (rsv->target.type) {
716 case UWB_RSV_TARGET_DEVADDR:
717 return rsv->stream == stream;
718 case UWB_RSV_TARGET_DEV:
719 if (uwb_ie_drp_owner(drp_ie))
720 rsv_src = &rsv->owner->dev_addr;
721 else
722 rsv_src = &rsv->target.dev->dev_addr;
723 return uwb_dev_addr_cmp(&src->dev_addr, rsv_src) == 0;
725 return false;
728 static struct uwb_rsv *uwb_rsv_new_target(struct uwb_rc *rc,
729 struct uwb_dev *src,
730 struct uwb_ie_drp *drp_ie)
732 struct uwb_rsv *rsv;
733 struct uwb_pal *pal;
734 enum uwb_rsv_state state;
736 rsv = uwb_rsv_alloc(rc);
737 if (!rsv)
738 return NULL;
740 rsv->rc = rc;
741 rsv->owner = src;
742 uwb_dev_get(rsv->owner);
743 rsv->target.type = UWB_RSV_TARGET_DEV;
744 rsv->target.dev = &rc->uwb_dev;
745 uwb_dev_get(&rc->uwb_dev);
746 rsv->type = uwb_ie_drp_type(drp_ie);
747 rsv->stream = uwb_ie_drp_stream_index(drp_ie);
748 uwb_drp_ie_to_bm(&rsv->mas, drp_ie);
751 * See if any PALs are interested in this reservation. If not,
752 * deny the request.
754 rsv->state = UWB_RSV_STATE_T_DENIED;
755 mutex_lock(&rc->uwb_dev.mutex);
756 list_for_each_entry(pal, &rc->pals, node) {
757 if (pal->new_rsv)
758 pal->new_rsv(pal, rsv);
759 if (rsv->state == UWB_RSV_STATE_T_ACCEPTED)
760 break;
762 mutex_unlock(&rc->uwb_dev.mutex);
764 list_add_tail(&rsv->rc_node, &rc->reservations);
765 state = rsv->state;
766 rsv->state = UWB_RSV_STATE_NONE;
768 /* FIXME: do something sensible here */
769 if (state == UWB_RSV_STATE_T_ACCEPTED
770 && uwb_drp_avail_reserve_pending(rc, &rsv->mas) == -EBUSY) {
771 /* FIXME: do something sensible here */
772 } else {
773 uwb_rsv_set_state(rsv, state);
776 return rsv;
780 * uwb_rsv_get_usable_mas - get the bitmap of the usable MAS of a reservations
781 * @rsv: the reservation.
782 * @mas: returns the available MAS.
784 * The usable MAS of a reservation may be less than the negotiated MAS
785 * if alien BPs are present.
787 void uwb_rsv_get_usable_mas(struct uwb_rsv *rsv, struct uwb_mas_bm *mas)
789 bitmap_zero(mas->bm, UWB_NUM_MAS);
790 bitmap_andnot(mas->bm, rsv->mas.bm, rsv->rc->cnflt_alien_bitmap.bm, UWB_NUM_MAS);
792 EXPORT_SYMBOL_GPL(uwb_rsv_get_usable_mas);
795 * uwb_rsv_find - find a reservation for a received DRP IE.
796 * @rc: the radio controller
797 * @src: source of the DRP IE
798 * @drp_ie: the DRP IE
800 * If the reservation cannot be found and the DRP IE is from a peer
801 * attempting to establish a new reservation, create a new reservation
802 * and add it to the list.
804 struct uwb_rsv *uwb_rsv_find(struct uwb_rc *rc, struct uwb_dev *src,
805 struct uwb_ie_drp *drp_ie)
807 struct uwb_rsv *rsv;
809 list_for_each_entry(rsv, &rc->reservations, rc_node) {
810 if (uwb_rsv_match(rsv, src, drp_ie))
811 return rsv;
814 if (uwb_ie_drp_owner(drp_ie))
815 return uwb_rsv_new_target(rc, src, drp_ie);
817 return NULL;
821 * Go through all the reservations and check for timeouts and (if
822 * necessary) update their DRP IEs.
824 * FIXME: look at building the SET_DRP_IE command here rather than
825 * having to rescan the list in uwb_rc_send_all_drp_ie().
827 static bool uwb_rsv_update_all(struct uwb_rc *rc)
829 struct uwb_rsv *rsv, *t;
830 bool ie_updated = false;
832 list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
833 if (!rsv->ie_valid) {
834 uwb_drp_ie_update(rsv);
835 ie_updated = true;
839 return ie_updated;
842 void uwb_rsv_queue_update(struct uwb_rc *rc)
844 unsigned long delay_us = UWB_MAS_LENGTH_US * UWB_MAS_PER_ZONE;
846 queue_delayed_work(rc->rsv_workq, &rc->rsv_update_work, usecs_to_jiffies(delay_us));
850 * uwb_rsv_sched_update - schedule an update of the DRP IEs
851 * @rc: the radio controller.
853 * To improve performance and ensure correctness with [ECMA-368] the
854 * number of SET-DRP-IE commands that are done are limited.
856 * DRP IEs update come from two sources: DRP events from the hardware
857 * which all occur at the beginning of the superframe ('syncronous'
858 * events) and reservation establishment/termination requests from
859 * PALs or timers ('asynchronous' events).
861 * A delayed work ensures that all the synchronous events result in
862 * one SET-DRP-IE command.
864 * Additional logic (the set_drp_ie_pending and rsv_updated_postponed
865 * flags) will prevent an asynchrous event starting a SET-DRP-IE
866 * command if one is currently awaiting a response.
868 * FIXME: this does leave a window where an asynchrous event can delay
869 * the SET-DRP-IE for a synchronous event by one superframe.
871 void uwb_rsv_sched_update(struct uwb_rc *rc)
873 spin_lock(&rc->rsvs_lock);
874 if (!delayed_work_pending(&rc->rsv_update_work)) {
875 if (rc->set_drp_ie_pending > 0) {
876 rc->set_drp_ie_pending++;
877 goto unlock;
879 uwb_rsv_queue_update(rc);
881 unlock:
882 spin_unlock(&rc->rsvs_lock);
886 * Update DRP IEs and, if necessary, the DRP Availability IE and send
887 * the updated IEs to the radio controller.
889 static void uwb_rsv_update_work(struct work_struct *work)
891 struct uwb_rc *rc = container_of(work, struct uwb_rc,
892 rsv_update_work.work);
893 bool ie_updated;
895 mutex_lock(&rc->rsvs_mutex);
897 ie_updated = uwb_rsv_update_all(rc);
899 if (!rc->drp_avail.ie_valid) {
900 uwb_drp_avail_ie_update(rc);
901 ie_updated = true;
904 if (ie_updated && (rc->set_drp_ie_pending == 0))
905 uwb_rc_send_all_drp_ie(rc);
907 mutex_unlock(&rc->rsvs_mutex);
910 static void uwb_rsv_alien_bp_work(struct work_struct *work)
912 struct uwb_rc *rc = container_of(work, struct uwb_rc,
913 rsv_alien_bp_work.work);
914 struct uwb_rsv *rsv;
916 mutex_lock(&rc->rsvs_mutex);
918 list_for_each_entry(rsv, &rc->reservations, rc_node) {
919 if (rsv->type != UWB_DRP_TYPE_ALIEN_BP) {
920 rsv->callback(rsv);
924 mutex_unlock(&rc->rsvs_mutex);
927 static void uwb_rsv_timer(unsigned long arg)
929 struct uwb_rsv *rsv = (struct uwb_rsv *)arg;
931 queue_work(rsv->rc->rsv_workq, &rsv->handle_timeout_work);
935 * uwb_rsv_remove_all - remove all reservations
936 * @rc: the radio controller
938 * A DRP IE update is not done.
940 void uwb_rsv_remove_all(struct uwb_rc *rc)
942 struct uwb_rsv *rsv, *t;
944 mutex_lock(&rc->rsvs_mutex);
945 list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
946 uwb_rsv_remove(rsv);
948 /* Cancel any postponed update. */
949 rc->set_drp_ie_pending = 0;
950 mutex_unlock(&rc->rsvs_mutex);
952 cancel_delayed_work_sync(&rc->rsv_update_work);
955 void uwb_rsv_init(struct uwb_rc *rc)
957 INIT_LIST_HEAD(&rc->reservations);
958 INIT_LIST_HEAD(&rc->cnflt_alien_list);
959 mutex_init(&rc->rsvs_mutex);
960 spin_lock_init(&rc->rsvs_lock);
961 INIT_DELAYED_WORK(&rc->rsv_update_work, uwb_rsv_update_work);
962 INIT_DELAYED_WORK(&rc->rsv_alien_bp_work, uwb_rsv_alien_bp_work);
963 rc->bow.can_reserve_extra_mases = true;
964 rc->bow.total_expired = 0;
965 rc->bow.window = UWB_DRP_BACKOFF_WIN_MIN >> 1;
966 init_timer(&rc->bow.timer);
967 rc->bow.timer.function = uwb_rsv_backoff_win_timer;
968 rc->bow.timer.data = (unsigned long)&rc->bow;
970 bitmap_complement(rc->uwb_dev.streams, rc->uwb_dev.streams, UWB_NUM_STREAMS);
973 int uwb_rsv_setup(struct uwb_rc *rc)
975 char name[16];
977 snprintf(name, sizeof(name), "%s_rsvd", dev_name(&rc->uwb_dev.dev));
978 rc->rsv_workq = create_singlethread_workqueue(name);
979 if (rc->rsv_workq == NULL)
980 return -ENOMEM;
982 return 0;
985 void uwb_rsv_cleanup(struct uwb_rc *rc)
987 uwb_rsv_remove_all(rc);
988 destroy_workqueue(rc->rsv_workq);