Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* Paging helper and manager.... */ |
| 2 | /* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 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 along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Relevant specs: |
| 23 | * 12.21: |
| 24 | * - 9.4.12 for CCCH Local Threshold |
| 25 | * |
| 26 | * 05.58: |
| 27 | * - 8.5.2 CCCH Load indication |
| 28 | * - 9.3.15 Paging Load |
| 29 | * |
| 30 | * Approach: |
| 31 | * - Send paging command to subscriber |
| 32 | * - On Channel Request we will remember the reason |
| 33 | * - After the ACK we will request the identity |
| 34 | * - Then we will send assign the gsm_subscriber and |
| 35 | * - and call a callback |
| 36 | */ |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <assert.h> |
| 41 | |
| 42 | #include <openbsc/paging.h> |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 43 | #include <openbsc/talloc.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 44 | #include <openbsc/debug.h> |
| 45 | #include <openbsc/signal.h> |
| 46 | #include <openbsc/abis_rsl.h> |
| 47 | #include <openbsc/gsm_data.h> |
| 48 | |
Harald Welte (local) | 8751ee9 | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 49 | void *tall_paging_ctx; |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 50 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 51 | static unsigned int calculate_group(struct gsm_bts *bts, struct gsm_subscriber *subscr) |
| 52 | { |
| 53 | int ccch_conf; |
| 54 | int bs_cc_chans; |
| 55 | int blocks; |
| 56 | unsigned int group; |
| 57 | |
| 58 | ccch_conf = bts->chan_desc.ccch_conf; |
| 59 | bs_cc_chans = rsl_ccch_conf_to_bs_cc_chans(ccch_conf); |
| 60 | /* code word + 2, as 2 channels equals 0x0 */ |
| 61 | blocks = rsl_number_of_paging_subchannels(bts); |
| 62 | group = get_paging_group(str_to_imsi(subscr->imsi), |
| 63 | bs_cc_chans, blocks); |
| 64 | return group; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Kill one paging request update the internal list... |
| 69 | */ |
| 70 | static void paging_remove_request(struct gsm_bts_paging_state *paging_bts, |
| 71 | struct gsm_paging_request *to_be_deleted) |
| 72 | { |
| 73 | /* Update the last_request if that is necessary */ |
| 74 | if (to_be_deleted == paging_bts->last_request) { |
| 75 | paging_bts->last_request = |
| 76 | (struct gsm_paging_request *)paging_bts->last_request->entry.next; |
| 77 | if (&to_be_deleted->entry == &paging_bts->pending_requests) |
| 78 | paging_bts->last_request = NULL; |
| 79 | } |
| 80 | |
| 81 | bsc_del_timer(&to_be_deleted->T3113); |
| 82 | llist_del(&to_be_deleted->entry); |
| 83 | subscr_put(to_be_deleted->subscr); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 84 | talloc_free(to_be_deleted); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static void page_ms(struct gsm_paging_request *request) |
| 88 | { |
| 89 | u_int8_t mi[128]; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 90 | unsigned int mi_len; |
| 91 | unsigned int page_group; |
| 92 | |
Holger Hans Peter Freyther | 9b7911e | 2009-08-21 14:55:57 +0200 | [diff] [blame] | 93 | DEBUGP(DPAG, "Going to send paging commands: imsi: '%s' tmsi: '0x%x'\n", |
| 94 | request->subscr->imsi, request->subscr->tmsi); |
| 95 | |
| 96 | if (request->subscr->tmsi == GSM_RESERVED_TMSI) |
| 97 | mi_len = gsm48_generate_mid_from_imsi(mi, request->subscr->imsi); |
| 98 | else |
| 99 | mi_len = gsm48_generate_mid_from_tmsi(mi, request->subscr->tmsi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 100 | |
| 101 | page_group = calculate_group(request->bts, request->subscr); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 102 | rsl_paging_cmd(request->bts, page_group, mi_len, mi, |
| 103 | request->chan_type); |
| 104 | } |
| 105 | |
| 106 | static void paging_move_to_next(struct gsm_bts_paging_state *paging_bts) |
| 107 | { |
| 108 | paging_bts->last_request = |
| 109 | (struct gsm_paging_request *)paging_bts->last_request->entry.next; |
| 110 | if (&paging_bts->last_request->entry == &paging_bts->pending_requests) |
| 111 | paging_bts->last_request = NULL; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * This is kicked by the periodic PAGING LOAD Indicator |
| 116 | * coming from abis_rsl.c |
| 117 | * |
| 118 | * We attempt to iterate once over the list of items but |
| 119 | * only upto available_slots. |
| 120 | */ |
| 121 | static void paging_handle_pending_requests(struct gsm_bts_paging_state *paging_bts) |
| 122 | { |
| 123 | struct gsm_paging_request *initial_request = NULL; |
| 124 | struct gsm_paging_request *current_request = NULL; |
| 125 | |
| 126 | /* |
| 127 | * Determine if the pending_requests list is empty and |
| 128 | * return then. |
| 129 | */ |
| 130 | if (llist_empty(&paging_bts->pending_requests)) { |
| 131 | paging_bts->last_request = NULL; |
| 132 | /* since the list is empty, no need to reschedule the timer */ |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | if (!paging_bts->last_request) |
| 137 | paging_bts->last_request = |
| 138 | (struct gsm_paging_request *)paging_bts->pending_requests.next; |
| 139 | |
| 140 | assert(paging_bts->last_request); |
| 141 | initial_request = paging_bts->last_request; |
| 142 | current_request = initial_request; |
| 143 | |
| 144 | do { |
| 145 | /* handle the paging request now */ |
| 146 | page_ms(current_request); |
| 147 | paging_bts->available_slots--; |
| 148 | |
| 149 | /* |
| 150 | * move to the next item. We might wrap around |
| 151 | * this means last_request will be NULL and we just |
| 152 | * call paging_page_to_next again. It it guranteed |
| 153 | * that the list is not empty. |
| 154 | */ |
| 155 | paging_move_to_next(paging_bts); |
| 156 | if (!paging_bts->last_request) |
| 157 | paging_bts->last_request = |
| 158 | (struct gsm_paging_request *)paging_bts->pending_requests.next; |
| 159 | current_request = paging_bts->last_request; |
| 160 | } while (paging_bts->available_slots > 0 |
| 161 | && initial_request != current_request); |
| 162 | |
| 163 | bsc_schedule_timer(&paging_bts->work_timer, 1, 0); |
| 164 | } |
| 165 | |
| 166 | static void paging_worker(void *data) |
| 167 | { |
| 168 | struct gsm_bts_paging_state *paging_bts = data; |
| 169 | |
| 170 | paging_handle_pending_requests(paging_bts); |
| 171 | } |
| 172 | |
| 173 | void paging_init(struct gsm_bts *bts) |
| 174 | { |
| 175 | bts->paging.bts = bts; |
| 176 | INIT_LLIST_HEAD(&bts->paging.pending_requests); |
| 177 | bts->paging.work_timer.cb = paging_worker; |
| 178 | bts->paging.work_timer.data = &bts->paging; |
| 179 | |
| 180 | /* Large number, until we get a proper message */ |
| 181 | bts->paging.available_slots = 100; |
| 182 | } |
| 183 | |
| 184 | static int paging_pending_request(struct gsm_bts_paging_state *bts, |
| 185 | struct gsm_subscriber *subscr) { |
| 186 | struct gsm_paging_request *req; |
| 187 | |
| 188 | llist_for_each_entry(req, &bts->pending_requests, entry) { |
| 189 | if (subscr == req->subscr) |
| 190 | return 1; |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static void paging_T3113_expired(void *data) |
| 197 | { |
| 198 | struct gsm_paging_request *req = (struct gsm_paging_request *)data; |
| 199 | struct paging_signal_data sig_data; |
| 200 | |
| 201 | DEBUGP(DPAG, "T3113 expired for request %p (%s)\n", |
| 202 | req, req->subscr->imsi); |
| 203 | |
Holger Hans Peter Freyther | a3a1666 | 2009-08-20 06:15:21 +0200 | [diff] [blame] | 204 | sig_data.subscr = req->subscr; |
| 205 | sig_data.bts = req->bts; |
| 206 | sig_data.lchan = NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 207 | |
| 208 | dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data); |
| 209 | if (req->cbfn) |
| 210 | req->cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED, NULL, NULL, |
| 211 | req->cbfn_param); |
| 212 | paging_remove_request(&req->bts->paging, req); |
| 213 | } |
| 214 | |
Harald Welte | d363c95 | 2009-08-15 03:16:17 +0200 | [diff] [blame] | 215 | static int _paging_request(struct gsm_bts *bts, struct gsm_subscriber *subscr, |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 216 | int type, gsm_cbfn *cbfn, void *data) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 217 | { |
| 218 | struct gsm_bts_paging_state *bts_entry = &bts->paging; |
| 219 | struct gsm_paging_request *req; |
| 220 | |
| 221 | if (paging_pending_request(bts_entry, subscr)) { |
| 222 | DEBUGP(DPAG, "Paging request already pending\n"); |
Harald Welte | d363c95 | 2009-08-15 03:16:17 +0200 | [diff] [blame] | 223 | return -EEXIST; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 224 | } |
| 225 | |
Harald Welte | cd97c21 | 2009-08-08 15:08:19 +0200 | [diff] [blame] | 226 | DEBUGP(DPAG, "Start paging of subscriber %llu on bts %d.\n", |
| 227 | subscr->id, bts->nr); |
Harald Welte | 857e00d | 2009-06-26 20:25:23 +0200 | [diff] [blame] | 228 | req = talloc_zero(tall_paging_ctx, struct gsm_paging_request); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 229 | req->subscr = subscr_get(subscr); |
| 230 | req->bts = bts; |
| 231 | req->chan_type = type; |
| 232 | req->cbfn = cbfn; |
| 233 | req->cbfn_param = data; |
| 234 | req->T3113.cb = paging_T3113_expired; |
| 235 | req->T3113.data = req; |
| 236 | bsc_schedule_timer(&req->T3113, T3113_VALUE); |
| 237 | llist_add_tail(&req->entry, &bts_entry->pending_requests); |
| 238 | |
| 239 | if (!bsc_timer_pending(&bts_entry->work_timer)) |
| 240 | bsc_schedule_timer(&bts_entry->work_timer, 1, 0); |
Harald Welte | d363c95 | 2009-08-15 03:16:17 +0200 | [diff] [blame] | 241 | |
| 242 | return 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 243 | } |
| 244 | |
Harald Welte | d363c95 | 2009-08-15 03:16:17 +0200 | [diff] [blame] | 245 | int paging_request(struct gsm_network *network, struct gsm_subscriber *subscr, |
| 246 | int type, gsm_cbfn *cbfn, void *data) |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 247 | { |
| 248 | struct gsm_bts *bts = NULL; |
Harald Welte (local) | dec08ee | 2009-08-15 11:25:45 +0200 | [diff] [blame] | 249 | int num_pages = 0; |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 250 | |
Harald Welte | 2a3a81b | 2009-08-01 19:31:47 +0200 | [diff] [blame] | 251 | /* start paging subscriber on all BTS within Location Area */ |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 252 | do { |
Harald Welte (local) | dec08ee | 2009-08-15 11:25:45 +0200 | [diff] [blame] | 253 | int rc; |
| 254 | |
Holger Freyther | 93795ac | 2009-03-31 04:35:19 +0200 | [diff] [blame] | 255 | bts = gsm_bts_by_lac(network, subscr->lac, bts); |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 256 | if (!bts) |
| 257 | break; |
Harald Welte (local) | dec08ee | 2009-08-15 11:25:45 +0200 | [diff] [blame] | 258 | num_pages++; |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 259 | |
Harald Welte | d363c95 | 2009-08-15 03:16:17 +0200 | [diff] [blame] | 260 | /* Trigger paging, pass any error to caller */ |
| 261 | rc = _paging_request(bts, subscr, type, cbfn, data); |
| 262 | if (rc < 0) |
| 263 | return rc; |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 264 | } while (1); |
Harald Welte | d363c95 | 2009-08-15 03:16:17 +0200 | [diff] [blame] | 265 | |
Harald Welte (local) | dec08ee | 2009-08-15 11:25:45 +0200 | [diff] [blame] | 266 | return num_pages; |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 270 | /* we consciously ignore the type of the request here */ |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 271 | static void _paging_request_stop(struct gsm_bts *bts, struct gsm_subscriber *subscr, |
| 272 | struct gsm_lchan *lchan) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 273 | { |
| 274 | struct gsm_bts_paging_state *bts_entry = &bts->paging; |
| 275 | struct gsm_paging_request *req, *req2; |
| 276 | |
| 277 | llist_for_each_entry_safe(req, req2, &bts_entry->pending_requests, |
| 278 | entry) { |
| 279 | if (req->subscr == subscr) { |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 280 | if (lchan && req->cbfn) { |
| 281 | DEBUGP(DPAG, "Stop paging on bts %d, calling cbfn.\n", bts->nr); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 282 | req->cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED, |
| 283 | NULL, lchan, req->cbfn_param); |
Harald Welte | 0374084 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 284 | } else |
| 285 | DEBUGP(DPAG, "Stop paging on bts %d silently.\n", bts->nr); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 286 | paging_remove_request(&bts->paging, req); |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 292 | /* Stop paging on all other bts' */ |
| 293 | void paging_request_stop(struct gsm_bts *_bts, struct gsm_subscriber *subscr, |
| 294 | struct gsm_lchan *lchan) |
| 295 | { |
| 296 | struct gsm_bts *bts = NULL; |
| 297 | |
Holger Hans Peter Freyther | b78507a | 2009-08-21 05:43:44 +0200 | [diff] [blame] | 298 | if (_bts) |
| 299 | _paging_request_stop(_bts, subscr, lchan); |
Holger Hans Peter Freyther | c51e333 | 2009-06-10 11:46:58 +0200 | [diff] [blame] | 300 | |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 301 | do { |
| 302 | /* |
| 303 | * FIXME: Don't use the lac of the subscriber... |
| 304 | * as it might have magically changed the lac.. use the |
| 305 | * location area of the _bts as reconfiguration of the |
| 306 | * network is probably happening less often. |
| 307 | */ |
Holger Hans Peter Freyther | b78507a | 2009-08-21 05:43:44 +0200 | [diff] [blame] | 308 | bts = gsm_bts_by_lac(subscr->net, subscr->lac, bts); |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 309 | if (!bts) |
| 310 | break; |
| 311 | |
| 312 | /* Stop paging */ |
Holger Hans Peter Freyther | c51e333 | 2009-06-10 11:46:58 +0200 | [diff] [blame] | 313 | if (bts != _bts) |
| 314 | _paging_request_stop(bts, subscr, NULL); |
Holger Hans Peter Freyther | 13d472d | 2009-06-10 02:45:42 +0200 | [diff] [blame] | 315 | } while (1); |
| 316 | } |
| 317 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 318 | void paging_update_buffer_space(struct gsm_bts *bts, u_int16_t free_slots) |
| 319 | { |
| 320 | bts->paging.available_slots = free_slots; |
| 321 | } |