blob: b63a717b008b53771cab409b041edf45e842f8e7 [file] [log] [blame]
Harald Welteb68899d2009-01-06 21:47:18 +00001/* 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 Welte2f1311b2009-01-06 23:50:39 +000038#include <stdio.h>
39#include <stdlib.h>
Holger Freythere36e15e2009-02-14 18:05:03 +000040#include <assert.h>
Harald Welte2f1311b2009-01-06 23:50:39 +000041
Harald Welteb68899d2009-01-06 21:47:18 +000042#include <openbsc/paging.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020043#include <openbsc/talloc.h>
Harald Welteb68899d2009-01-06 21:47:18 +000044#include <openbsc/debug.h>
Holger Freyther4af2b482009-02-14 22:51:03 +000045#include <openbsc/signal.h>
Harald Welte2f1311b2009-01-06 23:50:39 +000046#include <openbsc/abis_rsl.h>
Holger Freytherceb59b72009-02-06 18:54:00 +000047#include <openbsc/gsm_data.h>
Harald Welteb68899d2009-01-06 21:47:18 +000048
Holger Freytherd4ec5282009-02-03 23:18:46 +000049#define PAGING_TIMEOUT 1, 75000
Holger Freytherdf892da2009-02-03 21:05:49 +000050#define MAX_PAGING_REQUEST 750
Harald Welte38c2f132009-01-06 23:10:57 +000051
Harald Welte2cf161b2009-06-20 22:36:41 +020052static void *tall_paging_ctx;
53
Holger Freyther3aa8d6c2009-02-04 02:14:45 +000054static unsigned int calculate_group(struct gsm_bts *bts, struct gsm_subscriber *subscr)
55{
56 int ccch_conf;
57 int bs_cc_chans;
58 int blocks;
59 unsigned int group;
60
61 ccch_conf = bts->chan_desc.ccch_conf;
62 bs_cc_chans = rsl_ccch_conf_to_bs_cc_chans(ccch_conf);
63 /* code word + 2, as 2 channels equals 0x0 */
Holger Freyther152a1472009-02-04 13:38:26 +000064 blocks = rsl_number_of_paging_subchannels(bts);
Holger Freyther3aa8d6c2009-02-04 02:14:45 +000065 group = get_paging_group(str_to_imsi(subscr->imsi),
66 bs_cc_chans, blocks);
67 return group;
68}
69
Harald Welte38c2f132009-01-06 23:10:57 +000070/*
71 * Kill one paging request update the internal list...
72 */
Holger Freyther1fd34142009-02-09 23:42:03 +000073static void paging_remove_request(struct gsm_bts_paging_state *paging_bts,
Holger Freytherceb59b72009-02-06 18:54:00 +000074 struct gsm_paging_request *to_be_deleted)
Holger Freyther3d949242009-02-06 18:08:18 +000075{
76 /* Update the last_request if that is necessary */
77 if (to_be_deleted == paging_bts->last_request) {
78 paging_bts->last_request =
Holger Freytherceb59b72009-02-06 18:54:00 +000079 (struct gsm_paging_request *)paging_bts->last_request->entry.next;
Holger Freyther3d949242009-02-06 18:08:18 +000080 if (&to_be_deleted->entry == &paging_bts->pending_requests)
81 paging_bts->last_request = NULL;
82 }
83
Harald Welteff117a82009-05-23 05:22:08 +000084 bsc_del_timer(&to_be_deleted->T3113);
Harald Welte38c2f132009-01-06 23:10:57 +000085 llist_del(&to_be_deleted->entry);
Harald Welte51cce1e2009-02-10 17:35:04 +000086 subscr_put(to_be_deleted->subscr);
Harald Welte2cf161b2009-06-20 22:36:41 +020087 talloc_free(to_be_deleted);
Harald Welte38c2f132009-01-06 23:10:57 +000088}
89
Holger Freyther33ba7522009-02-06 22:33:25 +000090static void page_ms(struct gsm_paging_request *request)
91{
Harald Welte2f1311b2009-01-06 23:50:39 +000092 u_int8_t mi[128];
93 unsigned long int tmsi;
94 unsigned int mi_len;
Holger Freyther33ba7522009-02-06 22:33:25 +000095 unsigned int page_group;
96
97 DEBUGP(DPAG, "Going to send paging commands: '%s'\n",
98 request->subscr->imsi);
Holger Freyther33ba7522009-02-06 22:33:25 +000099
100 page_group = calculate_group(request->bts, request->subscr);
101 tmsi = strtoul(request->subscr->tmsi, NULL, 10);
102 mi_len = generate_mid_from_tmsi(mi, tmsi);
103 rsl_paging_cmd(request->bts, page_group, mi_len, mi,
104 request->chan_type);
105}
106
Holger Freyther334f2362009-02-11 03:00:20 +0000107static void paging_move_to_next(struct gsm_bts_paging_state *paging_bts)
108{
109 paging_bts->last_request =
110 (struct gsm_paging_request *)paging_bts->last_request->entry.next;
111 if (&paging_bts->last_request->entry == &paging_bts->pending_requests)
112 paging_bts->last_request = NULL;
113}
114
Holger Freythere36e15e2009-02-14 18:05:03 +0000115/*
116 * This is kicked by the periodic PAGING LOAD Indicator
117 * coming from abis_rsl.c
118 *
119 * We attempt to iterate once over the list of items but
120 * only upto available_slots.
121 */
Holger Freytherb1ffbb62009-02-11 01:49:15 +0000122static void paging_handle_pending_requests(struct gsm_bts_paging_state *paging_bts)
123{
Holger Freythere36e15e2009-02-14 18:05:03 +0000124 struct gsm_paging_request *initial_request = NULL;
125 struct gsm_paging_request *current_request = NULL;
Harald Welte38c2f132009-01-06 23:10:57 +0000126
Holger Freythere36e15e2009-02-14 18:05:03 +0000127 /*
128 * Determine if the pending_requests list is empty and
129 * return then.
130 */
Harald Welte75a1fa82009-02-17 01:39:41 +0000131 if (llist_empty(&paging_bts->pending_requests)) {
Harald Welte38c2f132009-01-06 23:10:57 +0000132 paging_bts->last_request = NULL;
Harald Welte75a1fa82009-02-17 01:39:41 +0000133 /* since the list is empty, no need to reschedule the timer */
Harald Welte38c2f132009-01-06 23:10:57 +0000134 return;
135 }
136
Holger Freythere36e15e2009-02-14 18:05:03 +0000137 if (!paging_bts->last_request)
Holger Freyther531c0de2009-02-14 23:35:09 +0000138 paging_bts->last_request =
139 (struct gsm_paging_request *)paging_bts->pending_requests.next;
Harald Welte2f1311b2009-01-06 23:50:39 +0000140
Holger Freythere36e15e2009-02-14 18:05:03 +0000141 assert(paging_bts->last_request);
142 initial_request = paging_bts->last_request;
143 current_request = initial_request;
144
145 do {
146 /* handle the paging request now */
147 page_ms(current_request);
148 paging_bts->available_slots--;
149
150 /*
151 * move to the next item. We might wrap around
152 * this means last_request will be NULL and we just
153 * call paging_page_to_next again. It it guranteed
154 * that the list is not empty.
155 */
156 paging_move_to_next(paging_bts);
157 if (!paging_bts->last_request)
Holger Freyther531c0de2009-02-14 23:35:09 +0000158 paging_bts->last_request =
159 (struct gsm_paging_request *)paging_bts->pending_requests.next;
Holger Freythere36e15e2009-02-14 18:05:03 +0000160 current_request = paging_bts->last_request;
161 } while (paging_bts->available_slots > 0
162 && initial_request != current_request);
Harald Welte75a1fa82009-02-17 01:39:41 +0000163
Harald Welteff117a82009-05-23 05:22:08 +0000164 bsc_schedule_timer(&paging_bts->work_timer, 1, 0);
Harald Welte75a1fa82009-02-17 01:39:41 +0000165}
166
167static void paging_worker(void *data)
168{
169 struct gsm_bts_paging_state *paging_bts = data;
170
171 paging_handle_pending_requests(paging_bts);
Harald Welte38c2f132009-01-06 23:10:57 +0000172}
173
Holger Freyther1fd34142009-02-09 23:42:03 +0000174void paging_init(struct gsm_bts *bts)
Holger Freytherceb59b72009-02-06 18:54:00 +0000175{
176 bts->paging.bts = bts;
177 INIT_LLIST_HEAD(&bts->paging.pending_requests);
Harald Welte75a1fa82009-02-17 01:39:41 +0000178 bts->paging.work_timer.cb = paging_worker;
179 bts->paging.work_timer.data = &bts->paging;
Holger Freyther392209c2009-02-10 00:06:19 +0000180
181 /* Large number, until we get a proper message */
Harald Welte75a1fa82009-02-17 01:39:41 +0000182 bts->paging.available_slots = 100;
Holger Freytherceb59b72009-02-06 18:54:00 +0000183}
184
Holger Freyther1fd34142009-02-09 23:42:03 +0000185static int paging_pending_request(struct gsm_bts_paging_state *bts,
Harald Welteb68899d2009-01-06 21:47:18 +0000186 struct gsm_subscriber *subscr) {
Holger Freytherceb59b72009-02-06 18:54:00 +0000187 struct gsm_paging_request *req;
Harald Welteb68899d2009-01-06 21:47:18 +0000188
189 llist_for_each_entry(req, &bts->pending_requests, entry) {
190 if (subscr == req->subscr)
191 return 1;
192 }
193
194 return 0;
195}
196
Harald Welte51cce1e2009-02-10 17:35:04 +0000197static void paging_T3113_expired(void *data)
198{
Holger Freyther83e44b02009-02-14 18:04:59 +0000199 struct gsm_paging_request *req = (struct gsm_paging_request *)data;
Harald Welte595ad7b2009-02-16 22:05:44 +0000200 struct paging_signal_data sig_data;
Harald Welte51cce1e2009-02-10 17:35:04 +0000201
202 DEBUGP(DPAG, "T3113 expired for request %p (%s)\n",
203 req, req->subscr->imsi);
204
Harald Welte595ad7b2009-02-16 22:05:44 +0000205 sig_data.subscr = req->subscr,
206 sig_data.bts = req->bts,
207 sig_data.lchan = NULL,
Holger Freyther4af2b482009-02-14 22:51:03 +0000208
Harald Welte595ad7b2009-02-16 22:05:44 +0000209 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Welte09e38af2009-02-16 22:52:23 +0000210 if (req->cbfn)
Harald Welte75a1fa82009-02-17 01:39:41 +0000211 req->cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED, NULL, NULL,
Harald Welte09e38af2009-02-16 22:52:23 +0000212 req->cbfn_param);
Harald Welte51cce1e2009-02-10 17:35:04 +0000213 paging_remove_request(&req->bts->paging, req);
214}
215
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200216static void _paging_request(struct gsm_bts *bts, struct gsm_subscriber *subscr,
217 int type, gsm_cbfn *cbfn, void *data)
Harald Welte09e38af2009-02-16 22:52:23 +0000218{
Holger Freytherceb59b72009-02-06 18:54:00 +0000219 struct gsm_bts_paging_state *bts_entry = &bts->paging;
220 struct gsm_paging_request *req;
Harald Welteb68899d2009-01-06 21:47:18 +0000221
Holger Freyther49314f62009-02-14 23:53:20 +0000222 if (paging_pending_request(bts_entry, subscr)) {
223 DEBUGP(DPAG, "Paging request already pending\n");
224 return;
225 }
226
Harald Welte4bfdfe72009-06-10 23:11:52 +0800227 DEBUGP(DPAG, "Start paging on bts %d.\n", bts->nr);
Harald Welte470ec292009-06-26 20:25:23 +0200228 req = talloc_zero(tall_paging_ctx, struct gsm_paging_request);
Harald Welteb68899d2009-01-06 21:47:18 +0000229 req->subscr = subscr_get(subscr);
230 req->bts = bts;
Harald Welte38c2f132009-01-06 23:10:57 +0000231 req->chan_type = type;
Harald Welte09e38af2009-02-16 22:52:23 +0000232 req->cbfn = cbfn;
233 req->cbfn_param = data;
Harald Welte51cce1e2009-02-10 17:35:04 +0000234 req->T3113.cb = paging_T3113_expired;
235 req->T3113.data = req;
Harald Welteff117a82009-05-23 05:22:08 +0000236 bsc_schedule_timer(&req->T3113, T3113_VALUE);
Holger Freyther49314f62009-02-14 23:53:20 +0000237 llist_add_tail(&req->entry, &bts_entry->pending_requests);
Harald Welte75a1fa82009-02-17 01:39:41 +0000238
Harald Welteff117a82009-05-23 05:22:08 +0000239 if (!bsc_timer_pending(&bts_entry->work_timer))
240 bsc_schedule_timer(&bts_entry->work_timer, 1, 0);
Harald Welteb68899d2009-01-06 21:47:18 +0000241}
Harald Welte763da002009-02-06 12:52:14 +0000242
Holger Freyther04866d42009-03-31 04:35:19 +0200243void paging_request(struct gsm_network *network, struct gsm_subscriber *subscr,
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200244 int type, gsm_cbfn *cbfn, void *data)
245{
246 struct gsm_bts *bts = NULL;
247
248 do {
Holger Freyther04866d42009-03-31 04:35:19 +0200249 bts = gsm_bts_by_lac(network, subscr->lac, bts);
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200250 if (!bts)
251 break;
252
253 /* Trigger paging */
254 _paging_request(bts, subscr, RSL_CHANNEED_TCH_F, cbfn, data);
255 } while (1);
256}
257
258
Harald Welte763da002009-02-06 12:52:14 +0000259/* we consciously ignore the type of the request here */
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200260static void _paging_request_stop(struct gsm_bts *bts, struct gsm_subscriber *subscr,
261 struct gsm_lchan *lchan)
Harald Welte763da002009-02-06 12:52:14 +0000262{
Holger Freytherceb59b72009-02-06 18:54:00 +0000263 struct gsm_bts_paging_state *bts_entry = &bts->paging;
264 struct gsm_paging_request *req, *req2;
Harald Welte763da002009-02-06 12:52:14 +0000265
266 llist_for_each_entry_safe(req, req2, &bts_entry->pending_requests,
267 entry) {
Holger Freyther3d949242009-02-06 18:08:18 +0000268 if (req->subscr == subscr) {
Harald Welte4bfdfe72009-06-10 23:11:52 +0800269 if (lchan && req->cbfn) {
270 DEBUGP(DPAG, "Stop paging on bts %d, calling cbfn.\n", bts->nr);
Harald Welte09e38af2009-02-16 22:52:23 +0000271 req->cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED,
Harald Welte75a1fa82009-02-17 01:39:41 +0000272 NULL, lchan, req->cbfn_param);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800273 } else
274 DEBUGP(DPAG, "Stop paging on bts %d silently.\n", bts->nr);
Holger Freyther1fd34142009-02-09 23:42:03 +0000275 paging_remove_request(&bts->paging, req);
Holger Freyther3d949242009-02-06 18:08:18 +0000276 break;
277 }
Harald Welte763da002009-02-06 12:52:14 +0000278 }
279}
Holger Freyther392209c2009-02-10 00:06:19 +0000280
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200281/* Stop paging on all other bts' */
282void paging_request_stop(struct gsm_bts *_bts, struct gsm_subscriber *subscr,
283 struct gsm_lchan *lchan)
284{
285 struct gsm_bts *bts = NULL;
286
Holger Hans Peter Freytherfdac4cc2009-06-10 11:46:58 +0200287 _paging_request_stop(_bts, subscr, lchan);
288
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200289 do {
290 /*
291 * FIXME: Don't use the lac of the subscriber...
292 * as it might have magically changed the lac.. use the
293 * location area of the _bts as reconfiguration of the
294 * network is probably happening less often.
295 */
296 bts = gsm_bts_by_lac(_bts->network, subscr->lac, bts);
297 if (!bts)
298 break;
299
300 /* Stop paging */
Holger Hans Peter Freytherfdac4cc2009-06-10 11:46:58 +0200301 if (bts != _bts)
302 _paging_request_stop(bts, subscr, NULL);
Holger Hans Peter Freyther2c451232009-06-10 02:45:42 +0200303 } while (1);
304}
305
Holger Freyther392209c2009-02-10 00:06:19 +0000306void paging_update_buffer_space(struct gsm_bts *bts, u_int16_t free_slots)
307{
308 bts->paging.available_slots = free_slots;
309}
Harald Welte7bfc2672009-07-28 00:41:45 +0200310
311static __attribute__((constructor)) void on_dso_load_paging(void)
312{
313 tall_paging_ctx = talloc_named_const(NULL, 1, "paging_request");
314}