]>
Commit | Line | Data |
---|---|---|
8cc13a09 DV |
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 | */ | |
8cc13a09 DV |
18 | #include <linux/kernel.h> |
19 | #include <linux/uwb.h> | |
20 | ||
21 | #include "uwb-internal.h" | |
22 | ||
23 | static void uwb_rsv_timer(unsigned long arg); | |
24 | ||
25 | static const char *rsv_states[] = { | |
26 | [UWB_RSV_STATE_NONE] = "none", | |
27 | [UWB_RSV_STATE_O_INITIATED] = "initiated", | |
28 | [UWB_RSV_STATE_O_PENDING] = "pending", | |
29 | [UWB_RSV_STATE_O_MODIFIED] = "modified", | |
30 | [UWB_RSV_STATE_O_ESTABLISHED] = "established", | |
31 | [UWB_RSV_STATE_T_ACCEPTED] = "accepted", | |
32 | [UWB_RSV_STATE_T_DENIED] = "denied", | |
33 | [UWB_RSV_STATE_T_PENDING] = "pending", | |
34 | }; | |
35 | ||
36 | static const char *rsv_types[] = { | |
37 | [UWB_DRP_TYPE_ALIEN_BP] = "alien-bp", | |
38 | [UWB_DRP_TYPE_HARD] = "hard", | |
39 | [UWB_DRP_TYPE_SOFT] = "soft", | |
40 | [UWB_DRP_TYPE_PRIVATE] = "private", | |
41 | [UWB_DRP_TYPE_PCA] = "pca", | |
42 | }; | |
43 | ||
44 | /** | |
45 | * uwb_rsv_state_str - return a string for a reservation state | |
46 | * @state: the reservation state. | |
47 | */ | |
48 | const char *uwb_rsv_state_str(enum uwb_rsv_state state) | |
49 | { | |
50 | if (state < UWB_RSV_STATE_NONE || state >= UWB_RSV_STATE_LAST) | |
51 | return "unknown"; | |
52 | return rsv_states[state]; | |
53 | } | |
54 | EXPORT_SYMBOL_GPL(uwb_rsv_state_str); | |
55 | ||
56 | /** | |
57 | * uwb_rsv_type_str - return a string for a reservation type | |
58 | * @type: the reservation type | |
59 | */ | |
60 | const char *uwb_rsv_type_str(enum uwb_drp_type type) | |
61 | { | |
62 | if (type < UWB_DRP_TYPE_ALIEN_BP || type > UWB_DRP_TYPE_PCA) | |
63 | return "invalid"; | |
64 | return rsv_types[type]; | |
65 | } | |
66 | EXPORT_SYMBOL_GPL(uwb_rsv_type_str); | |
67 | ||
68 | static void uwb_rsv_dump(struct uwb_rsv *rsv) | |
69 | { | |
70 | struct device *dev = &rsv->rc->uwb_dev.dev; | |
71 | struct uwb_dev_addr devaddr; | |
72 | char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE]; | |
73 | ||
74 | uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr); | |
75 | if (rsv->target.type == UWB_RSV_TARGET_DEV) | |
76 | devaddr = rsv->target.dev->dev_addr; | |
77 | else | |
78 | devaddr = rsv->target.devaddr; | |
79 | uwb_dev_addr_print(target, sizeof(target), &devaddr); | |
80 | ||
81 | dev_dbg(dev, "rsv %s -> %s: %s\n", owner, target, uwb_rsv_state_str(rsv->state)); | |
82 | } | |
83 | ||
cae1c114 DV |
84 | static void uwb_rsv_release(struct kref *kref) |
85 | { | |
86 | struct uwb_rsv *rsv = container_of(kref, struct uwb_rsv, kref); | |
87 | ||
88 | kfree(rsv); | |
89 | } | |
90 | ||
91 | static void uwb_rsv_get(struct uwb_rsv *rsv) | |
92 | { | |
93 | kref_get(&rsv->kref); | |
94 | } | |
95 | ||
96 | static void uwb_rsv_put(struct uwb_rsv *rsv) | |
97 | { | |
98 | kref_put(&rsv->kref, uwb_rsv_release); | |
99 | } | |
100 | ||
8cc13a09 DV |
101 | /* |
102 | * Get a free stream index for a reservation. | |
103 | * | |
104 | * If the target is a DevAddr (e.g., a WUSB cluster reservation) then | |
105 | * the stream is allocated from a pool of per-RC stream indexes, | |
106 | * otherwise a unique stream index for the target is selected. | |
107 | */ | |
108 | static int uwb_rsv_get_stream(struct uwb_rsv *rsv) | |
109 | { | |
110 | struct uwb_rc *rc = rsv->rc; | |
111 | unsigned long *streams_bm; | |
112 | int stream; | |
113 | ||
114 | switch (rsv->target.type) { | |
115 | case UWB_RSV_TARGET_DEV: | |
116 | streams_bm = rsv->target.dev->streams; | |
117 | break; | |
118 | case UWB_RSV_TARGET_DEVADDR: | |
119 | streams_bm = rc->uwb_dev.streams; | |
120 | break; | |
121 | default: | |
122 | return -EINVAL; | |
123 | } | |
124 | ||
125 | stream = find_first_zero_bit(streams_bm, UWB_NUM_STREAMS); | |
126 | if (stream >= UWB_NUM_STREAMS) | |
127 | return -EBUSY; | |
128 | ||
129 | rsv->stream = stream; | |
130 | set_bit(stream, streams_bm); | |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
135 | static void uwb_rsv_put_stream(struct uwb_rsv *rsv) | |
136 | { | |
137 | struct uwb_rc *rc = rsv->rc; | |
138 | unsigned long *streams_bm; | |
139 | ||
140 | switch (rsv->target.type) { | |
141 | case UWB_RSV_TARGET_DEV: | |
142 | streams_bm = rsv->target.dev->streams; | |
143 | break; | |
144 | case UWB_RSV_TARGET_DEVADDR: | |
145 | streams_bm = rc->uwb_dev.streams; | |
146 | break; | |
147 | default: | |
148 | return; | |
149 | } | |
150 | ||
151 | clear_bit(rsv->stream, streams_bm); | |
152 | } | |
153 | ||
154 | /* | |
155 | * Generate a MAS allocation with a single row component. | |
156 | */ | |
157 | static void uwb_rsv_gen_alloc_row(struct uwb_mas_bm *mas, | |
158 | int first_mas, int mas_per_zone, | |
159 | int zs, int ze) | |
160 | { | |
161 | struct uwb_mas_bm col; | |
162 | int z; | |
163 | ||
164 | bitmap_zero(mas->bm, UWB_NUM_MAS); | |
165 | bitmap_zero(col.bm, UWB_NUM_MAS); | |
166 | bitmap_fill(col.bm, mas_per_zone); | |
167 | bitmap_shift_left(col.bm, col.bm, first_mas + zs * UWB_MAS_PER_ZONE, UWB_NUM_MAS); | |
168 | ||
169 | for (z = zs; z <= ze; z++) { | |
170 | bitmap_or(mas->bm, mas->bm, col.bm, UWB_NUM_MAS); | |
171 | bitmap_shift_left(col.bm, col.bm, UWB_MAS_PER_ZONE, UWB_NUM_MAS); | |
172 | } | |
173 | } | |
174 | ||
175 | /* | |
176 | * Allocate some MAS for this reservation based on current local | |
177 | * availability, the reservation parameters (max_mas, min_mas, | |
178 | * sparsity), and the WiMedia rules for MAS allocations. | |
179 | * | |
180 | * Returns -EBUSY is insufficient free MAS are available. | |
181 | * | |
182 | * FIXME: to simplify this, only safe reservations with a single row | |
183 | * component in zones 1 to 15 are tried (zone 0 is skipped to avoid | |
184 | * problems with the MAS reserved for the BP). | |
185 | * | |
186 | * [ECMA-368] section B.2. | |
187 | */ | |
188 | static int uwb_rsv_alloc_mas(struct uwb_rsv *rsv) | |
189 | { | |
190 | static const int safe_mas_in_row[UWB_NUM_ZONES] = { | |
191 | 8, 7, 6, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 2, 1, | |
192 | }; | |
193 | int n, r; | |
194 | struct uwb_mas_bm mas; | |
195 | bool found = false; | |
196 | ||
197 | /* | |
198 | * Search all valid safe allocations until either: too few MAS | |
199 | * are available; or the smallest allocation with sufficient | |
200 | * MAS is found. | |
201 | * | |
202 | * The top of the zones are preferred, so space for larger | |
203 | * allocations is available in the bottom of the zone (e.g., a | |
204 | * 15 MAS allocation should start in row 14 leaving space for | |
205 | * a 120 MAS allocation at row 0). | |
206 | */ | |
207 | for (n = safe_mas_in_row[0]; n >= 1; n--) { | |
208 | int num_mas; | |
209 | ||
210 | num_mas = n * (UWB_NUM_ZONES - 1); | |
211 | if (num_mas < rsv->min_mas) | |
212 | break; | |
213 | if (found && num_mas < rsv->max_mas) | |
214 | break; | |
215 | ||
216 | for (r = UWB_MAS_PER_ZONE-1; r >= 0; r--) { | |
217 | if (safe_mas_in_row[r] < n) | |
218 | continue; | |
219 | uwb_rsv_gen_alloc_row(&mas, r, n, 1, UWB_NUM_ZONES); | |
220 | if (uwb_drp_avail_reserve_pending(rsv->rc, &mas) == 0) { | |
221 | found = true; | |
222 | break; | |
223 | } | |
224 | } | |
225 | } | |
226 | ||
227 | if (!found) | |
228 | return -EBUSY; | |
229 | ||
230 | bitmap_copy(rsv->mas.bm, mas.bm, UWB_NUM_MAS); | |
231 | return 0; | |
232 | } | |
233 | ||
234 | static void uwb_rsv_stroke_timer(struct uwb_rsv *rsv) | |
235 | { | |
236 | int sframes = UWB_MAX_LOST_BEACONS; | |
237 | ||
238 | /* | |
239 | * Multicast reservations can become established within 1 | |
240 | * super frame and should not be terminated if no response is | |
241 | * received. | |
242 | */ | |
243 | if (rsv->is_multicast) { | |
244 | if (rsv->state == UWB_RSV_STATE_O_INITIATED) | |
245 | sframes = 1; | |
246 | if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED) | |
247 | sframes = 0; | |
248 | } | |
249 | ||
250 | rsv->expired = false; | |
251 | if (sframes > 0) { | |
252 | /* | |
253 | * Add an additional 2 superframes to account for the | |
254 | * time to send the SET DRP IE command. | |
255 | */ | |
256 | unsigned timeout_us = (sframes + 2) * UWB_SUPERFRAME_LENGTH_US; | |
257 | mod_timer(&rsv->timer, jiffies + usecs_to_jiffies(timeout_us)); | |
258 | } else | |
259 | del_timer(&rsv->timer); | |
260 | } | |
261 | ||
262 | /* | |
263 | * Update a reservations state, and schedule an update of the | |
264 | * transmitted DRP IEs. | |
265 | */ | |
266 | static void uwb_rsv_state_update(struct uwb_rsv *rsv, | |
267 | enum uwb_rsv_state new_state) | |
268 | { | |
269 | rsv->state = new_state; | |
270 | rsv->ie_valid = false; | |
271 | ||
272 | uwb_rsv_dump(rsv); | |
273 | ||
274 | uwb_rsv_stroke_timer(rsv); | |
275 | uwb_rsv_sched_update(rsv->rc); | |
276 | } | |
277 | ||
278 | static void uwb_rsv_callback(struct uwb_rsv *rsv) | |
279 | { | |
280 | if (rsv->callback) | |
281 | rsv->callback(rsv); | |
282 | } | |
283 | ||
284 | void uwb_rsv_set_state(struct uwb_rsv *rsv, enum uwb_rsv_state new_state) | |
285 | { | |
286 | if (rsv->state == new_state) { | |
287 | switch (rsv->state) { | |
288 | case UWB_RSV_STATE_O_ESTABLISHED: | |
289 | case UWB_RSV_STATE_T_ACCEPTED: | |
290 | case UWB_RSV_STATE_NONE: | |
291 | uwb_rsv_stroke_timer(rsv); | |
292 | break; | |
293 | default: | |
294 | /* Expecting a state transition so leave timer | |
295 | as-is. */ | |
296 | break; | |
297 | } | |
298 | return; | |
299 | } | |
300 | ||
301 | switch (new_state) { | |
302 | case UWB_RSV_STATE_NONE: | |
303 | uwb_drp_avail_release(rsv->rc, &rsv->mas); | |
6a4b5870 DV |
304 | if (uwb_rsv_is_owner(rsv)) |
305 | uwb_rsv_put_stream(rsv); | |
8cc13a09 DV |
306 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_NONE); |
307 | uwb_rsv_callback(rsv); | |
308 | break; | |
309 | case UWB_RSV_STATE_O_INITIATED: | |
310 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_INITIATED); | |
311 | break; | |
312 | case UWB_RSV_STATE_O_PENDING: | |
313 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_PENDING); | |
314 | break; | |
315 | case UWB_RSV_STATE_O_ESTABLISHED: | |
316 | uwb_drp_avail_reserve(rsv->rc, &rsv->mas); | |
317 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_ESTABLISHED); | |
318 | uwb_rsv_callback(rsv); | |
319 | break; | |
320 | case UWB_RSV_STATE_T_ACCEPTED: | |
321 | uwb_drp_avail_reserve(rsv->rc, &rsv->mas); | |
322 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_ACCEPTED); | |
323 | uwb_rsv_callback(rsv); | |
324 | break; | |
325 | case UWB_RSV_STATE_T_DENIED: | |
326 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_DENIED); | |
327 | break; | |
328 | default: | |
329 | dev_err(&rsv->rc->uwb_dev.dev, "unhandled state: %s (%d)\n", | |
330 | uwb_rsv_state_str(new_state), new_state); | |
331 | } | |
332 | } | |
333 | ||
334 | static struct uwb_rsv *uwb_rsv_alloc(struct uwb_rc *rc) | |
335 | { | |
336 | struct uwb_rsv *rsv; | |
337 | ||
338 | rsv = kzalloc(sizeof(struct uwb_rsv), GFP_KERNEL); | |
339 | if (!rsv) | |
340 | return NULL; | |
341 | ||
342 | INIT_LIST_HEAD(&rsv->rc_node); | |
343 | INIT_LIST_HEAD(&rsv->pal_node); | |
cae1c114 | 344 | kref_init(&rsv->kref); |
8cc13a09 DV |
345 | init_timer(&rsv->timer); |
346 | rsv->timer.function = uwb_rsv_timer; | |
347 | rsv->timer.data = (unsigned long)rsv; | |
348 | ||
349 | rsv->rc = rc; | |
350 | ||
351 | return rsv; | |
352 | } | |
353 | ||
8cc13a09 DV |
354 | /** |
355 | * uwb_rsv_create - allocate and initialize a UWB reservation structure | |
356 | * @rc: the radio controller | |
357 | * @cb: callback to use when the reservation completes or terminates | |
358 | * @pal_priv: data private to the PAL to be passed in the callback | |
359 | * | |
360 | * The callback is called when the state of the reservation changes from: | |
361 | * | |
362 | * - pending to accepted | |
363 | * - pending to denined | |
364 | * - accepted to terminated | |
365 | * - pending to terminated | |
366 | */ | |
367 | struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb, void *pal_priv) | |
368 | { | |
369 | struct uwb_rsv *rsv; | |
370 | ||
371 | rsv = uwb_rsv_alloc(rc); | |
372 | if (!rsv) | |
373 | return NULL; | |
374 | ||
375 | rsv->callback = cb; | |
376 | rsv->pal_priv = pal_priv; | |
377 | ||
378 | return rsv; | |
379 | } | |
380 | EXPORT_SYMBOL_GPL(uwb_rsv_create); | |
381 | ||
382 | void uwb_rsv_remove(struct uwb_rsv *rsv) | |
383 | { | |
384 | if (rsv->state != UWB_RSV_STATE_NONE) | |
385 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE); | |
386 | del_timer_sync(&rsv->timer); | |
cae1c114 DV |
387 | uwb_dev_put(rsv->owner); |
388 | if (rsv->target.type == UWB_RSV_TARGET_DEV) | |
389 | uwb_dev_put(rsv->target.dev); | |
390 | ||
391 | list_del_init(&rsv->rc_node); | |
392 | uwb_rsv_put(rsv); | |
8cc13a09 DV |
393 | } |
394 | ||
395 | /** | |
396 | * uwb_rsv_destroy - free a UWB reservation structure | |
397 | * @rsv: the reservation to free | |
398 | * | |
cae1c114 | 399 | * The reservation must already be terminated. |
8cc13a09 DV |
400 | */ |
401 | void uwb_rsv_destroy(struct uwb_rsv *rsv) | |
402 | { | |
cae1c114 | 403 | uwb_rsv_put(rsv); |
8cc13a09 DV |
404 | } |
405 | EXPORT_SYMBOL_GPL(uwb_rsv_destroy); | |
406 | ||
407 | /** | |
408 | * usb_rsv_establish - start a reservation establishment | |
409 | * @rsv: the reservation | |
410 | * | |
411 | * The PAL should fill in @rsv's owner, target, type, max_mas, | |
412 | * min_mas, sparsity and is_multicast fields. If the target is a | |
413 | * uwb_dev it must be referenced. | |
414 | * | |
415 | * The reservation's callback will be called when the reservation is | |
416 | * accepted, denied or times out. | |
417 | */ | |
418 | int uwb_rsv_establish(struct uwb_rsv *rsv) | |
419 | { | |
420 | struct uwb_rc *rc = rsv->rc; | |
421 | int ret; | |
422 | ||
423 | mutex_lock(&rc->rsvs_mutex); | |
424 | ||
425 | ret = uwb_rsv_get_stream(rsv); | |
426 | if (ret) | |
427 | goto out; | |
428 | ||
429 | ret = uwb_rsv_alloc_mas(rsv); | |
430 | if (ret) { | |
431 | uwb_rsv_put_stream(rsv); | |
432 | goto out; | |
433 | } | |
434 | ||
cae1c114 | 435 | uwb_rsv_get(rsv); |
8cc13a09 DV |
436 | list_add_tail(&rsv->rc_node, &rc->reservations); |
437 | rsv->owner = &rc->uwb_dev; | |
438 | uwb_dev_get(rsv->owner); | |
439 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_INITIATED); | |
440 | out: | |
441 | mutex_unlock(&rc->rsvs_mutex); | |
442 | return ret; | |
443 | } | |
444 | EXPORT_SYMBOL_GPL(uwb_rsv_establish); | |
445 | ||
446 | /** | |
447 | * uwb_rsv_modify - modify an already established reservation | |
448 | * @rsv: the reservation to modify | |
449 | * @max_mas: new maximum MAS to reserve | |
450 | * @min_mas: new minimum MAS to reserve | |
451 | * @sparsity: new sparsity to use | |
452 | * | |
453 | * FIXME: implement this once there are PALs that use it. | |
454 | */ | |
455 | int uwb_rsv_modify(struct uwb_rsv *rsv, int max_mas, int min_mas, int sparsity) | |
456 | { | |
457 | return -ENOSYS; | |
458 | } | |
459 | EXPORT_SYMBOL_GPL(uwb_rsv_modify); | |
460 | ||
461 | /** | |
462 | * uwb_rsv_terminate - terminate an established reservation | |
463 | * @rsv: the reservation to terminate | |
464 | * | |
465 | * A reservation is terminated by removing the DRP IE from the beacon, | |
466 | * the other end will consider the reservation to be terminated when | |
467 | * it does not see the DRP IE for at least mMaxLostBeacons. | |
468 | * | |
469 | * If applicable, the reference to the target uwb_dev will be released. | |
470 | */ | |
471 | void uwb_rsv_terminate(struct uwb_rsv *rsv) | |
472 | { | |
473 | struct uwb_rc *rc = rsv->rc; | |
474 | ||
475 | mutex_lock(&rc->rsvs_mutex); | |
476 | ||
477 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE); | |
478 | ||
479 | mutex_unlock(&rc->rsvs_mutex); | |
480 | } | |
481 | EXPORT_SYMBOL_GPL(uwb_rsv_terminate); | |
482 | ||
483 | /** | |
484 | * uwb_rsv_accept - accept a new reservation from a peer | |
485 | * @rsv: the reservation | |
486 | * @cb: call back for reservation changes | |
487 | * @pal_priv: data to be passed in the above call back | |
488 | * | |
489 | * Reservation requests from peers are denied unless a PAL accepts it | |
490 | * by calling this function. | |
cae1c114 DV |
491 | * |
492 | * The PAL call uwb_rsv_destroy() for all accepted reservations before | |
493 | * calling uwb_pal_unregister(). | |
8cc13a09 DV |
494 | */ |
495 | void uwb_rsv_accept(struct uwb_rsv *rsv, uwb_rsv_cb_f cb, void *pal_priv) | |
496 | { | |
cae1c114 DV |
497 | uwb_rsv_get(rsv); |
498 | ||
8cc13a09 DV |
499 | rsv->callback = cb; |
500 | rsv->pal_priv = pal_priv; | |
501 | rsv->state = UWB_RSV_STATE_T_ACCEPTED; | |
502 | } | |
503 | EXPORT_SYMBOL_GPL(uwb_rsv_accept); | |
504 | ||
505 | /* | |
506 | * Is a received DRP IE for this reservation? | |
507 | */ | |
508 | static bool uwb_rsv_match(struct uwb_rsv *rsv, struct uwb_dev *src, | |
509 | struct uwb_ie_drp *drp_ie) | |
510 | { | |
511 | struct uwb_dev_addr *rsv_src; | |
512 | int stream; | |
513 | ||
514 | stream = uwb_ie_drp_stream_index(drp_ie); | |
515 | ||
516 | if (rsv->stream != stream) | |
517 | return false; | |
518 | ||
519 | switch (rsv->target.type) { | |
520 | case UWB_RSV_TARGET_DEVADDR: | |
521 | return rsv->stream == stream; | |
522 | case UWB_RSV_TARGET_DEV: | |
523 | if (uwb_ie_drp_owner(drp_ie)) | |
524 | rsv_src = &rsv->owner->dev_addr; | |
525 | else | |
526 | rsv_src = &rsv->target.dev->dev_addr; | |
527 | return uwb_dev_addr_cmp(&src->dev_addr, rsv_src) == 0; | |
528 | } | |
529 | return false; | |
530 | } | |
531 | ||
532 | static struct uwb_rsv *uwb_rsv_new_target(struct uwb_rc *rc, | |
533 | struct uwb_dev *src, | |
534 | struct uwb_ie_drp *drp_ie) | |
535 | { | |
536 | struct uwb_rsv *rsv; | |
537 | struct uwb_pal *pal; | |
538 | enum uwb_rsv_state state; | |
539 | ||
540 | rsv = uwb_rsv_alloc(rc); | |
541 | if (!rsv) | |
542 | return NULL; | |
543 | ||
544 | rsv->rc = rc; | |
545 | rsv->owner = src; | |
546 | uwb_dev_get(rsv->owner); | |
547 | rsv->target.type = UWB_RSV_TARGET_DEV; | |
548 | rsv->target.dev = &rc->uwb_dev; | |
549 | rsv->type = uwb_ie_drp_type(drp_ie); | |
550 | rsv->stream = uwb_ie_drp_stream_index(drp_ie); | |
8cc13a09 DV |
551 | uwb_drp_ie_to_bm(&rsv->mas, drp_ie); |
552 | ||
553 | /* | |
554 | * See if any PALs are interested in this reservation. If not, | |
555 | * deny the request. | |
556 | */ | |
557 | rsv->state = UWB_RSV_STATE_T_DENIED; | |
6fae35f9 | 558 | mutex_lock(&rc->uwb_dev.mutex); |
8cc13a09 DV |
559 | list_for_each_entry(pal, &rc->pals, node) { |
560 | if (pal->new_rsv) | |
e17be2b2 | 561 | pal->new_rsv(pal, rsv); |
8cc13a09 DV |
562 | if (rsv->state == UWB_RSV_STATE_T_ACCEPTED) |
563 | break; | |
564 | } | |
6fae35f9 | 565 | mutex_unlock(&rc->uwb_dev.mutex); |
8cc13a09 DV |
566 | |
567 | list_add_tail(&rsv->rc_node, &rc->reservations); | |
568 | state = rsv->state; | |
569 | rsv->state = UWB_RSV_STATE_NONE; | |
570 | uwb_rsv_set_state(rsv, state); | |
571 | ||
572 | return rsv; | |
573 | } | |
574 | ||
575 | /** | |
576 | * uwb_rsv_find - find a reservation for a received DRP IE. | |
577 | * @rc: the radio controller | |
578 | * @src: source of the DRP IE | |
579 | * @drp_ie: the DRP IE | |
580 | * | |
581 | * If the reservation cannot be found and the DRP IE is from a peer | |
582 | * attempting to establish a new reservation, create a new reservation | |
583 | * and add it to the list. | |
584 | */ | |
585 | struct uwb_rsv *uwb_rsv_find(struct uwb_rc *rc, struct uwb_dev *src, | |
586 | struct uwb_ie_drp *drp_ie) | |
587 | { | |
588 | struct uwb_rsv *rsv; | |
589 | ||
590 | list_for_each_entry(rsv, &rc->reservations, rc_node) { | |
591 | if (uwb_rsv_match(rsv, src, drp_ie)) | |
592 | return rsv; | |
593 | } | |
594 | ||
595 | if (uwb_ie_drp_owner(drp_ie)) | |
596 | return uwb_rsv_new_target(rc, src, drp_ie); | |
597 | ||
598 | return NULL; | |
599 | } | |
600 | ||
601 | /* | |
602 | * Go through all the reservations and check for timeouts and (if | |
603 | * necessary) update their DRP IEs. | |
604 | * | |
605 | * FIXME: look at building the SET_DRP_IE command here rather than | |
606 | * having to rescan the list in uwb_rc_send_all_drp_ie(). | |
607 | */ | |
608 | static bool uwb_rsv_update_all(struct uwb_rc *rc) | |
609 | { | |
610 | struct uwb_rsv *rsv, *t; | |
611 | bool ie_updated = false; | |
612 | ||
613 | list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) { | |
614 | if (rsv->expired) | |
615 | uwb_drp_handle_timeout(rsv); | |
616 | if (!rsv->ie_valid) { | |
617 | uwb_drp_ie_update(rsv); | |
618 | ie_updated = true; | |
619 | } | |
620 | } | |
621 | ||
622 | return ie_updated; | |
623 | } | |
624 | ||
625 | void uwb_rsv_sched_update(struct uwb_rc *rc) | |
626 | { | |
627 | queue_work(rc->rsv_workq, &rc->rsv_update_work); | |
628 | } | |
629 | ||
630 | /* | |
631 | * Update DRP IEs and, if necessary, the DRP Availability IE and send | |
632 | * the updated IEs to the radio controller. | |
633 | */ | |
634 | static void uwb_rsv_update_work(struct work_struct *work) | |
635 | { | |
636 | struct uwb_rc *rc = container_of(work, struct uwb_rc, rsv_update_work); | |
637 | bool ie_updated; | |
638 | ||
639 | mutex_lock(&rc->rsvs_mutex); | |
640 | ||
641 | ie_updated = uwb_rsv_update_all(rc); | |
642 | ||
643 | if (!rc->drp_avail.ie_valid) { | |
644 | uwb_drp_avail_ie_update(rc); | |
645 | ie_updated = true; | |
646 | } | |
647 | ||
648 | if (ie_updated) | |
649 | uwb_rc_send_all_drp_ie(rc); | |
650 | ||
651 | mutex_unlock(&rc->rsvs_mutex); | |
652 | } | |
653 | ||
654 | static void uwb_rsv_timer(unsigned long arg) | |
655 | { | |
656 | struct uwb_rsv *rsv = (struct uwb_rsv *)arg; | |
657 | ||
658 | rsv->expired = true; | |
659 | uwb_rsv_sched_update(rsv->rc); | |
660 | } | |
661 | ||
307ba6dd DV |
662 | /** |
663 | * uwb_rsv_remove_all - remove all reservations | |
664 | * @rc: the radio controller | |
665 | * | |
666 | * A DRP IE update is not done. | |
667 | */ | |
668 | void uwb_rsv_remove_all(struct uwb_rc *rc) | |
669 | { | |
670 | struct uwb_rsv *rsv, *t; | |
671 | ||
672 | mutex_lock(&rc->rsvs_mutex); | |
673 | list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) { | |
674 | uwb_rsv_remove(rsv); | |
675 | } | |
676 | mutex_unlock(&rc->rsvs_mutex); | |
677 | ||
678 | cancel_work_sync(&rc->rsv_update_work); | |
679 | } | |
680 | ||
8cc13a09 DV |
681 | void uwb_rsv_init(struct uwb_rc *rc) |
682 | { | |
683 | INIT_LIST_HEAD(&rc->reservations); | |
684 | mutex_init(&rc->rsvs_mutex); | |
685 | INIT_WORK(&rc->rsv_update_work, uwb_rsv_update_work); | |
686 | ||
687 | bitmap_complement(rc->uwb_dev.streams, rc->uwb_dev.streams, UWB_NUM_STREAMS); | |
688 | } | |
689 | ||
690 | int uwb_rsv_setup(struct uwb_rc *rc) | |
691 | { | |
692 | char name[16]; | |
693 | ||
694 | snprintf(name, sizeof(name), "%s_rsvd", dev_name(&rc->uwb_dev.dev)); | |
695 | rc->rsv_workq = create_singlethread_workqueue(name); | |
696 | if (rc->rsv_workq == NULL) | |
697 | return -ENOMEM; | |
698 | ||
699 | return 0; | |
700 | } | |
701 | ||
702 | void uwb_rsv_cleanup(struct uwb_rc *rc) | |
703 | { | |
307ba6dd | 704 | uwb_rsv_remove_all(rc); |
8cc13a09 DV |
705 | destroy_workqueue(rc->rsv_workq); |
706 | } |