David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 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 | */ |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/uwb.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 21 | #include <linux/random.h> |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 22 | |
| 23 | #include "uwb-internal.h" |
| 24 | |
| 25 | static void uwb_rsv_timer(unsigned long arg); |
| 26 | |
| 27 | static const char *rsv_states[] = { |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 28 | [UWB_RSV_STATE_NONE] = "none ", |
| 29 | [UWB_RSV_STATE_O_INITIATED] = "o initiated ", |
| 30 | [UWB_RSV_STATE_O_PENDING] = "o pending ", |
| 31 | [UWB_RSV_STATE_O_MODIFIED] = "o modified ", |
| 32 | [UWB_RSV_STATE_O_ESTABLISHED] = "o established ", |
| 33 | [UWB_RSV_STATE_O_TO_BE_MOVED] = "o to be moved ", |
| 34 | [UWB_RSV_STATE_O_MOVE_EXPANDING] = "o move expanding", |
| 35 | [UWB_RSV_STATE_O_MOVE_COMBINING] = "o move combining", |
| 36 | [UWB_RSV_STATE_O_MOVE_REDUCING] = "o move reducing ", |
| 37 | [UWB_RSV_STATE_T_ACCEPTED] = "t accepted ", |
| 38 | [UWB_RSV_STATE_T_CONFLICT] = "t conflict ", |
| 39 | [UWB_RSV_STATE_T_PENDING] = "t pending ", |
| 40 | [UWB_RSV_STATE_T_DENIED] = "t denied ", |
| 41 | [UWB_RSV_STATE_T_RESIZED] = "t resized ", |
| 42 | [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = "t expanding acc ", |
| 43 | [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = "t expanding conf", |
| 44 | [UWB_RSV_STATE_T_EXPANDING_PENDING] = "t expanding pend", |
| 45 | [UWB_RSV_STATE_T_EXPANDING_DENIED] = "t expanding den ", |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static const char *rsv_types[] = { |
| 49 | [UWB_DRP_TYPE_ALIEN_BP] = "alien-bp", |
| 50 | [UWB_DRP_TYPE_HARD] = "hard", |
| 51 | [UWB_DRP_TYPE_SOFT] = "soft", |
| 52 | [UWB_DRP_TYPE_PRIVATE] = "private", |
| 53 | [UWB_DRP_TYPE_PCA] = "pca", |
| 54 | }; |
| 55 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 56 | bool uwb_rsv_has_two_drp_ies(struct uwb_rsv *rsv) |
| 57 | { |
| 58 | static const bool has_two_drp_ies[] = { |
| 59 | [UWB_RSV_STATE_O_INITIATED] = false, |
| 60 | [UWB_RSV_STATE_O_PENDING] = false, |
| 61 | [UWB_RSV_STATE_O_MODIFIED] = false, |
| 62 | [UWB_RSV_STATE_O_ESTABLISHED] = false, |
| 63 | [UWB_RSV_STATE_O_TO_BE_MOVED] = false, |
| 64 | [UWB_RSV_STATE_O_MOVE_COMBINING] = false, |
| 65 | [UWB_RSV_STATE_O_MOVE_REDUCING] = false, |
| 66 | [UWB_RSV_STATE_O_MOVE_EXPANDING] = true, |
| 67 | [UWB_RSV_STATE_T_ACCEPTED] = false, |
| 68 | [UWB_RSV_STATE_T_CONFLICT] = false, |
| 69 | [UWB_RSV_STATE_T_PENDING] = false, |
| 70 | [UWB_RSV_STATE_T_DENIED] = false, |
| 71 | [UWB_RSV_STATE_T_RESIZED] = false, |
| 72 | [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = true, |
| 73 | [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = true, |
| 74 | [UWB_RSV_STATE_T_EXPANDING_PENDING] = true, |
| 75 | [UWB_RSV_STATE_T_EXPANDING_DENIED] = true, |
| 76 | }; |
| 77 | |
| 78 | return has_two_drp_ies[rsv->state]; |
| 79 | } |
| 80 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 81 | /** |
| 82 | * uwb_rsv_state_str - return a string for a reservation state |
| 83 | * @state: the reservation state. |
| 84 | */ |
| 85 | const char *uwb_rsv_state_str(enum uwb_rsv_state state) |
| 86 | { |
| 87 | if (state < UWB_RSV_STATE_NONE || state >= UWB_RSV_STATE_LAST) |
| 88 | return "unknown"; |
| 89 | return rsv_states[state]; |
| 90 | } |
| 91 | EXPORT_SYMBOL_GPL(uwb_rsv_state_str); |
| 92 | |
| 93 | /** |
| 94 | * uwb_rsv_type_str - return a string for a reservation type |
| 95 | * @type: the reservation type |
| 96 | */ |
| 97 | const char *uwb_rsv_type_str(enum uwb_drp_type type) |
| 98 | { |
| 99 | if (type < UWB_DRP_TYPE_ALIEN_BP || type > UWB_DRP_TYPE_PCA) |
| 100 | return "invalid"; |
| 101 | return rsv_types[type]; |
| 102 | } |
| 103 | EXPORT_SYMBOL_GPL(uwb_rsv_type_str); |
| 104 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 105 | void uwb_rsv_dump(char *text, struct uwb_rsv *rsv) |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 106 | { |
| 107 | struct device *dev = &rsv->rc->uwb_dev.dev; |
| 108 | struct uwb_dev_addr devaddr; |
| 109 | char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE]; |
| 110 | |
| 111 | uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr); |
| 112 | if (rsv->target.type == UWB_RSV_TARGET_DEV) |
| 113 | devaddr = rsv->target.dev->dev_addr; |
| 114 | else |
| 115 | devaddr = rsv->target.devaddr; |
| 116 | uwb_dev_addr_print(target, sizeof(target), &devaddr); |
| 117 | |
David Vrabel | 2226b1c | 2009-01-06 17:55:32 +0000 | [diff] [blame] | 118 | dev_dbg(dev, "rsv %s %s -> %s: %s\n", |
| 119 | text, owner, target, uwb_rsv_state_str(rsv->state)); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 120 | } |
| 121 | |
David Vrabel | cae1c11 | 2008-10-27 15:22:46 +0000 | [diff] [blame] | 122 | static void uwb_rsv_release(struct kref *kref) |
| 123 | { |
| 124 | struct uwb_rsv *rsv = container_of(kref, struct uwb_rsv, kref); |
| 125 | |
| 126 | kfree(rsv); |
| 127 | } |
| 128 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 129 | void uwb_rsv_get(struct uwb_rsv *rsv) |
David Vrabel | cae1c11 | 2008-10-27 15:22:46 +0000 | [diff] [blame] | 130 | { |
| 131 | kref_get(&rsv->kref); |
| 132 | } |
| 133 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 134 | void uwb_rsv_put(struct uwb_rsv *rsv) |
David Vrabel | cae1c11 | 2008-10-27 15:22:46 +0000 | [diff] [blame] | 135 | { |
| 136 | kref_put(&rsv->kref, uwb_rsv_release); |
| 137 | } |
| 138 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 139 | /* |
| 140 | * Get a free stream index for a reservation. |
| 141 | * |
| 142 | * If the target is a DevAddr (e.g., a WUSB cluster reservation) then |
| 143 | * the stream is allocated from a pool of per-RC stream indexes, |
| 144 | * otherwise a unique stream index for the target is selected. |
| 145 | */ |
| 146 | static int uwb_rsv_get_stream(struct uwb_rsv *rsv) |
| 147 | { |
| 148 | struct uwb_rc *rc = rsv->rc; |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 149 | struct device *dev = &rc->uwb_dev.dev; |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 150 | unsigned long *streams_bm; |
| 151 | int stream; |
| 152 | |
| 153 | switch (rsv->target.type) { |
| 154 | case UWB_RSV_TARGET_DEV: |
| 155 | streams_bm = rsv->target.dev->streams; |
| 156 | break; |
| 157 | case UWB_RSV_TARGET_DEVADDR: |
| 158 | streams_bm = rc->uwb_dev.streams; |
| 159 | break; |
| 160 | default: |
| 161 | return -EINVAL; |
| 162 | } |
| 163 | |
| 164 | stream = find_first_zero_bit(streams_bm, UWB_NUM_STREAMS); |
| 165 | if (stream >= UWB_NUM_STREAMS) |
| 166 | return -EBUSY; |
| 167 | |
| 168 | rsv->stream = stream; |
| 169 | set_bit(stream, streams_bm); |
| 170 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 171 | dev_dbg(dev, "get stream %d\n", rsv->stream); |
| 172 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static void uwb_rsv_put_stream(struct uwb_rsv *rsv) |
| 177 | { |
| 178 | struct uwb_rc *rc = rsv->rc; |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 179 | struct device *dev = &rc->uwb_dev.dev; |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 180 | unsigned long *streams_bm; |
| 181 | |
| 182 | switch (rsv->target.type) { |
| 183 | case UWB_RSV_TARGET_DEV: |
| 184 | streams_bm = rsv->target.dev->streams; |
| 185 | break; |
| 186 | case UWB_RSV_TARGET_DEVADDR: |
| 187 | streams_bm = rc->uwb_dev.streams; |
| 188 | break; |
| 189 | default: |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | clear_bit(rsv->stream, streams_bm); |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 194 | |
| 195 | dev_dbg(dev, "put stream %d\n", rsv->stream); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 198 | void uwb_rsv_backoff_win_timer(unsigned long arg) |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 199 | { |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 200 | struct uwb_drp_backoff_win *bow = (struct uwb_drp_backoff_win *)arg; |
| 201 | struct uwb_rc *rc = container_of(bow, struct uwb_rc, bow); |
| 202 | struct device *dev = &rc->uwb_dev.dev; |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 203 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 204 | bow->can_reserve_extra_mases = true; |
| 205 | if (bow->total_expired <= 4) { |
| 206 | bow->total_expired++; |
| 207 | } else { |
| 208 | /* after 4 backoff window has expired we can exit from |
| 209 | * the backoff procedure */ |
| 210 | bow->total_expired = 0; |
| 211 | bow->window = UWB_DRP_BACKOFF_WIN_MIN >> 1; |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 212 | } |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 213 | dev_dbg(dev, "backoff_win_timer total_expired=%d, n=%d\n: ", bow->total_expired, bow->n); |
| 214 | |
| 215 | /* try to relocate all the "to be moved" relocations */ |
| 216 | uwb_rsv_handle_drp_avail_change(rc); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 219 | void uwb_rsv_backoff_win_increment(struct uwb_rc *rc) |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 220 | { |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 221 | struct uwb_drp_backoff_win *bow = &rc->bow; |
| 222 | struct device *dev = &rc->uwb_dev.dev; |
| 223 | unsigned timeout_us; |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 224 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 225 | dev_dbg(dev, "backoff_win_increment: window=%d\n", bow->window); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 226 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 227 | bow->can_reserve_extra_mases = false; |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 228 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 229 | if((bow->window << 1) == UWB_DRP_BACKOFF_WIN_MAX) |
| 230 | return; |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 231 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 232 | bow->window <<= 1; |
| 233 | bow->n = random32() & (bow->window - 1); |
| 234 | dev_dbg(dev, "new_window=%d, n=%d\n: ", bow->window, bow->n); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 235 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 236 | /* reset the timer associated variables */ |
| 237 | timeout_us = bow->n * UWB_SUPERFRAME_LENGTH_US; |
| 238 | bow->total_expired = 0; |
| 239 | mod_timer(&bow->timer, jiffies + usecs_to_jiffies(timeout_us)); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static void uwb_rsv_stroke_timer(struct uwb_rsv *rsv) |
| 243 | { |
| 244 | int sframes = UWB_MAX_LOST_BEACONS; |
| 245 | |
| 246 | /* |
| 247 | * Multicast reservations can become established within 1 |
| 248 | * super frame and should not be terminated if no response is |
| 249 | * received. |
| 250 | */ |
| 251 | if (rsv->is_multicast) { |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 252 | if (rsv->state == UWB_RSV_STATE_O_INITIATED |
| 253 | || rsv->state == UWB_RSV_STATE_O_MOVE_EXPANDING |
| 254 | || rsv->state == UWB_RSV_STATE_O_MOVE_COMBINING |
| 255 | || rsv->state == UWB_RSV_STATE_O_MOVE_REDUCING) |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 256 | sframes = 1; |
| 257 | if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED) |
| 258 | sframes = 0; |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 259 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 260 | } |
| 261 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 262 | if (sframes > 0) { |
| 263 | /* |
| 264 | * Add an additional 2 superframes to account for the |
| 265 | * time to send the SET DRP IE command. |
| 266 | */ |
| 267 | unsigned timeout_us = (sframes + 2) * UWB_SUPERFRAME_LENGTH_US; |
| 268 | mod_timer(&rsv->timer, jiffies + usecs_to_jiffies(timeout_us)); |
| 269 | } else |
| 270 | del_timer(&rsv->timer); |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Update a reservations state, and schedule an update of the |
| 275 | * transmitted DRP IEs. |
| 276 | */ |
| 277 | static void uwb_rsv_state_update(struct uwb_rsv *rsv, |
| 278 | enum uwb_rsv_state new_state) |
| 279 | { |
| 280 | rsv->state = new_state; |
| 281 | rsv->ie_valid = false; |
| 282 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 283 | uwb_rsv_dump("SU", rsv); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 284 | |
| 285 | uwb_rsv_stroke_timer(rsv); |
| 286 | uwb_rsv_sched_update(rsv->rc); |
| 287 | } |
| 288 | |
| 289 | static void uwb_rsv_callback(struct uwb_rsv *rsv) |
| 290 | { |
| 291 | if (rsv->callback) |
| 292 | rsv->callback(rsv); |
| 293 | } |
| 294 | |
| 295 | void uwb_rsv_set_state(struct uwb_rsv *rsv, enum uwb_rsv_state new_state) |
| 296 | { |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 297 | struct uwb_rsv_move *mv = &rsv->mv; |
| 298 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 299 | if (rsv->state == new_state) { |
| 300 | switch (rsv->state) { |
| 301 | case UWB_RSV_STATE_O_ESTABLISHED: |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 302 | case UWB_RSV_STATE_O_MOVE_EXPANDING: |
| 303 | case UWB_RSV_STATE_O_MOVE_COMBINING: |
| 304 | case UWB_RSV_STATE_O_MOVE_REDUCING: |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 305 | case UWB_RSV_STATE_T_ACCEPTED: |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 306 | case UWB_RSV_STATE_T_EXPANDING_ACCEPTED: |
| 307 | case UWB_RSV_STATE_T_RESIZED: |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 308 | case UWB_RSV_STATE_NONE: |
| 309 | uwb_rsv_stroke_timer(rsv); |
| 310 | break; |
| 311 | default: |
| 312 | /* Expecting a state transition so leave timer |
| 313 | as-is. */ |
| 314 | break; |
| 315 | } |
| 316 | return; |
| 317 | } |
| 318 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 319 | uwb_rsv_dump("SC", rsv); |
| 320 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 321 | switch (new_state) { |
| 322 | case UWB_RSV_STATE_NONE: |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 323 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_NONE); |
| 324 | uwb_rsv_callback(rsv); |
| 325 | break; |
| 326 | case UWB_RSV_STATE_O_INITIATED: |
| 327 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_INITIATED); |
| 328 | break; |
| 329 | case UWB_RSV_STATE_O_PENDING: |
| 330 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_PENDING); |
| 331 | break; |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 332 | case UWB_RSV_STATE_O_MODIFIED: |
| 333 | /* in the companion there are the MASes to drop */ |
| 334 | bitmap_andnot(rsv->mas.bm, rsv->mas.bm, mv->companion_mas.bm, UWB_NUM_MAS); |
| 335 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MODIFIED); |
| 336 | break; |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 337 | case UWB_RSV_STATE_O_ESTABLISHED: |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 338 | if (rsv->state == UWB_RSV_STATE_O_MODIFIED |
| 339 | || rsv->state == UWB_RSV_STATE_O_MOVE_REDUCING) { |
| 340 | uwb_drp_avail_release(rsv->rc, &mv->companion_mas); |
| 341 | rsv->needs_release_companion_mas = false; |
| 342 | } |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 343 | uwb_drp_avail_reserve(rsv->rc, &rsv->mas); |
| 344 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_ESTABLISHED); |
| 345 | uwb_rsv_callback(rsv); |
| 346 | break; |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 347 | case UWB_RSV_STATE_O_MOVE_EXPANDING: |
| 348 | rsv->needs_release_companion_mas = true; |
| 349 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_EXPANDING); |
| 350 | break; |
| 351 | case UWB_RSV_STATE_O_MOVE_COMBINING: |
| 352 | rsv->needs_release_companion_mas = false; |
| 353 | uwb_drp_avail_reserve(rsv->rc, &mv->companion_mas); |
| 354 | bitmap_or(rsv->mas.bm, rsv->mas.bm, mv->companion_mas.bm, UWB_NUM_MAS); |
| 355 | rsv->mas.safe += mv->companion_mas.safe; |
| 356 | rsv->mas.unsafe += mv->companion_mas.unsafe; |
| 357 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_COMBINING); |
| 358 | break; |
| 359 | case UWB_RSV_STATE_O_MOVE_REDUCING: |
| 360 | bitmap_andnot(mv->companion_mas.bm, rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS); |
| 361 | rsv->needs_release_companion_mas = true; |
| 362 | rsv->mas.safe = mv->final_mas.safe; |
| 363 | rsv->mas.unsafe = mv->final_mas.unsafe; |
| 364 | bitmap_copy(rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS); |
| 365 | bitmap_copy(rsv->mas.unsafe_bm, mv->final_mas.unsafe_bm, UWB_NUM_MAS); |
| 366 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_REDUCING); |
| 367 | break; |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 368 | case UWB_RSV_STATE_T_ACCEPTED: |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 369 | case UWB_RSV_STATE_T_RESIZED: |
| 370 | rsv->needs_release_companion_mas = false; |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 371 | uwb_drp_avail_reserve(rsv->rc, &rsv->mas); |
| 372 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_ACCEPTED); |
| 373 | uwb_rsv_callback(rsv); |
| 374 | break; |
| 375 | case UWB_RSV_STATE_T_DENIED: |
| 376 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_DENIED); |
| 377 | break; |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 378 | case UWB_RSV_STATE_T_CONFLICT: |
| 379 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_CONFLICT); |
| 380 | break; |
| 381 | case UWB_RSV_STATE_T_PENDING: |
| 382 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_PENDING); |
| 383 | break; |
| 384 | case UWB_RSV_STATE_T_EXPANDING_ACCEPTED: |
| 385 | rsv->needs_release_companion_mas = true; |
| 386 | uwb_drp_avail_reserve(rsv->rc, &mv->companion_mas); |
| 387 | uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_EXPANDING_ACCEPTED); |
| 388 | break; |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 389 | default: |
| 390 | dev_err(&rsv->rc->uwb_dev.dev, "unhandled state: %s (%d)\n", |
| 391 | uwb_rsv_state_str(new_state), new_state); |
| 392 | } |
| 393 | } |
| 394 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 395 | static void uwb_rsv_handle_timeout_work(struct work_struct *work) |
| 396 | { |
| 397 | struct uwb_rsv *rsv = container_of(work, struct uwb_rsv, |
| 398 | handle_timeout_work); |
| 399 | struct uwb_rc *rc = rsv->rc; |
| 400 | |
| 401 | mutex_lock(&rc->rsvs_mutex); |
| 402 | |
| 403 | uwb_rsv_dump("TO", rsv); |
| 404 | |
| 405 | switch (rsv->state) { |
| 406 | case UWB_RSV_STATE_O_INITIATED: |
| 407 | if (rsv->is_multicast) { |
| 408 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED); |
| 409 | goto unlock; |
| 410 | } |
| 411 | break; |
| 412 | case UWB_RSV_STATE_O_MOVE_EXPANDING: |
| 413 | if (rsv->is_multicast) { |
| 414 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_COMBINING); |
| 415 | goto unlock; |
| 416 | } |
| 417 | break; |
| 418 | case UWB_RSV_STATE_O_MOVE_COMBINING: |
| 419 | if (rsv->is_multicast) { |
| 420 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_REDUCING); |
| 421 | goto unlock; |
| 422 | } |
| 423 | break; |
| 424 | case UWB_RSV_STATE_O_MOVE_REDUCING: |
| 425 | if (rsv->is_multicast) { |
| 426 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED); |
| 427 | goto unlock; |
| 428 | } |
| 429 | break; |
| 430 | case UWB_RSV_STATE_O_ESTABLISHED: |
| 431 | if (rsv->is_multicast) |
| 432 | goto unlock; |
| 433 | break; |
| 434 | case UWB_RSV_STATE_T_EXPANDING_ACCEPTED: |
| 435 | /* |
| 436 | * The time out could be for the main or of the |
| 437 | * companion DRP, assume it's for the companion and |
| 438 | * drop that first. A further time out is required to |
| 439 | * drop the main. |
| 440 | */ |
| 441 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_T_ACCEPTED); |
| 442 | uwb_drp_avail_release(rsv->rc, &rsv->mv.companion_mas); |
| 443 | goto unlock; |
| 444 | default: |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | uwb_rsv_remove(rsv); |
| 449 | |
| 450 | unlock: |
| 451 | mutex_unlock(&rc->rsvs_mutex); |
| 452 | } |
| 453 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 454 | static struct uwb_rsv *uwb_rsv_alloc(struct uwb_rc *rc) |
| 455 | { |
| 456 | struct uwb_rsv *rsv; |
| 457 | |
| 458 | rsv = kzalloc(sizeof(struct uwb_rsv), GFP_KERNEL); |
| 459 | if (!rsv) |
| 460 | return NULL; |
| 461 | |
| 462 | INIT_LIST_HEAD(&rsv->rc_node); |
| 463 | INIT_LIST_HEAD(&rsv->pal_node); |
David Vrabel | cae1c11 | 2008-10-27 15:22:46 +0000 | [diff] [blame] | 464 | kref_init(&rsv->kref); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 465 | init_timer(&rsv->timer); |
| 466 | rsv->timer.function = uwb_rsv_timer; |
| 467 | rsv->timer.data = (unsigned long)rsv; |
| 468 | |
| 469 | rsv->rc = rc; |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 470 | INIT_WORK(&rsv->handle_timeout_work, uwb_rsv_handle_timeout_work); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 471 | |
| 472 | return rsv; |
| 473 | } |
| 474 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 475 | /** |
| 476 | * uwb_rsv_create - allocate and initialize a UWB reservation structure |
| 477 | * @rc: the radio controller |
| 478 | * @cb: callback to use when the reservation completes or terminates |
| 479 | * @pal_priv: data private to the PAL to be passed in the callback |
| 480 | * |
| 481 | * The callback is called when the state of the reservation changes from: |
| 482 | * |
| 483 | * - pending to accepted |
| 484 | * - pending to denined |
| 485 | * - accepted to terminated |
| 486 | * - pending to terminated |
| 487 | */ |
| 488 | struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb, void *pal_priv) |
| 489 | { |
| 490 | struct uwb_rsv *rsv; |
| 491 | |
| 492 | rsv = uwb_rsv_alloc(rc); |
| 493 | if (!rsv) |
| 494 | return NULL; |
| 495 | |
| 496 | rsv->callback = cb; |
| 497 | rsv->pal_priv = pal_priv; |
| 498 | |
| 499 | return rsv; |
| 500 | } |
| 501 | EXPORT_SYMBOL_GPL(uwb_rsv_create); |
| 502 | |
| 503 | void uwb_rsv_remove(struct uwb_rsv *rsv) |
| 504 | { |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 505 | uwb_rsv_dump("RM", rsv); |
| 506 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 507 | if (rsv->state != UWB_RSV_STATE_NONE) |
| 508 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE); |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 509 | |
| 510 | if (rsv->needs_release_companion_mas) |
| 511 | uwb_drp_avail_release(rsv->rc, &rsv->mv.companion_mas); |
| 512 | uwb_drp_avail_release(rsv->rc, &rsv->mas); |
| 513 | |
| 514 | if (uwb_rsv_is_owner(rsv)) |
| 515 | uwb_rsv_put_stream(rsv); |
David Vrabel | 2226b1c | 2009-01-06 17:55:32 +0000 | [diff] [blame] | 516 | |
David Vrabel | cae1c11 | 2008-10-27 15:22:46 +0000 | [diff] [blame] | 517 | uwb_dev_put(rsv->owner); |
| 518 | if (rsv->target.type == UWB_RSV_TARGET_DEV) |
| 519 | uwb_dev_put(rsv->target.dev); |
| 520 | |
| 521 | list_del_init(&rsv->rc_node); |
| 522 | uwb_rsv_put(rsv); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /** |
| 526 | * uwb_rsv_destroy - free a UWB reservation structure |
| 527 | * @rsv: the reservation to free |
| 528 | * |
David Vrabel | cae1c11 | 2008-10-27 15:22:46 +0000 | [diff] [blame] | 529 | * The reservation must already be terminated. |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 530 | */ |
| 531 | void uwb_rsv_destroy(struct uwb_rsv *rsv) |
| 532 | { |
David Vrabel | cae1c11 | 2008-10-27 15:22:46 +0000 | [diff] [blame] | 533 | uwb_rsv_put(rsv); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 534 | } |
| 535 | EXPORT_SYMBOL_GPL(uwb_rsv_destroy); |
| 536 | |
| 537 | /** |
| 538 | * usb_rsv_establish - start a reservation establishment |
| 539 | * @rsv: the reservation |
| 540 | * |
| 541 | * The PAL should fill in @rsv's owner, target, type, max_mas, |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 542 | * min_mas, max_interval and is_multicast fields. If the target is a |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 543 | * uwb_dev it must be referenced. |
| 544 | * |
| 545 | * The reservation's callback will be called when the reservation is |
| 546 | * accepted, denied or times out. |
| 547 | */ |
| 548 | int uwb_rsv_establish(struct uwb_rsv *rsv) |
| 549 | { |
| 550 | struct uwb_rc *rc = rsv->rc; |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 551 | struct uwb_mas_bm available; |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 552 | int ret; |
| 553 | |
| 554 | mutex_lock(&rc->rsvs_mutex); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 555 | ret = uwb_rsv_get_stream(rsv); |
| 556 | if (ret) |
| 557 | goto out; |
| 558 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 559 | rsv->tiebreaker = random32() & 1; |
| 560 | /* get available mas bitmap */ |
| 561 | uwb_drp_available(rc, &available); |
| 562 | |
| 563 | ret = uwb_rsv_find_best_allocation(rsv, &available, &rsv->mas); |
| 564 | if (ret == UWB_RSV_ALLOC_NOT_FOUND) { |
| 565 | ret = -EBUSY; |
| 566 | uwb_rsv_put_stream(rsv); |
| 567 | goto out; |
| 568 | } |
| 569 | |
| 570 | ret = uwb_drp_avail_reserve_pending(rc, &rsv->mas); |
| 571 | if (ret != 0) { |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 572 | uwb_rsv_put_stream(rsv); |
| 573 | goto out; |
| 574 | } |
| 575 | |
David Vrabel | cae1c11 | 2008-10-27 15:22:46 +0000 | [diff] [blame] | 576 | uwb_rsv_get(rsv); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 577 | list_add_tail(&rsv->rc_node, &rc->reservations); |
| 578 | rsv->owner = &rc->uwb_dev; |
| 579 | uwb_dev_get(rsv->owner); |
| 580 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_INITIATED); |
| 581 | out: |
| 582 | mutex_unlock(&rc->rsvs_mutex); |
| 583 | return ret; |
| 584 | } |
| 585 | EXPORT_SYMBOL_GPL(uwb_rsv_establish); |
| 586 | |
| 587 | /** |
| 588 | * uwb_rsv_modify - modify an already established reservation |
| 589 | * @rsv: the reservation to modify |
| 590 | * @max_mas: new maximum MAS to reserve |
| 591 | * @min_mas: new minimum MAS to reserve |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 592 | * @max_interval: new max_interval to use |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 593 | * |
| 594 | * FIXME: implement this once there are PALs that use it. |
| 595 | */ |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 596 | int uwb_rsv_modify(struct uwb_rsv *rsv, int max_mas, int min_mas, int max_interval) |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 597 | { |
| 598 | return -ENOSYS; |
| 599 | } |
| 600 | EXPORT_SYMBOL_GPL(uwb_rsv_modify); |
| 601 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 602 | /* |
| 603 | * move an already established reservation (rc->rsvs_mutex must to be |
| 604 | * taken when tis function is called) |
| 605 | */ |
| 606 | int uwb_rsv_try_move(struct uwb_rsv *rsv, struct uwb_mas_bm *available) |
| 607 | { |
| 608 | struct uwb_rc *rc = rsv->rc; |
| 609 | struct uwb_drp_backoff_win *bow = &rc->bow; |
| 610 | struct device *dev = &rc->uwb_dev.dev; |
| 611 | struct uwb_rsv_move *mv; |
| 612 | int ret = 0; |
| 613 | |
| 614 | if (bow->can_reserve_extra_mases == false) |
| 615 | return -EBUSY; |
| 616 | |
| 617 | mv = &rsv->mv; |
| 618 | |
| 619 | if (uwb_rsv_find_best_allocation(rsv, available, &mv->final_mas) == UWB_RSV_ALLOC_FOUND) { |
| 620 | |
| 621 | if (!bitmap_equal(rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS)) { |
| 622 | /* We want to move the reservation */ |
| 623 | bitmap_andnot(mv->companion_mas.bm, mv->final_mas.bm, rsv->mas.bm, UWB_NUM_MAS); |
| 624 | uwb_drp_avail_reserve_pending(rc, &mv->companion_mas); |
| 625 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_EXPANDING); |
| 626 | } |
| 627 | } else { |
| 628 | dev_dbg(dev, "new allocation not found\n"); |
| 629 | } |
| 630 | |
| 631 | return ret; |
| 632 | } |
| 633 | |
| 634 | /* It will try to move every reservation in state O_ESTABLISHED giving |
| 635 | * to the MAS allocator algorithm an availability that is the real one |
| 636 | * plus the allocation already established from the reservation. */ |
| 637 | void uwb_rsv_handle_drp_avail_change(struct uwb_rc *rc) |
| 638 | { |
| 639 | struct uwb_drp_backoff_win *bow = &rc->bow; |
| 640 | struct uwb_rsv *rsv; |
| 641 | struct uwb_mas_bm mas; |
| 642 | |
| 643 | if (bow->can_reserve_extra_mases == false) |
| 644 | return; |
| 645 | |
| 646 | list_for_each_entry(rsv, &rc->reservations, rc_node) { |
| 647 | if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED || |
| 648 | rsv->state == UWB_RSV_STATE_O_TO_BE_MOVED) { |
| 649 | uwb_drp_available(rc, &mas); |
| 650 | bitmap_or(mas.bm, mas.bm, rsv->mas.bm, UWB_NUM_MAS); |
| 651 | uwb_rsv_try_move(rsv, &mas); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | } |
| 656 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 657 | /** |
| 658 | * uwb_rsv_terminate - terminate an established reservation |
| 659 | * @rsv: the reservation to terminate |
| 660 | * |
| 661 | * A reservation is terminated by removing the DRP IE from the beacon, |
| 662 | * the other end will consider the reservation to be terminated when |
| 663 | * it does not see the DRP IE for at least mMaxLostBeacons. |
| 664 | * |
| 665 | * If applicable, the reference to the target uwb_dev will be released. |
| 666 | */ |
| 667 | void uwb_rsv_terminate(struct uwb_rsv *rsv) |
| 668 | { |
| 669 | struct uwb_rc *rc = rsv->rc; |
| 670 | |
| 671 | mutex_lock(&rc->rsvs_mutex); |
| 672 | |
David Vrabel | 671e470 | 2008-12-12 13:23:24 +0000 | [diff] [blame] | 673 | if (rsv->state != UWB_RSV_STATE_NONE) |
| 674 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 675 | |
| 676 | mutex_unlock(&rc->rsvs_mutex); |
| 677 | } |
| 678 | EXPORT_SYMBOL_GPL(uwb_rsv_terminate); |
| 679 | |
| 680 | /** |
| 681 | * uwb_rsv_accept - accept a new reservation from a peer |
| 682 | * @rsv: the reservation |
| 683 | * @cb: call back for reservation changes |
| 684 | * @pal_priv: data to be passed in the above call back |
| 685 | * |
| 686 | * Reservation requests from peers are denied unless a PAL accepts it |
| 687 | * by calling this function. |
David Vrabel | cae1c11 | 2008-10-27 15:22:46 +0000 | [diff] [blame] | 688 | * |
| 689 | * The PAL call uwb_rsv_destroy() for all accepted reservations before |
| 690 | * calling uwb_pal_unregister(). |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 691 | */ |
| 692 | void uwb_rsv_accept(struct uwb_rsv *rsv, uwb_rsv_cb_f cb, void *pal_priv) |
| 693 | { |
David Vrabel | cae1c11 | 2008-10-27 15:22:46 +0000 | [diff] [blame] | 694 | uwb_rsv_get(rsv); |
| 695 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 696 | rsv->callback = cb; |
| 697 | rsv->pal_priv = pal_priv; |
| 698 | rsv->state = UWB_RSV_STATE_T_ACCEPTED; |
| 699 | } |
| 700 | EXPORT_SYMBOL_GPL(uwb_rsv_accept); |
| 701 | |
| 702 | /* |
| 703 | * Is a received DRP IE for this reservation? |
| 704 | */ |
| 705 | static bool uwb_rsv_match(struct uwb_rsv *rsv, struct uwb_dev *src, |
| 706 | struct uwb_ie_drp *drp_ie) |
| 707 | { |
| 708 | struct uwb_dev_addr *rsv_src; |
| 709 | int stream; |
| 710 | |
| 711 | stream = uwb_ie_drp_stream_index(drp_ie); |
| 712 | |
| 713 | if (rsv->stream != stream) |
| 714 | return false; |
| 715 | |
| 716 | switch (rsv->target.type) { |
| 717 | case UWB_RSV_TARGET_DEVADDR: |
| 718 | return rsv->stream == stream; |
| 719 | case UWB_RSV_TARGET_DEV: |
| 720 | if (uwb_ie_drp_owner(drp_ie)) |
| 721 | rsv_src = &rsv->owner->dev_addr; |
| 722 | else |
| 723 | rsv_src = &rsv->target.dev->dev_addr; |
| 724 | return uwb_dev_addr_cmp(&src->dev_addr, rsv_src) == 0; |
| 725 | } |
| 726 | return false; |
| 727 | } |
| 728 | |
| 729 | static struct uwb_rsv *uwb_rsv_new_target(struct uwb_rc *rc, |
| 730 | struct uwb_dev *src, |
| 731 | struct uwb_ie_drp *drp_ie) |
| 732 | { |
| 733 | struct uwb_rsv *rsv; |
| 734 | struct uwb_pal *pal; |
| 735 | enum uwb_rsv_state state; |
| 736 | |
| 737 | rsv = uwb_rsv_alloc(rc); |
| 738 | if (!rsv) |
| 739 | return NULL; |
| 740 | |
| 741 | rsv->rc = rc; |
| 742 | rsv->owner = src; |
| 743 | uwb_dev_get(rsv->owner); |
| 744 | rsv->target.type = UWB_RSV_TARGET_DEV; |
| 745 | rsv->target.dev = &rc->uwb_dev; |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 746 | uwb_dev_get(&rc->uwb_dev); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 747 | rsv->type = uwb_ie_drp_type(drp_ie); |
| 748 | rsv->stream = uwb_ie_drp_stream_index(drp_ie); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 749 | uwb_drp_ie_to_bm(&rsv->mas, drp_ie); |
| 750 | |
| 751 | /* |
| 752 | * See if any PALs are interested in this reservation. If not, |
| 753 | * deny the request. |
| 754 | */ |
| 755 | rsv->state = UWB_RSV_STATE_T_DENIED; |
David Vrabel | 6fae35f | 2008-11-17 15:53:42 +0000 | [diff] [blame] | 756 | mutex_lock(&rc->uwb_dev.mutex); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 757 | list_for_each_entry(pal, &rc->pals, node) { |
| 758 | if (pal->new_rsv) |
David Vrabel | e17be2b | 2008-11-17 15:24:14 +0000 | [diff] [blame] | 759 | pal->new_rsv(pal, rsv); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 760 | if (rsv->state == UWB_RSV_STATE_T_ACCEPTED) |
| 761 | break; |
| 762 | } |
David Vrabel | 6fae35f | 2008-11-17 15:53:42 +0000 | [diff] [blame] | 763 | mutex_unlock(&rc->uwb_dev.mutex); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 764 | |
| 765 | list_add_tail(&rsv->rc_node, &rc->reservations); |
| 766 | state = rsv->state; |
| 767 | rsv->state = UWB_RSV_STATE_NONE; |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 768 | |
| 769 | /* FIXME: do something sensible here */ |
| 770 | if (state == UWB_RSV_STATE_T_ACCEPTED |
| 771 | && uwb_drp_avail_reserve_pending(rc, &rsv->mas) == -EBUSY) { |
| 772 | /* FIXME: do something sensible here */ |
| 773 | } else { |
| 774 | uwb_rsv_set_state(rsv, state); |
| 775 | } |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 776 | |
| 777 | return rsv; |
| 778 | } |
| 779 | |
| 780 | /** |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 781 | * uwb_rsv_get_usable_mas - get the bitmap of the usable MAS of a reservations |
| 782 | * @rsv: the reservation. |
| 783 | * @mas: returns the available MAS. |
| 784 | * |
| 785 | * The usable MAS of a reservation may be less than the negotiated MAS |
| 786 | * if alien BPs are present. |
| 787 | */ |
| 788 | void uwb_rsv_get_usable_mas(struct uwb_rsv *rsv, struct uwb_mas_bm *mas) |
| 789 | { |
| 790 | bitmap_zero(mas->bm, UWB_NUM_MAS); |
| 791 | bitmap_andnot(mas->bm, rsv->mas.bm, rsv->rc->cnflt_alien_bitmap.bm, UWB_NUM_MAS); |
| 792 | } |
| 793 | EXPORT_SYMBOL_GPL(uwb_rsv_get_usable_mas); |
| 794 | |
| 795 | /** |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 796 | * uwb_rsv_find - find a reservation for a received DRP IE. |
| 797 | * @rc: the radio controller |
| 798 | * @src: source of the DRP IE |
| 799 | * @drp_ie: the DRP IE |
| 800 | * |
| 801 | * If the reservation cannot be found and the DRP IE is from a peer |
| 802 | * attempting to establish a new reservation, create a new reservation |
| 803 | * and add it to the list. |
| 804 | */ |
| 805 | struct uwb_rsv *uwb_rsv_find(struct uwb_rc *rc, struct uwb_dev *src, |
| 806 | struct uwb_ie_drp *drp_ie) |
| 807 | { |
| 808 | struct uwb_rsv *rsv; |
| 809 | |
| 810 | list_for_each_entry(rsv, &rc->reservations, rc_node) { |
| 811 | if (uwb_rsv_match(rsv, src, drp_ie)) |
| 812 | return rsv; |
| 813 | } |
| 814 | |
| 815 | if (uwb_ie_drp_owner(drp_ie)) |
| 816 | return uwb_rsv_new_target(rc, src, drp_ie); |
| 817 | |
| 818 | return NULL; |
| 819 | } |
| 820 | |
| 821 | /* |
| 822 | * Go through all the reservations and check for timeouts and (if |
| 823 | * necessary) update their DRP IEs. |
| 824 | * |
| 825 | * FIXME: look at building the SET_DRP_IE command here rather than |
| 826 | * having to rescan the list in uwb_rc_send_all_drp_ie(). |
| 827 | */ |
| 828 | static bool uwb_rsv_update_all(struct uwb_rc *rc) |
| 829 | { |
| 830 | struct uwb_rsv *rsv, *t; |
| 831 | bool ie_updated = false; |
| 832 | |
| 833 | list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) { |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 834 | if (!rsv->ie_valid) { |
| 835 | uwb_drp_ie_update(rsv); |
| 836 | ie_updated = true; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | return ie_updated; |
| 841 | } |
| 842 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 843 | void uwb_rsv_queue_update(struct uwb_rc *rc) |
| 844 | { |
| 845 | unsigned long delay_us = UWB_MAS_LENGTH_US * UWB_MAS_PER_ZONE; |
| 846 | |
| 847 | queue_delayed_work(rc->rsv_workq, &rc->rsv_update_work, usecs_to_jiffies(delay_us)); |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * uwb_rsv_sched_update - schedule an update of the DRP IEs |
| 852 | * @rc: the radio controller. |
| 853 | * |
| 854 | * To improve performance and ensure correctness with [ECMA-368] the |
| 855 | * number of SET-DRP-IE commands that are done are limited. |
| 856 | * |
| 857 | * DRP IEs update come from two sources: DRP events from the hardware |
| 858 | * which all occur at the beginning of the superframe ('syncronous' |
| 859 | * events) and reservation establishment/termination requests from |
| 860 | * PALs or timers ('asynchronous' events). |
| 861 | * |
| 862 | * A delayed work ensures that all the synchronous events result in |
| 863 | * one SET-DRP-IE command. |
| 864 | * |
| 865 | * Additional logic (the set_drp_ie_pending and rsv_updated_postponed |
| 866 | * flags) will prevent an asynchrous event starting a SET-DRP-IE |
| 867 | * command if one is currently awaiting a response. |
| 868 | * |
| 869 | * FIXME: this does leave a window where an asynchrous event can delay |
| 870 | * the SET-DRP-IE for a synchronous event by one superframe. |
| 871 | */ |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 872 | void uwb_rsv_sched_update(struct uwb_rc *rc) |
| 873 | { |
David Vrabel | 8f5140a | 2009-01-23 12:57:20 +0000 | [diff] [blame] | 874 | spin_lock_bh(&rc->rsvs_lock); |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 875 | if (!delayed_work_pending(&rc->rsv_update_work)) { |
| 876 | if (rc->set_drp_ie_pending > 0) { |
| 877 | rc->set_drp_ie_pending++; |
| 878 | goto unlock; |
| 879 | } |
| 880 | uwb_rsv_queue_update(rc); |
| 881 | } |
| 882 | unlock: |
David Vrabel | 8f5140a | 2009-01-23 12:57:20 +0000 | [diff] [blame] | 883 | spin_unlock_bh(&rc->rsvs_lock); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /* |
| 887 | * Update DRP IEs and, if necessary, the DRP Availability IE and send |
| 888 | * the updated IEs to the radio controller. |
| 889 | */ |
| 890 | static void uwb_rsv_update_work(struct work_struct *work) |
| 891 | { |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 892 | struct uwb_rc *rc = container_of(work, struct uwb_rc, |
| 893 | rsv_update_work.work); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 894 | bool ie_updated; |
| 895 | |
| 896 | mutex_lock(&rc->rsvs_mutex); |
| 897 | |
| 898 | ie_updated = uwb_rsv_update_all(rc); |
| 899 | |
| 900 | if (!rc->drp_avail.ie_valid) { |
| 901 | uwb_drp_avail_ie_update(rc); |
| 902 | ie_updated = true; |
| 903 | } |
| 904 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 905 | if (ie_updated && (rc->set_drp_ie_pending == 0)) |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 906 | uwb_rc_send_all_drp_ie(rc); |
| 907 | |
| 908 | mutex_unlock(&rc->rsvs_mutex); |
| 909 | } |
| 910 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 911 | static void uwb_rsv_alien_bp_work(struct work_struct *work) |
| 912 | { |
| 913 | struct uwb_rc *rc = container_of(work, struct uwb_rc, |
| 914 | rsv_alien_bp_work.work); |
| 915 | struct uwb_rsv *rsv; |
| 916 | |
| 917 | mutex_lock(&rc->rsvs_mutex); |
| 918 | |
| 919 | list_for_each_entry(rsv, &rc->reservations, rc_node) { |
| 920 | if (rsv->type != UWB_DRP_TYPE_ALIEN_BP) { |
| 921 | rsv->callback(rsv); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | mutex_unlock(&rc->rsvs_mutex); |
| 926 | } |
| 927 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 928 | static void uwb_rsv_timer(unsigned long arg) |
| 929 | { |
| 930 | struct uwb_rsv *rsv = (struct uwb_rsv *)arg; |
| 931 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 932 | queue_work(rsv->rc->rsv_workq, &rsv->handle_timeout_work); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 933 | } |
| 934 | |
David Vrabel | 307ba6d | 2008-11-07 17:37:33 +0000 | [diff] [blame] | 935 | /** |
| 936 | * uwb_rsv_remove_all - remove all reservations |
| 937 | * @rc: the radio controller |
| 938 | * |
| 939 | * A DRP IE update is not done. |
| 940 | */ |
| 941 | void uwb_rsv_remove_all(struct uwb_rc *rc) |
| 942 | { |
| 943 | struct uwb_rsv *rsv, *t; |
| 944 | |
| 945 | mutex_lock(&rc->rsvs_mutex); |
| 946 | list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) { |
David Vrabel | 2226b1c | 2009-01-06 17:55:32 +0000 | [diff] [blame] | 947 | if (rsv->state != UWB_RSV_STATE_NONE) |
| 948 | uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE); |
| 949 | del_timer_sync(&rsv->timer); |
David Vrabel | 307ba6d | 2008-11-07 17:37:33 +0000 | [diff] [blame] | 950 | } |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 951 | /* Cancel any postponed update. */ |
| 952 | rc->set_drp_ie_pending = 0; |
David Vrabel | 307ba6d | 2008-11-07 17:37:33 +0000 | [diff] [blame] | 953 | mutex_unlock(&rc->rsvs_mutex); |
| 954 | |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 955 | cancel_delayed_work_sync(&rc->rsv_update_work); |
David Vrabel | 2226b1c | 2009-01-06 17:55:32 +0000 | [diff] [blame] | 956 | flush_workqueue(rc->rsv_workq); |
| 957 | |
| 958 | mutex_lock(&rc->rsvs_mutex); |
| 959 | list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) { |
| 960 | uwb_rsv_remove(rsv); |
| 961 | } |
| 962 | mutex_unlock(&rc->rsvs_mutex); |
David Vrabel | 307ba6d | 2008-11-07 17:37:33 +0000 | [diff] [blame] | 963 | } |
| 964 | |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 965 | void uwb_rsv_init(struct uwb_rc *rc) |
| 966 | { |
| 967 | INIT_LIST_HEAD(&rc->reservations); |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 968 | INIT_LIST_HEAD(&rc->cnflt_alien_list); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 969 | mutex_init(&rc->rsvs_mutex); |
Stefano Panella | 5b37717 | 2008-12-12 13:00:06 +0000 | [diff] [blame] | 970 | spin_lock_init(&rc->rsvs_lock); |
| 971 | INIT_DELAYED_WORK(&rc->rsv_update_work, uwb_rsv_update_work); |
| 972 | INIT_DELAYED_WORK(&rc->rsv_alien_bp_work, uwb_rsv_alien_bp_work); |
| 973 | rc->bow.can_reserve_extra_mases = true; |
| 974 | rc->bow.total_expired = 0; |
| 975 | rc->bow.window = UWB_DRP_BACKOFF_WIN_MIN >> 1; |
| 976 | init_timer(&rc->bow.timer); |
| 977 | rc->bow.timer.function = uwb_rsv_backoff_win_timer; |
| 978 | rc->bow.timer.data = (unsigned long)&rc->bow; |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 979 | |
| 980 | bitmap_complement(rc->uwb_dev.streams, rc->uwb_dev.streams, UWB_NUM_STREAMS); |
| 981 | } |
| 982 | |
| 983 | int uwb_rsv_setup(struct uwb_rc *rc) |
| 984 | { |
| 985 | char name[16]; |
| 986 | |
| 987 | snprintf(name, sizeof(name), "%s_rsvd", dev_name(&rc->uwb_dev.dev)); |
| 988 | rc->rsv_workq = create_singlethread_workqueue(name); |
| 989 | if (rc->rsv_workq == NULL) |
| 990 | return -ENOMEM; |
| 991 | |
| 992 | return 0; |
| 993 | } |
| 994 | |
| 995 | void uwb_rsv_cleanup(struct uwb_rc *rc) |
| 996 | { |
David Vrabel | 307ba6d | 2008-11-07 17:37:33 +0000 | [diff] [blame] | 997 | uwb_rsv_remove_all(rc); |
David Vrabel | 8cc13a0 | 2008-09-17 16:34:09 +0100 | [diff] [blame] | 998 | destroy_workqueue(rc->rsv_workq); |
| 999 | } |