blob: 4dfa921619a68b84701092facfd8977a070133e4 [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/* 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 Affero General Public License as published by
7 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20/*
21 * Relevant specs:
22 * 12.21:
23 * - 9.4.12 for CCCH Local Threshold
24 *
25 * 05.58:
26 * - 8.5.2 CCCH Load indication
27 * - 9.3.15 Paging Load
28 *
29 * Approach:
30 * - Send paging command to subscriber
31 * - On Channel Request we will remember the reason
32 * - After the ACK we will request the identity
33 * - Then we will send assign the gsm_subscriber and
34 * - and call a callback
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <assert.h>
40
Jonathan Santos5a45b152011-08-17 15:33:57 -040041#include <osmocom/core/talloc.h>
42#include <osmocom/gsm/gsm48.h>
43#include <osmocom/gsm/gsm0502.h>
44
Jonathan Santos03fd8d02011-05-25 13:54:02 -040045#include <openbsc/paging.h>
Jonathan Santos03fd8d02011-05-25 13:54:02 -040046#include <openbsc/debug.h>
47#include <openbsc/signal.h>
48#include <openbsc/abis_rsl.h>
49#include <openbsc/gsm_data.h>
50#include <openbsc/chan_alloc.h>
51#include <openbsc/bsc_api.h>
52
53void *tall_paging_ctx;
54
55#define PAGING_TIMER 0, 500000
56
Jonathan Santos03fd8d02011-05-25 13:54:02 -040057/*
58 * Kill one paging request update the internal list...
59 */
60static void paging_remove_request(struct gsm_bts_paging_state *paging_bts,
61 struct gsm_paging_request *to_be_deleted)
62{
Jonathan Santos5a45b152011-08-17 15:33:57 -040063 osmo_timer_del(&to_be_deleted->T3113);
Jonathan Santos03fd8d02011-05-25 13:54:02 -040064 llist_del(&to_be_deleted->entry);
65 subscr_put(to_be_deleted->subscr);
66 talloc_free(to_be_deleted);
67}
68
69static void page_ms(struct gsm_paging_request *request)
70{
Jonathan Santos5a45b152011-08-17 15:33:57 -040071 uint8_t mi[128];
Jonathan Santos03fd8d02011-05-25 13:54:02 -040072 unsigned int mi_len;
73 unsigned int page_group;
Jonathan Santos5a45b152011-08-17 15:33:57 -040074 struct gsm_bts *bts = request->bts;
Jonathan Santos03fd8d02011-05-25 13:54:02 -040075
76 LOGP(DPAG, LOGL_INFO, "Going to send paging commands: imsi: '%s' tmsi: '0x%x'\n",
77 request->subscr->imsi, request->subscr->tmsi);
78
79 if (request->subscr->tmsi == GSM_RESERVED_TMSI)
80 mi_len = gsm48_generate_mid_from_imsi(mi, request->subscr->imsi);
81 else
82 mi_len = gsm48_generate_mid_from_tmsi(mi, request->subscr->tmsi);
83
Jonathan Santos5a45b152011-08-17 15:33:57 -040084 page_group = gsm0502_calc_paging_group(&bts->si_common.chan_desc,
85 str_to_imsi(request->subscr->imsi));
86 gsm0808_page(bts, page_group, mi_len, mi, request->chan_type);
Jonathan Santos03fd8d02011-05-25 13:54:02 -040087}
88
89static void paging_schedule_if_needed(struct gsm_bts_paging_state *paging_bts)
90{
91 if (llist_empty(&paging_bts->pending_requests))
92 return;
93
Jonathan Santos5a45b152011-08-17 15:33:57 -040094 if (!osmo_timer_pending(&paging_bts->work_timer))
95 osmo_timer_schedule(&paging_bts->work_timer, PAGING_TIMER);
Jonathan Santos03fd8d02011-05-25 13:54:02 -040096}
97
98
99static void paging_handle_pending_requests(struct gsm_bts_paging_state *paging_bts);
100static void paging_give_credit(void *data)
101{
102 struct gsm_bts_paging_state *paging_bts = data;
103
104 LOGP(DPAG, LOGL_NOTICE, "No slots available on bts nr %d\n", paging_bts->bts->nr);
105 paging_bts->available_slots = 20;
106 paging_handle_pending_requests(paging_bts);
107}
108
109static int can_send_pag_req(struct gsm_bts *bts, int rsl_type)
110{
111 struct pchan_load pl;
112 int count;
113
114 memset(&pl, 0, sizeof(pl));
115 bts_chan_load(&pl, bts);
116
117 switch (rsl_type) {
118 case RSL_CHANNEED_TCH_F:
119 case RSL_CHANNEED_TCH_ForH:
120 goto count_tch;
121 break;
122 case RSL_CHANNEED_SDCCH:
123 goto count_sdcch;
124 break;
125 case RSL_CHANNEED_ANY:
126 default:
127 if (bts->network->pag_any_tch)
128 goto count_tch;
129 else
130 goto count_sdcch;
131 break;
132 }
133
134 return 0;
135
136 /* could available SDCCH */
137count_sdcch:
138 count = 0;
139 count += pl.pchan[GSM_PCHAN_SDCCH8_SACCH8C].total
140 - pl.pchan[GSM_PCHAN_SDCCH8_SACCH8C].used;
141 count += pl.pchan[GSM_PCHAN_CCCH_SDCCH4].total
142 - pl.pchan[GSM_PCHAN_CCCH_SDCCH4].used;
143 return bts->paging.free_chans_need > count;
144
145count_tch:
146 count = 0;
147 count += pl.pchan[GSM_PCHAN_TCH_F].total
148 - pl.pchan[GSM_PCHAN_TCH_F].used;
149 if (bts->network->neci)
150 count += pl.pchan[GSM_PCHAN_TCH_H].total
151 - pl.pchan[GSM_PCHAN_TCH_H].used;
152 return bts->paging.free_chans_need > count;
153}
154
155/*
156 * This is kicked by the periodic PAGING LOAD Indicator
157 * coming from abis_rsl.c
158 *
159 * We attempt to iterate once over the list of items but
160 * only upto available_slots.
161 */
162static void paging_handle_pending_requests(struct gsm_bts_paging_state *paging_bts)
163{
164 struct gsm_paging_request *request = NULL;
165
166 /*
167 * Determine if the pending_requests list is empty and
168 * return then.
169 */
170 if (llist_empty(&paging_bts->pending_requests)) {
171 /* since the list is empty, no need to reschedule the timer */
172 return;
173 }
174
175 /*
176 * In case the BTS does not provide us with load indication and we
177 * ran out of slots, call an autofill routine. It might be that the
178 * BTS did not like our paging messages and then we have counted down
179 * to zero and we do not get any messages.
180 */
181 if (paging_bts->available_slots == 0) {
182 paging_bts->credit_timer.cb = paging_give_credit;
183 paging_bts->credit_timer.data = paging_bts;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400184 osmo_timer_schedule(&paging_bts->credit_timer, 5, 0);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400185 return;
186 }
187
188 request = llist_entry(paging_bts->pending_requests.next,
189 struct gsm_paging_request, entry);
190
191 /* we need to determine the number of free channels */
192 if (paging_bts->free_chans_need != -1) {
193 if (can_send_pag_req(request->bts, request->chan_type) != 0)
194 goto skip_paging;
195 }
196
197 /* handle the paging request now */
198 page_ms(request);
199 paging_bts->available_slots--;
200 request->attempts++;
201
202 /* take the current and add it to the back */
203 llist_del(&request->entry);
204 llist_add_tail(&request->entry, &paging_bts->pending_requests);
205
206skip_paging:
Jonathan Santos5a45b152011-08-17 15:33:57 -0400207 osmo_timer_schedule(&paging_bts->work_timer, PAGING_TIMER);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400208}
209
210static void paging_worker(void *data)
211{
212 struct gsm_bts_paging_state *paging_bts = data;
213
214 paging_handle_pending_requests(paging_bts);
215}
216
Jonathan Santos5a45b152011-08-17 15:33:57 -0400217static void paging_init_if_needed(struct gsm_bts *bts)
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400218{
Jonathan Santos5a45b152011-08-17 15:33:57 -0400219 if (bts->paging.bts)
220 return;
221
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400222 bts->paging.bts = bts;
223 INIT_LLIST_HEAD(&bts->paging.pending_requests);
224 bts->paging.work_timer.cb = paging_worker;
225 bts->paging.work_timer.data = &bts->paging;
226
227 /* Large number, until we get a proper message */
228 bts->paging.available_slots = 20;
229}
230
231static int paging_pending_request(struct gsm_bts_paging_state *bts,
232 struct gsm_subscriber *subscr) {
233 struct gsm_paging_request *req;
234
235 llist_for_each_entry(req, &bts->pending_requests, entry) {
236 if (subscr == req->subscr)
237 return 1;
238 }
239
240 return 0;
241}
242
243static void paging_T3113_expired(void *data)
244{
245 struct gsm_paging_request *req = (struct gsm_paging_request *)data;
246 void *cbfn_param;
247 gsm_cbfn *cbfn;
248 int msg;
249
250 LOGP(DPAG, LOGL_INFO, "T3113 expired for request %p (%s)\n",
251 req, req->subscr->imsi);
252
253 /* must be destroyed before calling cbfn, to prevent double free */
Jonathan Santos5a45b152011-08-17 15:33:57 -0400254 osmo_counter_inc(req->bts->network->stats.paging.expired);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400255 cbfn_param = req->cbfn_param;
256 cbfn = req->cbfn;
257
258 /* did we ever manage to page the subscriber */
259 msg = req->attempts > 0 ? GSM_PAGING_EXPIRED : GSM_PAGING_BUSY;
260
261 /* destroy it now. Do not access req afterwards */
262 paging_remove_request(&req->bts->paging, req);
263
264 if (cbfn)
265 cbfn(GSM_HOOK_RR_PAGING, msg, NULL, NULL,
266 cbfn_param);
267}
268
269static int _paging_request(struct gsm_bts *bts, struct gsm_subscriber *subscr,
270 int type, gsm_cbfn *cbfn, void *data)
271{
272 struct gsm_bts_paging_state *bts_entry = &bts->paging;
273 struct gsm_paging_request *req;
274
275 if (paging_pending_request(bts_entry, subscr)) {
276 LOGP(DPAG, LOGL_INFO, "Paging request already pending for %s\n", subscr->imsi);
277 return -EEXIST;
278 }
279
280 LOGP(DPAG, LOGL_DEBUG, "Start paging of subscriber %llu on bts %d.\n",
281 subscr->id, bts->nr);
282 req = talloc_zero(tall_paging_ctx, struct gsm_paging_request);
283 req->subscr = subscr_get(subscr);
284 req->bts = bts;
285 req->chan_type = type;
286 req->cbfn = cbfn;
287 req->cbfn_param = data;
288 req->T3113.cb = paging_T3113_expired;
289 req->T3113.data = req;
Jonathan Santos5a45b152011-08-17 15:33:57 -0400290 osmo_timer_schedule(&req->T3113, bts->network->T3113, 0);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400291 llist_add_tail(&req->entry, &bts_entry->pending_requests);
292 paging_schedule_if_needed(bts_entry);
293
294 return 0;
295}
296
297int paging_request(struct gsm_network *network, struct gsm_subscriber *subscr,
298 int type, gsm_cbfn *cbfn, void *data)
299{
300 struct gsm_bts *bts = NULL;
301 int num_pages = 0;
302
Jonathan Santos5a45b152011-08-17 15:33:57 -0400303 osmo_counter_inc(network->stats.paging.attempted);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400304
305 /* start paging subscriber on all BTS within Location Area */
306 do {
307 int rc;
308
309 bts = gsm_bts_by_lac(network, subscr->lac, bts);
310 if (!bts)
311 break;
312
313 /* skip all currently inactive TRX */
314 if (!trx_is_usable(bts->c0))
315 continue;
316
Jonathan Santos5a45b152011-08-17 15:33:57 -0400317 /* maybe it is the first time we use it */
318 paging_init_if_needed(bts);
319
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400320 num_pages++;
321
322 /* Trigger paging, pass any error to caller */
323 rc = _paging_request(bts, subscr, type, cbfn, data);
324 if (rc < 0)
325 return rc;
326 } while (1);
327
328 if (num_pages == 0)
Jonathan Santos5a45b152011-08-17 15:33:57 -0400329 osmo_counter_inc(network->stats.paging.detached);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400330
331 return num_pages;
332}
333
334
335/* we consciously ignore the type of the request here */
336static void _paging_request_stop(struct gsm_bts *bts, struct gsm_subscriber *subscr,
337 struct gsm_subscriber_connection *conn,
338 struct msgb *msg)
339{
340 struct gsm_bts_paging_state *bts_entry = &bts->paging;
341 struct gsm_paging_request *req, *req2;
342
Jonathan Santos5a45b152011-08-17 15:33:57 -0400343 paging_init_if_needed(bts);
344
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400345 llist_for_each_entry_safe(req, req2, &bts_entry->pending_requests,
346 entry) {
347 if (req->subscr == subscr) {
348 if (conn && req->cbfn) {
349 LOGP(DPAG, LOGL_DEBUG, "Stop paging on bts %d, calling cbfn.\n", bts->nr);
350 req->cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED,
351 msg, conn, req->cbfn_param);
352 } else
353 LOGP(DPAG, LOGL_DEBUG, "Stop paging on bts %d silently.\n", bts->nr);
354 paging_remove_request(&bts->paging, req);
355 break;
356 }
357 }
358}
359
360/* Stop paging on all other bts' */
361void paging_request_stop(struct gsm_bts *_bts, struct gsm_subscriber *subscr,
362 struct gsm_subscriber_connection *conn,
363 struct msgb *msg)
364{
365 struct gsm_bts *bts = NULL;
366
367 if (_bts)
368 _paging_request_stop(_bts, subscr, conn, msg);
369
370 do {
371 /*
372 * FIXME: Don't use the lac of the subscriber...
373 * as it might have magically changed the lac.. use the
374 * location area of the _bts as reconfiguration of the
375 * network is probably happening less often.
376 */
377 bts = gsm_bts_by_lac(subscr->net, subscr->lac, bts);
378 if (!bts)
379 break;
380
381 /* Stop paging */
382 if (bts != _bts)
383 _paging_request_stop(bts, subscr, NULL, NULL);
384 } while (1);
385}
386
Jonathan Santos5a45b152011-08-17 15:33:57 -0400387void paging_update_buffer_space(struct gsm_bts *bts, uint16_t free_slots)
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400388{
Jonathan Santos5a45b152011-08-17 15:33:57 -0400389 paging_init_if_needed(bts);
390
391 osmo_timer_del(&bts->paging.credit_timer);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400392 bts->paging.available_slots = free_slots;
393 paging_schedule_if_needed(&bts->paging);
394}
Jonathan Santos5a45b152011-08-17 15:33:57 -0400395
396unsigned int paging_pending_requests_nr(struct gsm_bts *bts)
397{
398 unsigned int requests = 0;
399 struct gsm_paging_request *req;
400
401 paging_init_if_needed(bts);
402
403 llist_for_each_entry(req, &bts->paging.pending_requests, entry)
404 ++requests;
405
406 return requests;
407}