uwb: remove beacon cache entry after calling uwb_notify()
[safe/jmp/linux-2.6] / drivers / uwb / rsv.c
1 /*
2  * UWB reservation management.
3  *
4  * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
5  *
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.
9  *
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.
14  *
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/>.
17  */
18 #include <linux/kernel.h>
19 #include <linux/uwb.h>
20 #include <linux/random.h>
21
22 #include "uwb-internal.h"
23
24 static void uwb_rsv_timer(unsigned long arg);
25
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 ",
45 };
46
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",
53 };
54
55 bool uwb_rsv_has_two_drp_ies(struct uwb_rsv *rsv)
56 {
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,
75         };
76
77         return has_two_drp_ies[rsv->state];
78 }
79
80 /**
81  * uwb_rsv_state_str - return a string for a reservation state
82  * @state: the reservation state.
83  */
84 const char *uwb_rsv_state_str(enum uwb_rsv_state state)
85 {
86         if (state < UWB_RSV_STATE_NONE || state >= UWB_RSV_STATE_LAST)
87                 return "unknown";
88         return rsv_states[state];
89 }
90 EXPORT_SYMBOL_GPL(uwb_rsv_state_str);
91
92 /**
93  * uwb_rsv_type_str - return a string for a reservation type
94  * @type: the reservation type
95  */
96 const char *uwb_rsv_type_str(enum uwb_drp_type type)
97 {
98         if (type < UWB_DRP_TYPE_ALIEN_BP || type > UWB_DRP_TYPE_PCA)
99                 return "invalid";
100         return rsv_types[type];
101 }
102 EXPORT_SYMBOL_GPL(uwb_rsv_type_str);
103
104 void uwb_rsv_dump(char *text, struct uwb_rsv *rsv)
105 {
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];
109
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);
116
117         dev_dbg(dev, "rsv %s -> %s: %s\n", owner, target, uwb_rsv_state_str(rsv->state));
118 }
119
120 static void uwb_rsv_release(struct kref *kref)
121 {
122         struct uwb_rsv *rsv = container_of(kref, struct uwb_rsv, kref);
123
124         kfree(rsv);
125 }
126
127 void uwb_rsv_get(struct uwb_rsv *rsv)
128 {
129         kref_get(&rsv->kref);
130 }
131
132 void uwb_rsv_put(struct uwb_rsv *rsv)
133 {
134         kref_put(&rsv->kref, uwb_rsv_release);
135 }
136
137 /*
138  * Get a free stream index for a reservation.
139  *
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.
143  */
144 static int uwb_rsv_get_stream(struct uwb_rsv *rsv)
145 {
146         struct uwb_rc *rc = rsv->rc;
147         struct device *dev = &rc->uwb_dev.dev;
148         unsigned long *streams_bm;
149         int stream;
150
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;
160         }
161
162         stream = find_first_zero_bit(streams_bm, UWB_NUM_STREAMS);
163         if (stream >= UWB_NUM_STREAMS)
164                 return -EBUSY;
165
166         rsv->stream = stream;
167         set_bit(stream, streams_bm);
168
169         dev_dbg(dev, "get stream %d\n", rsv->stream);
170
171         return 0;
172 }
173
174 static void uwb_rsv_put_stream(struct uwb_rsv *rsv)
175 {
176         struct uwb_rc *rc = rsv->rc;
177         struct device *dev = &rc->uwb_dev.dev;
178         unsigned long *streams_bm;
179
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;
189         }
190
191         clear_bit(rsv->stream, streams_bm);
192
193         dev_dbg(dev, "put stream %d\n", rsv->stream);
194 }
195
196 void uwb_rsv_backoff_win_timer(unsigned long arg)
197 {
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;
201
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;
210         }
211         dev_dbg(dev, "backoff_win_timer total_expired=%d, n=%d\n: ", bow->total_expired, bow->n);
212
213         /* try to relocate all the "to be moved" relocations */
214         uwb_rsv_handle_drp_avail_change(rc);
215 }
216
217 void uwb_rsv_backoff_win_increment(struct uwb_rc *rc)
218 {
219         struct uwb_drp_backoff_win *bow = &rc->bow;
220         struct device *dev = &rc->uwb_dev.dev;
221         unsigned timeout_us;
222
223         dev_dbg(dev, "backoff_win_increment: window=%d\n", bow->window);
224
225         bow->can_reserve_extra_mases = false;
226
227         if((bow->window << 1) == UWB_DRP_BACKOFF_WIN_MAX)
228                 return;
229
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);
233
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));         
238 }
239
240 static void uwb_rsv_stroke_timer(struct uwb_rsv *rsv)
241 {
242         int sframes = UWB_MAX_LOST_BEACONS;
243
244         /*
245          * Multicast reservations can become established within 1
246          * super frame and should not be terminated if no response is
247          * received.
248          */
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;
257                 
258         }
259
260         if (sframes > 0) {
261                 /*
262                  * Add an additional 2 superframes to account for the
263                  * time to send the SET DRP IE command.
264                  */
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);
269 }
270
271 /*
272  * Update a reservations state, and schedule an update of the
273  * transmitted DRP IEs.
274  */
275 static void uwb_rsv_state_update(struct uwb_rsv *rsv,
276                                  enum uwb_rsv_state new_state)
277 {
278         rsv->state = new_state;
279         rsv->ie_valid = false;
280
281         uwb_rsv_dump("SU", rsv);
282
283         uwb_rsv_stroke_timer(rsv);
284         uwb_rsv_sched_update(rsv->rc);
285 }
286
287 static void uwb_rsv_callback(struct uwb_rsv *rsv)
288 {
289         if (rsv->callback)
290                 rsv->callback(rsv);
291 }
292
293 void uwb_rsv_set_state(struct uwb_rsv *rsv, enum uwb_rsv_state new_state)
294 {
295         struct uwb_rsv_move *mv = &rsv->mv;
296
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;
313                 }
314                 return;
315         }
316
317         uwb_rsv_dump("SC", rsv);
318
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;
340                 }
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);
390         }
391 }
392
393 static void uwb_rsv_handle_timeout_work(struct work_struct *work)
394 {
395         struct uwb_rsv *rsv = container_of(work, struct uwb_rsv,
396                                            handle_timeout_work);
397         struct uwb_rc *rc = rsv->rc;
398
399         mutex_lock(&rc->rsvs_mutex);
400
401         uwb_rsv_dump("TO", rsv);
402
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;
408                 }
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;
414                 }
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;
420                 }
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;
426                 }
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:
433                 /*
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.
438                  */
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;
444         }
445
446         uwb_rsv_remove(rsv);
447
448 unlock:
449         mutex_unlock(&rc->rsvs_mutex);
450 }
451
452 static struct uwb_rsv *uwb_rsv_alloc(struct uwb_rc *rc)
453 {
454         struct uwb_rsv *rsv;
455
456         rsv = kzalloc(sizeof(struct uwb_rsv), GFP_KERNEL);
457         if (!rsv)
458                 return NULL;
459
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;
466
467         rsv->rc = rc;
468         INIT_WORK(&rsv->handle_timeout_work, uwb_rsv_handle_timeout_work);
469
470         return rsv;
471 }
472
473 /**
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
478  *
479  * The callback is called when the state of the reservation changes from:
480  *
481  *   - pending to accepted
482  *   - pending to denined
483  *   - accepted to terminated
484  *   - pending to terminated
485  */
486 struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb, void *pal_priv)
487 {
488         struct uwb_rsv *rsv;
489
490         rsv = uwb_rsv_alloc(rc);
491         if (!rsv)
492                 return NULL;
493
494         rsv->callback = cb;
495         rsv->pal_priv = pal_priv;
496
497         return rsv;
498 }
499 EXPORT_SYMBOL_GPL(uwb_rsv_create);
500
501 void uwb_rsv_remove(struct uwb_rsv *rsv)
502 {
503         uwb_rsv_dump("RM", rsv);
504
505         if (rsv->state != UWB_RSV_STATE_NONE)
506                 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
507
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);
511
512         if (uwb_rsv_is_owner(rsv))
513                 uwb_rsv_put_stream(rsv);
514         
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);
519
520         list_del_init(&rsv->rc_node);
521         uwb_rsv_put(rsv);
522 }
523
524 /**
525  * uwb_rsv_destroy - free a UWB reservation structure
526  * @rsv: the reservation to free
527  *
528  * The reservation must already be terminated.
529  */
530 void uwb_rsv_destroy(struct uwb_rsv *rsv)
531 {
532         uwb_rsv_put(rsv);
533 }
534 EXPORT_SYMBOL_GPL(uwb_rsv_destroy);
535
536 /**
537  * usb_rsv_establish - start a reservation establishment
538  * @rsv: the reservation
539  *
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.
543  *
544  * The reservation's callback will be called when the reservation is
545  * accepted, denied or times out.
546  */
547 int uwb_rsv_establish(struct uwb_rsv *rsv)
548 {
549         struct uwb_rc *rc = rsv->rc;
550         struct uwb_mas_bm available;
551         int ret;
552
553         mutex_lock(&rc->rsvs_mutex);
554         ret = uwb_rsv_get_stream(rsv);
555         if (ret)
556                 goto out;
557
558         rsv->tiebreaker = random32() & 1;
559         /* get available mas bitmap */
560         uwb_drp_available(rc, &available);
561
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;
567         }
568
569         ret = uwb_drp_avail_reserve_pending(rc, &rsv->mas);
570         if (ret != 0) {
571                 uwb_rsv_put_stream(rsv);
572                 goto out;
573         }
574
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;
583 }
584 EXPORT_SYMBOL_GPL(uwb_rsv_establish);
585
586 /**
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
592  *
593  * FIXME: implement this once there are PALs that use it.
594  */
595 int uwb_rsv_modify(struct uwb_rsv *rsv, int max_mas, int min_mas, int max_interval)
596 {
597         return -ENOSYS;
598 }
599 EXPORT_SYMBOL_GPL(uwb_rsv_modify);
600
601 /*
602  * move an already established reservation (rc->rsvs_mutex must to be
603  * taken when tis function is called)
604  */
605 int uwb_rsv_try_move(struct uwb_rsv *rsv, struct uwb_mas_bm *available)
606 {
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;
612  
613         if (bow->can_reserve_extra_mases == false)
614                 return -EBUSY;
615
616         mv = &rsv->mv;
617
618         if (uwb_rsv_find_best_allocation(rsv, available, &mv->final_mas) == UWB_RSV_ALLOC_FOUND) {
619
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);
625                 }
626         } else {
627                 dev_dbg(dev, "new allocation not found\n");
628         }
629         
630         return ret;
631 }
632
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)
637 {
638         struct uwb_drp_backoff_win *bow = &rc->bow;
639         struct uwb_rsv *rsv;
640         struct uwb_mas_bm mas;
641         
642         if (bow->can_reserve_extra_mases == false)
643                 return;
644
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);
651                 }
652         }
653         
654 }
655
656 /**
657  * uwb_rsv_terminate - terminate an established reservation
658  * @rsv: the reservation to terminate
659  *
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.
663  *
664  * If applicable, the reference to the target uwb_dev will be released.
665  */
666 void uwb_rsv_terminate(struct uwb_rsv *rsv)
667 {
668         struct uwb_rc *rc = rsv->rc;
669
670         mutex_lock(&rc->rsvs_mutex);
671
672         if (rsv->state != UWB_RSV_STATE_NONE)
673                 uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
674
675         mutex_unlock(&rc->rsvs_mutex);
676 }
677 EXPORT_SYMBOL_GPL(uwb_rsv_terminate);
678
679 /**
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
684  *
685  * Reservation requests from peers are denied unless a PAL accepts it
686  * by calling this function.
687  *
688  * The PAL call uwb_rsv_destroy() for all accepted reservations before
689  * calling uwb_pal_unregister().
690  */
691 void uwb_rsv_accept(struct uwb_rsv *rsv, uwb_rsv_cb_f cb, void *pal_priv)
692 {
693         uwb_rsv_get(rsv);
694
695         rsv->callback = cb;
696         rsv->pal_priv = pal_priv;
697         rsv->state    = UWB_RSV_STATE_T_ACCEPTED;
698 }
699 EXPORT_SYMBOL_GPL(uwb_rsv_accept);
700
701 /*
702  * Is a received DRP IE for this reservation?
703  */
704 static bool uwb_rsv_match(struct uwb_rsv *rsv, struct uwb_dev *src,
705                           struct uwb_ie_drp *drp_ie)
706 {
707         struct uwb_dev_addr *rsv_src;
708         int stream;
709
710         stream = uwb_ie_drp_stream_index(drp_ie);
711
712         if (rsv->stream != stream)
713                 return false;
714
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;
724         }
725         return false;
726 }
727
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)
731 {
732         struct uwb_rsv *rsv;
733         struct uwb_pal *pal;
734         enum uwb_rsv_state state;
735
736         rsv = uwb_rsv_alloc(rc);
737         if (!rsv)
738                 return NULL;
739
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);
749
750         /*
751          * See if any PALs are interested in this reservation. If not,
752          * deny the request.
753          */
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;
761         }
762         mutex_unlock(&rc->uwb_dev.mutex);
763
764         list_add_tail(&rsv->rc_node, &rc->reservations);
765         state = rsv->state;
766         rsv->state = UWB_RSV_STATE_NONE;
767
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);
774         }
775
776         return rsv;
777 }
778
779 /**
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.
783  *
784  * The usable MAS of a reservation may be less than the negotiated MAS
785  * if alien BPs are present.
786  */
787 void uwb_rsv_get_usable_mas(struct uwb_rsv *rsv, struct uwb_mas_bm *mas)
788 {
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);
791 }
792 EXPORT_SYMBOL_GPL(uwb_rsv_get_usable_mas);
793
794 /**
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
799  *
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.
803  */
804 struct uwb_rsv *uwb_rsv_find(struct uwb_rc *rc, struct uwb_dev *src,
805                              struct uwb_ie_drp *drp_ie)
806 {
807         struct uwb_rsv *rsv;
808
809         list_for_each_entry(rsv, &rc->reservations, rc_node) {
810                 if (uwb_rsv_match(rsv, src, drp_ie))
811                         return rsv;
812         }
813
814         if (uwb_ie_drp_owner(drp_ie))
815                 return uwb_rsv_new_target(rc, src, drp_ie);
816
817         return NULL;
818 }
819
820 /*
821  * Go through all the reservations and check for timeouts and (if
822  * necessary) update their DRP IEs.
823  *
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().
826  */
827 static bool uwb_rsv_update_all(struct uwb_rc *rc)
828 {
829         struct uwb_rsv *rsv, *t;
830         bool ie_updated = false;
831
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;
836                 }
837         }
838
839         return ie_updated;
840 }
841
842 void uwb_rsv_queue_update(struct uwb_rc *rc)
843 {
844         unsigned long delay_us = UWB_MAS_LENGTH_US * UWB_MAS_PER_ZONE;
845
846         queue_delayed_work(rc->rsv_workq, &rc->rsv_update_work, usecs_to_jiffies(delay_us));
847 }
848
849 /**
850  * uwb_rsv_sched_update - schedule an update of the DRP IEs
851  * @rc: the radio controller.
852  *
853  * To improve performance and ensure correctness with [ECMA-368] the
854  * number of SET-DRP-IE commands that are done are limited.
855  *
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).
860  *
861  * A delayed work ensures that all the synchronous events result in
862  * one SET-DRP-IE command.
863  *
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.
867  *
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.
870  */
871 void uwb_rsv_sched_update(struct uwb_rc *rc)
872 {
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;
878                 }
879                 uwb_rsv_queue_update(rc);
880         }
881 unlock:
882         spin_unlock(&rc->rsvs_lock);
883 }
884
885 /*
886  * Update DRP IEs and, if necessary, the DRP Availability IE and send
887  * the updated IEs to the radio controller.
888  */
889 static void uwb_rsv_update_work(struct work_struct *work)
890 {
891         struct uwb_rc *rc = container_of(work, struct uwb_rc,
892                                          rsv_update_work.work);
893         bool ie_updated;
894
895         mutex_lock(&rc->rsvs_mutex);
896
897         ie_updated = uwb_rsv_update_all(rc);
898
899         if (!rc->drp_avail.ie_valid) {
900                 uwb_drp_avail_ie_update(rc);
901                 ie_updated = true;
902         }
903
904         if (ie_updated && (rc->set_drp_ie_pending == 0))
905                 uwb_rc_send_all_drp_ie(rc);
906
907         mutex_unlock(&rc->rsvs_mutex);
908 }
909
910 static void uwb_rsv_alien_bp_work(struct work_struct *work)
911 {
912         struct uwb_rc *rc = container_of(work, struct uwb_rc,
913                                          rsv_alien_bp_work.work);
914         struct uwb_rsv *rsv;
915
916         mutex_lock(&rc->rsvs_mutex);
917         
918         list_for_each_entry(rsv, &rc->reservations, rc_node) {
919                 if (rsv->type != UWB_DRP_TYPE_ALIEN_BP) {
920                         rsv->callback(rsv);
921                 }
922         }
923
924         mutex_unlock(&rc->rsvs_mutex);
925 }
926
927 static void uwb_rsv_timer(unsigned long arg)
928 {
929         struct uwb_rsv *rsv = (struct uwb_rsv *)arg;
930
931         queue_work(rsv->rc->rsv_workq, &rsv->handle_timeout_work);
932 }
933
934 /**
935  * uwb_rsv_remove_all - remove all reservations
936  * @rc: the radio controller
937  *
938  * A DRP IE update is not done.
939  */
940 void uwb_rsv_remove_all(struct uwb_rc *rc)
941 {
942         struct uwb_rsv *rsv, *t;
943
944         mutex_lock(&rc->rsvs_mutex);
945         list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
946                 uwb_rsv_remove(rsv);
947         }
948         /* Cancel any postponed update. */
949         rc->set_drp_ie_pending = 0;
950         mutex_unlock(&rc->rsvs_mutex);
951
952         cancel_delayed_work_sync(&rc->rsv_update_work);
953 }
954
955 void uwb_rsv_init(struct uwb_rc *rc)
956 {
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;
969
970         bitmap_complement(rc->uwb_dev.streams, rc->uwb_dev.streams, UWB_NUM_STREAMS);
971 }
972
973 int uwb_rsv_setup(struct uwb_rc *rc)
974 {
975         char name[16];
976
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;
981
982         return 0;
983 }
984
985 void uwb_rsv_cleanup(struct uwb_rc *rc)
986 {
987         uwb_rsv_remove_all(rc);
988         destroy_workqueue(rc->rsv_workq);
989 }