Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [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 | |
Harald Welte | 2f1311b | 2009-01-06 23:50:39 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
Holger Freyther | e36e15e | 2009-02-14 18:05:03 +0000 | [diff] [blame] | 40 | #include <assert.h> |
Harald Welte | 2f1311b | 2009-01-06 23:50:39 +0000 | [diff] [blame] | 41 | |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 42 | #include <openbsc/paging.h> |
| 43 | #include <openbsc/debug.h> |
Holger Freyther | 4af2b48 | 2009-02-14 22:51:03 +0000 | [diff] [blame] | 44 | #include <openbsc/signal.h> |
Harald Welte | 2f1311b | 2009-01-06 23:50:39 +0000 | [diff] [blame] | 45 | #include <openbsc/abis_rsl.h> |
Holger Freyther | ceb59b7 | 2009-02-06 18:54:00 +0000 | [diff] [blame] | 46 | #include <openbsc/gsm_data.h> |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 47 | |
Holger Freyther | d4ec528 | 2009-02-03 23:18:46 +0000 | [diff] [blame] | 48 | #define PAGING_TIMEOUT 1, 75000 |
Holger Freyther | df892da | 2009-02-03 21:05:49 +0000 | [diff] [blame] | 49 | #define MAX_PAGING_REQUEST 750 |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 50 | |
Holger Freyther | 3aa8d6c | 2009-02-04 02:14:45 +0000 | [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 */ |
Holger Freyther | 152a147 | 2009-02-04 13:38:26 +0000 | [diff] [blame] | 61 | blocks = rsl_number_of_paging_subchannels(bts); |
Holger Freyther | 3aa8d6c | 2009-02-04 02:14:45 +0000 | [diff] [blame] | 62 | group = get_paging_group(str_to_imsi(subscr->imsi), |
| 63 | bs_cc_chans, blocks); |
| 64 | return group; |
| 65 | } |
| 66 | |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 67 | /* |
| 68 | * Kill one paging request update the internal list... |
| 69 | */ |
Holger Freyther | 1fd3414 | 2009-02-09 23:42:03 +0000 | [diff] [blame] | 70 | static void paging_remove_request(struct gsm_bts_paging_state *paging_bts, |
Holger Freyther | ceb59b7 | 2009-02-06 18:54:00 +0000 | [diff] [blame] | 71 | struct gsm_paging_request *to_be_deleted) |
Holger Freyther | 3d94924 | 2009-02-06 18:08:18 +0000 | [diff] [blame] | 72 | { |
| 73 | /* Update the last_request if that is necessary */ |
| 74 | if (to_be_deleted == paging_bts->last_request) { |
| 75 | paging_bts->last_request = |
Holger Freyther | ceb59b7 | 2009-02-06 18:54:00 +0000 | [diff] [blame] | 76 | (struct gsm_paging_request *)paging_bts->last_request->entry.next; |
Holger Freyther | 3d94924 | 2009-02-06 18:08:18 +0000 | [diff] [blame] | 77 | if (&to_be_deleted->entry == &paging_bts->pending_requests) |
| 78 | paging_bts->last_request = NULL; |
| 79 | } |
| 80 | |
Holger Freyther | ed2aea8 | 2009-02-11 01:18:42 +0000 | [diff] [blame] | 81 | del_timer(&to_be_deleted->T3113); |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 82 | llist_del(&to_be_deleted->entry); |
Harald Welte | 51cce1e | 2009-02-10 17:35:04 +0000 | [diff] [blame] | 83 | subscr_put(to_be_deleted->subscr); |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 84 | free(to_be_deleted); |
| 85 | } |
| 86 | |
Holger Freyther | 33ba752 | 2009-02-06 22:33:25 +0000 | [diff] [blame] | 87 | static void page_ms(struct gsm_paging_request *request) |
| 88 | { |
Harald Welte | 2f1311b | 2009-01-06 23:50:39 +0000 | [diff] [blame] | 89 | u_int8_t mi[128]; |
| 90 | unsigned long int tmsi; |
| 91 | unsigned int mi_len; |
Holger Freyther | 33ba752 | 2009-02-06 22:33:25 +0000 | [diff] [blame] | 92 | unsigned int page_group; |
| 93 | |
| 94 | DEBUGP(DPAG, "Going to send paging commands: '%s'\n", |
| 95 | request->subscr->imsi); |
Holger Freyther | 33ba752 | 2009-02-06 22:33:25 +0000 | [diff] [blame] | 96 | |
| 97 | page_group = calculate_group(request->bts, request->subscr); |
| 98 | tmsi = strtoul(request->subscr->tmsi, NULL, 10); |
| 99 | mi_len = generate_mid_from_tmsi(mi, tmsi); |
| 100 | rsl_paging_cmd(request->bts, page_group, mi_len, mi, |
| 101 | request->chan_type); |
| 102 | } |
| 103 | |
Holger Freyther | 334f236 | 2009-02-11 03:00:20 +0000 | [diff] [blame] | 104 | static void paging_move_to_next(struct gsm_bts_paging_state *paging_bts) |
| 105 | { |
| 106 | paging_bts->last_request = |
| 107 | (struct gsm_paging_request *)paging_bts->last_request->entry.next; |
| 108 | if (&paging_bts->last_request->entry == &paging_bts->pending_requests) |
| 109 | paging_bts->last_request = NULL; |
| 110 | } |
| 111 | |
Holger Freyther | e36e15e | 2009-02-14 18:05:03 +0000 | [diff] [blame] | 112 | /* |
| 113 | * This is kicked by the periodic PAGING LOAD Indicator |
| 114 | * coming from abis_rsl.c |
| 115 | * |
| 116 | * We attempt to iterate once over the list of items but |
| 117 | * only upto available_slots. |
| 118 | */ |
Holger Freyther | b1ffbb6 | 2009-02-11 01:49:15 +0000 | [diff] [blame] | 119 | static void paging_handle_pending_requests(struct gsm_bts_paging_state *paging_bts) |
| 120 | { |
Holger Freyther | e36e15e | 2009-02-14 18:05:03 +0000 | [diff] [blame] | 121 | struct gsm_paging_request *initial_request = NULL; |
| 122 | struct gsm_paging_request *current_request = NULL; |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 123 | |
Holger Freyther | e36e15e | 2009-02-14 18:05:03 +0000 | [diff] [blame] | 124 | /* |
| 125 | * Determine if the pending_requests list is empty and |
| 126 | * return then. |
| 127 | */ |
| 128 | if (&paging_bts->pending_requests == paging_bts->pending_requests.next) { |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 129 | paging_bts->last_request = NULL; |
| 130 | return; |
| 131 | } |
| 132 | |
Holger Freyther | e36e15e | 2009-02-14 18:05:03 +0000 | [diff] [blame] | 133 | if (!paging_bts->last_request) |
Holger Freyther | 531c0de | 2009-02-14 23:35:09 +0000 | [diff] [blame] | 134 | paging_bts->last_request = |
| 135 | (struct gsm_paging_request *)paging_bts->pending_requests.next; |
Harald Welte | 2f1311b | 2009-01-06 23:50:39 +0000 | [diff] [blame] | 136 | |
Holger Freyther | e36e15e | 2009-02-14 18:05:03 +0000 | [diff] [blame] | 137 | assert(paging_bts->last_request); |
| 138 | initial_request = paging_bts->last_request; |
| 139 | current_request = initial_request; |
| 140 | |
| 141 | do { |
| 142 | /* handle the paging request now */ |
| 143 | page_ms(current_request); |
| 144 | paging_bts->available_slots--; |
| 145 | |
| 146 | /* |
| 147 | * move to the next item. We might wrap around |
| 148 | * this means last_request will be NULL and we just |
| 149 | * call paging_page_to_next again. It it guranteed |
| 150 | * that the list is not empty. |
| 151 | */ |
| 152 | paging_move_to_next(paging_bts); |
| 153 | if (!paging_bts->last_request) |
Holger Freyther | 531c0de | 2009-02-14 23:35:09 +0000 | [diff] [blame] | 154 | paging_bts->last_request = |
| 155 | (struct gsm_paging_request *)paging_bts->pending_requests.next; |
Holger Freyther | e36e15e | 2009-02-14 18:05:03 +0000 | [diff] [blame] | 156 | current_request = paging_bts->last_request; |
| 157 | } while (paging_bts->available_slots > 0 |
| 158 | && initial_request != current_request); |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Holger Freyther | 1fd3414 | 2009-02-09 23:42:03 +0000 | [diff] [blame] | 161 | void paging_init(struct gsm_bts *bts) |
Holger Freyther | ceb59b7 | 2009-02-06 18:54:00 +0000 | [diff] [blame] | 162 | { |
| 163 | bts->paging.bts = bts; |
| 164 | INIT_LLIST_HEAD(&bts->paging.pending_requests); |
Holger Freyther | 392209c | 2009-02-10 00:06:19 +0000 | [diff] [blame] | 165 | |
| 166 | /* Large number, until we get a proper message */ |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 167 | bts->paging.available_slots = 10; |
Holger Freyther | ceb59b7 | 2009-02-06 18:54:00 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Holger Freyther | 1fd3414 | 2009-02-09 23:42:03 +0000 | [diff] [blame] | 170 | static int paging_pending_request(struct gsm_bts_paging_state *bts, |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 171 | struct gsm_subscriber *subscr) { |
Holger Freyther | ceb59b7 | 2009-02-06 18:54:00 +0000 | [diff] [blame] | 172 | struct gsm_paging_request *req; |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 173 | |
| 174 | llist_for_each_entry(req, &bts->pending_requests, entry) { |
| 175 | if (subscr == req->subscr) |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
Harald Welte | 51cce1e | 2009-02-10 17:35:04 +0000 | [diff] [blame] | 182 | static void paging_T3113_expired(void *data) |
| 183 | { |
Holger Freyther | 83e44b0 | 2009-02-14 18:04:59 +0000 | [diff] [blame] | 184 | struct gsm_paging_request *req = (struct gsm_paging_request *)data; |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 185 | struct paging_signal_data sig_data; |
Harald Welte | 51cce1e | 2009-02-10 17:35:04 +0000 | [diff] [blame] | 186 | |
| 187 | DEBUGP(DPAG, "T3113 expired for request %p (%s)\n", |
| 188 | req, req->subscr->imsi); |
| 189 | |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 190 | sig_data.subscr = req->subscr, |
| 191 | sig_data.bts = req->bts, |
| 192 | sig_data.lchan = NULL, |
Holger Freyther | 4af2b48 | 2009-02-14 22:51:03 +0000 | [diff] [blame] | 193 | |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 194 | dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data); |
Harald Welte | 51cce1e | 2009-02-10 17:35:04 +0000 | [diff] [blame] | 195 | paging_remove_request(&req->bts->paging, req); |
| 196 | } |
| 197 | |
Holger Freyther | 1fd3414 | 2009-02-09 23:42:03 +0000 | [diff] [blame] | 198 | void paging_request(struct gsm_bts *bts, struct gsm_subscriber *subscr, int type) { |
Holger Freyther | ceb59b7 | 2009-02-06 18:54:00 +0000 | [diff] [blame] | 199 | struct gsm_bts_paging_state *bts_entry = &bts->paging; |
| 200 | struct gsm_paging_request *req; |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 201 | |
Holger Freyther | 49314f6 | 2009-02-14 23:53:20 +0000 | [diff] [blame] | 202 | if (paging_pending_request(bts_entry, subscr)) { |
| 203 | DEBUGP(DPAG, "Paging request already pending\n"); |
| 204 | return; |
| 205 | } |
| 206 | |
Holger Freyther | ceb59b7 | 2009-02-06 18:54:00 +0000 | [diff] [blame] | 207 | req = (struct gsm_paging_request *)malloc(sizeof(*req)); |
Holger Freyther | 1525de0 | 2009-01-27 23:38:27 +0000 | [diff] [blame] | 208 | memset(req, 0, sizeof(*req)); |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 209 | req->subscr = subscr_get(subscr); |
| 210 | req->bts = bts; |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 211 | req->chan_type = type; |
Harald Welte | 51cce1e | 2009-02-10 17:35:04 +0000 | [diff] [blame] | 212 | req->T3113.cb = paging_T3113_expired; |
| 213 | req->T3113.data = req; |
| 214 | schedule_timer(&req->T3113, T3113_VALUE); |
Holger Freyther | 49314f6 | 2009-02-14 23:53:20 +0000 | [diff] [blame] | 215 | llist_add_tail(&req->entry, &bts_entry->pending_requests); |
Harald Welte | b68899d | 2009-01-06 21:47:18 +0000 | [diff] [blame] | 216 | } |
Harald Welte | 763da00 | 2009-02-06 12:52:14 +0000 | [diff] [blame] | 217 | |
| 218 | /* we consciously ignore the type of the request here */ |
Holger Freyther | 1fd3414 | 2009-02-09 23:42:03 +0000 | [diff] [blame] | 219 | void paging_request_stop(struct gsm_bts *bts, struct gsm_subscriber *subscr) |
Harald Welte | 763da00 | 2009-02-06 12:52:14 +0000 | [diff] [blame] | 220 | { |
Holger Freyther | ceb59b7 | 2009-02-06 18:54:00 +0000 | [diff] [blame] | 221 | struct gsm_bts_paging_state *bts_entry = &bts->paging; |
| 222 | struct gsm_paging_request *req, *req2; |
Harald Welte | 763da00 | 2009-02-06 12:52:14 +0000 | [diff] [blame] | 223 | |
| 224 | llist_for_each_entry_safe(req, req2, &bts_entry->pending_requests, |
| 225 | entry) { |
Holger Freyther | 3d94924 | 2009-02-06 18:08:18 +0000 | [diff] [blame] | 226 | if (req->subscr == subscr) { |
Holger Freyther | 1fd3414 | 2009-02-09 23:42:03 +0000 | [diff] [blame] | 227 | paging_remove_request(&bts->paging, req); |
Holger Freyther | 3d94924 | 2009-02-06 18:08:18 +0000 | [diff] [blame] | 228 | break; |
| 229 | } |
Harald Welte | 763da00 | 2009-02-06 12:52:14 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
Holger Freyther | 392209c | 2009-02-10 00:06:19 +0000 | [diff] [blame] | 232 | |
| 233 | void paging_update_buffer_space(struct gsm_bts *bts, u_int16_t free_slots) |
| 234 | { |
| 235 | bts->paging.available_slots = free_slots; |
Holger Freyther | b1ffbb6 | 2009-02-11 01:49:15 +0000 | [diff] [blame] | 236 | paging_handle_pending_requests(&bts->paging); |
Holger Freyther | 392209c | 2009-02-10 00:06:19 +0000 | [diff] [blame] | 237 | } |