blob: 18c076334bd171f5bbe9fc2a03145d41b9c23ce8 [file] [log] [blame]
Andreas Eversberg39621c42012-06-27 15:36:27 +02001/* PDCH scheduler
2 *
3 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
Pau Espin Pedrol488aa292019-09-25 17:48:35 +020019
Andreas Eversberg39621c42012-06-27 15:36:27 +020020#include <gprs_bssgp_pcu.h>
21#include <gprs_rlcmac.h>
22#include <pcu_l1_if.h>
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020023#include <bts.h>
Holger Hans Peter Freyther099535a2013-10-16 17:42:31 +020024#include <tbf.h>
Pau Espin Pedrol9d1cdb12019-09-25 17:47:02 +020025#include <tbf_ul.h>
Max1187a772018-01-26 13:31:42 +010026#include <gprs_debug.h>
27#include <gprs_ms.h>
28#include <rlc.h>
29#include <sba.h>
Max6dc90b82018-02-19 17:17:28 +010030#include <pdch.h>
Jacob Erlbeck502bd1f2015-03-20 14:26:05 +010031#include "pcu_utils.h"
32
Max1187a772018-01-26 13:31:42 +010033extern "C" {
34 #include <osmocom/core/gsmtap.h>
35}
36
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010037struct tbf_sched_candidates {
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010038 struct gprs_rlcmac_tbf *ul_ass;
39 struct gprs_rlcmac_tbf *dl_ass;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010040 struct gprs_rlcmac_tbf *nacc;
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010041 struct gprs_rlcmac_ul_tbf *ul_ack;
42};
43
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +020044static void get_ctrl_msg_tbf_candidates(const struct gprs_rlcmac_pdch *pdch,
Pau Espin Pedrol48517622021-04-30 14:15:46 +020045 struct tbf_sched_candidates *tbf_cand)
Andreas Eversberg39621c42012-06-27 15:36:27 +020046{
Daniel Willmann4f3c4202014-08-07 14:58:59 +020047 struct gprs_rlcmac_ul_tbf *ul_tbf;
48 struct gprs_rlcmac_dl_tbf *dl_tbf;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010049 struct llist_item *pos;
Andreas Eversberge6228b32012-07-03 13:36:03 +020050
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +020051 llist_for_each_entry(pos, &pdch->trx->ul_tbfs, list) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010052 ul_tbf = as_ul_tbf((struct gprs_rlcmac_tbf *)pos->entry);
Pau Espin Pedrolb5fece92021-08-23 16:58:04 +020053 OSMO_ASSERT(ul_tbf);
Andreas Eversberg592e04a2012-07-15 06:25:37 +020054 /* this trx, this ts */
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +020055 if (!ul_tbf->is_control_ts(pdch->ts_no))
Andreas Eversberg592e04a2012-07-15 06:25:37 +020056 continue;
Max088c7df2018-01-23 20:16:23 +010057 if (ul_tbf->ul_ack_state_is(GPRS_RLCMAC_UL_ACK_SEND_ACK))
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010058 tbf_cand->ul_ack = ul_tbf;
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +020059 if (tbf_dl_ass_rts(ul_tbf))
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010060 tbf_cand->dl_ass = ul_tbf;
Pau Espin Pedrol6ad11a62021-07-27 12:27:08 +020061 if (tbf_ul_ass_rts(ul_tbf))
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010062 tbf_cand->ul_ass = ul_tbf;
Pau Espin Pedrol9345eb32021-02-18 14:27:14 +010063 /* NACC ready to send. TFI assigned is needed to send messages */
64 if (ul_tbf->is_tfi_assigned() && ms_nacc_rts(ul_tbf->ms()))
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010065 tbf_cand->nacc = ul_tbf;
Max5a6bcfb2017-09-01 14:36:44 +020066/* FIXME: Is this supposed to be fair? The last TBF for each wins? Maybe use llist_add_tail and skip once we have all
67states? */
Andreas Eversberg39621c42012-06-27 15:36:27 +020068 }
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +020069 llist_for_each_entry(pos, &pdch->trx->dl_tbfs, list) {
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010070 dl_tbf = as_dl_tbf((struct gprs_rlcmac_tbf *)pos->entry);
Pau Espin Pedrolb5fece92021-08-23 16:58:04 +020071 OSMO_ASSERT(dl_tbf);
Andreas Eversberg592e04a2012-07-15 06:25:37 +020072 /* this trx, this ts */
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +020073 if (!dl_tbf->is_control_ts(pdch->ts_no))
Andreas Eversberg592e04a2012-07-15 06:25:37 +020074 continue;
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +020075 if (tbf_dl_ass_rts(dl_tbf))
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010076 tbf_cand->dl_ass = dl_tbf;
Pau Espin Pedrol6ad11a62021-07-27 12:27:08 +020077 if (tbf_ul_ass_rts(dl_tbf))
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010078 tbf_cand->ul_ass = dl_tbf;
Pau Espin Pedrol9345eb32021-02-18 14:27:14 +010079 /* NACC ready to send. TFI assigned is needed to send messages */
80 if (dl_tbf->is_tfi_assigned() && ms_nacc_rts(dl_tbf->ms()))
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010081 tbf_cand->nacc = dl_tbf;
Andreas Eversberg592e04a2012-07-15 06:25:37 +020082 }
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020083}
84
Pau Espin Pedrol63254232021-05-11 14:04:33 +020085static struct gprs_rlcmac_ul_tbf *sched_select_uplink(struct gprs_rlcmac_pdch *pdch, bool require_gprs_only)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020086{
Pau Espin Pedrol107e94c2021-03-24 17:06:15 +010087 struct gprs_rlcmac_ul_tbf *tbf;
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020088 uint8_t i, tfi;
89
Holger Hans Peter Freyther90b87ea2013-10-27 10:38:31 +010090 /* select uplink resource */
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020091 for (i = 0, tfi = pdch->next_ul_tfi; i < 32;
92 i++, tfi = (tfi + 1) & 31) {
Daniel Willmann1e0c6102014-06-04 14:56:09 +020093 tbf = pdch->ul_tbf_by_tfi(tfi);
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020094 /* no TBF for this tfi, go next */
95 if (!tbf)
96 continue;
Holger Hans Peter Freyther483f77a2013-10-16 16:33:00 +020097 /* no UL resources needed, go next */
98 /* we don't need to give resources in FINISHED state,
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020099 * because we have received all blocks and only poll
100 * for packet control ack. */
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200101 if (tbf->state_is_not(TBF_ST_FLOW))
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200102 continue;
103
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100104 if (require_gprs_only && tbf->is_egprs_enabled())
105 continue;
106
Pau Espin Pedrol63254232021-05-11 14:04:33 +0200107 /* use this USF (tbf) */
Holger Hans Peter Freyther90b87ea2013-10-27 10:38:31 +0100108 /* next TBF to handle resource is the next one */
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200109 pdch->next_ul_tfi = (tfi + 1) & 31;
Pau Espin Pedrol107e94c2021-03-24 17:06:15 +0100110 return tbf;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200111 }
112
Pau Espin Pedrol107e94c2021-03-24 17:06:15 +0100113 return NULL;
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200114}
115
Oliver Smithcfb63212019-09-05 17:13:33 +0200116struct msgb *sched_app_info(struct gprs_rlcmac_tbf *tbf) {
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100117 struct gprs_rlcmac_bts *bts;
Oliver Smithcfb63212019-09-05 17:13:33 +0200118 struct msgb *msg = NULL;
119
120 if (!tbf || !tbf->ms()->app_info_pending)
121 return NULL;
122
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100123 bts = tbf->bts;
Oliver Smithcfb63212019-09-05 17:13:33 +0200124
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100125 if (bts->app_info) {
Oliver Smithcfb63212019-09-05 17:13:33 +0200126 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Sending Packet Application Information message\n");
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100127 msg = msgb_copy(bts->app_info, "app_info_msg_sched");
Oliver Smithcfb63212019-09-05 17:13:33 +0200128 } else
129 LOGP(DRLCMACSCHED, LOGL_ERROR, "MS has app_info_pending flag set, but no Packet Application Information"
130 " message stored in BTS!\n");
131
132 tbf->ms()->app_info_pending = false;
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100133 bts->app_info_pending--;
Oliver Smithcfb63212019-09-05 17:13:33 +0200134
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100135 if (!bts->app_info_pending) {
Oliver Smithcfb63212019-09-05 17:13:33 +0200136 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Packet Application Information successfully sent to all MS with active"
137 " TBF\n");
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100138 msgb_free(bts->app_info);
139 bts->app_info = NULL;
Oliver Smithcfb63212019-09-05 17:13:33 +0200140 }
141 return msg;
142}
143
Pau Espin Pedrol3973eb52021-03-03 20:47:07 +0100144static struct msgb *sched_select_ctrl_msg(struct gprs_rlcmac_pdch *pdch, uint32_t fn,
145 uint8_t block_nr, struct tbf_sched_candidates *tbfs)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200146{
147 struct msgb *msg = NULL;
148 struct gprs_rlcmac_tbf *tbf = NULL;
Pau Espin Pedrold6b913f2020-12-15 18:55:16 +0100149 struct gprs_rlcmac_tbf *next_list[] = { tbfs->ul_ass,
150 tbfs->dl_ass,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100151 tbfs->ul_ack,
152 tbfs->nacc };
Pau Espin Pedrol3973eb52021-03-03 20:47:07 +0100153 uint8_t ts = pdch->ts_no;
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200154
Oliver Smithcfb63212019-09-05 17:13:33 +0200155 /* Send Packet Application Information first (ETWS primary notifications) */
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +0100156 msg = sched_app_info(tbfs->dl_ass);
Holger Hans Peter Freyther705653b2013-11-26 16:39:28 +0100157
Oliver Smithcfb63212019-09-05 17:13:33 +0200158 if (!msg) {
159 for (size_t i = 0; i < ARRAY_SIZE(next_list); ++i) {
Pau Espin Pedrold6b913f2020-12-15 18:55:16 +0100160 tbf = next_list[(pdch->next_ctrl_prio + i) % ARRAY_SIZE(next_list)];
Oliver Smithcfb63212019-09-05 17:13:33 +0200161 if (!tbf)
162 continue;
163
164 /*
165 * Assignments for the same direction have lower precedence,
166 * because they may kill the TBF when the CONTROL ACK is
167 * received, thus preventing the others from being processed.
168 */
Pau Espin Pedrol6ad11a62021-07-27 12:27:08 +0200169 if (tbf == tbfs->ul_ass && tbf->ul_ass_state_is(TBF_UL_ASS_SEND_ASS_REJ))
170 msg = tbf_ul_ass_create_rlcmac_msg(tbfs->ul_ass, fn, ts);
Pau Espin Pedrol34f61af2021-04-26 18:02:52 +0200171 else if (tbf == tbfs->ul_ass && tbf->direction == GPRS_RLCMAC_DL_TBF)
Pau Espin Pedrol6ad11a62021-07-27 12:27:08 +0200172 msg = tbf_ul_ass_create_rlcmac_msg(tbfs->ul_ass, fn, ts);
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +0100173 else if (tbf == tbfs->dl_ass && tbf->direction == GPRS_RLCMAC_UL_TBF)
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +0200174 msg = tbf_dl_ass_create_rlcmac_msg(tbfs->dl_ass, fn, ts);
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +0100175 else if (tbf == tbfs->ul_ack)
176 msg = tbfs->ul_ack->create_ul_ack(fn, ts);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100177 else if (tbf == tbfs->nacc) {
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100178 msg = ms_nacc_create_rlcmac_msg(tbf->ms(), tbf, fn, ts);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100179 }
Holger Hans Peter Freyther705653b2013-11-26 16:39:28 +0100180
Oliver Smithcfb63212019-09-05 17:13:33 +0200181 if (!msg) {
182 tbf = NULL;
183 continue;
184 }
185
Pau Espin Pedrold6b913f2020-12-15 18:55:16 +0100186 pdch->next_ctrl_prio = (pdch->next_ctrl_prio + 1) % ARRAY_SIZE(next_list);
Oliver Smithcfb63212019-09-05 17:13:33 +0200187 break;
Holger Hans Peter Freyther705653b2013-11-26 16:39:28 +0100188 }
Andreas Eversbergee31b782012-07-15 16:27:01 +0200189 }
Holger Hans Peter Freyther705653b2013-11-26 16:39:28 +0100190
Jacob Erlbeck1ff70c22016-02-01 19:42:09 +0100191 if (!msg) {
192 /*
193 * If one of these is left, the response (CONTROL ACK) from the
194 * MS will kill the current TBF, only one of them can be
195 * non-NULL
196 */
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +0100197 if (tbfs->dl_ass) {
198 tbf = tbfs->dl_ass;
Pau Espin Pedrol49a2f402021-07-27 17:33:07 +0200199 msg = tbf_dl_ass_create_rlcmac_msg(tbfs->dl_ass, fn, ts);
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +0100200 } else if (tbfs->ul_ass) {
201 tbf = tbfs->ul_ass;
Pau Espin Pedrol6ad11a62021-07-27 12:27:08 +0200202 msg = tbf_ul_ass_create_rlcmac_msg(tbfs->ul_ass, fn, ts);
Jacob Erlbeck1ff70c22016-02-01 19:42:09 +0100203 }
204 }
205
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200206 /* any message */
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200207 if (msg) {
Neels Hofmeyr3de6d062017-04-27 01:33:39 +0200208 if (!tbf) {
Pau Espin Pedrol3973eb52021-03-03 20:47:07 +0100209 LOGPDCH(pdch, DRLCMACSCHED, LOGL_ERROR, "FN=%" PRIu32
210 " Control message to be scheduled, but no TBF\n", fn);
Neels Hofmeyr3de6d062017-04-27 01:33:39 +0200211 msgb_free(msg);
212 return NULL;
213 }
Holger Hans Peter Freyther7a5f3c22013-11-26 13:08:12 +0100214 tbf->rotate_in_list();
Pau Espin Pedrol3973eb52021-03-03 20:47:07 +0100215 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "FN=%" PRIu32
216 " Scheduling control message at RTS for %s\n",
217 fn, tbf_name(tbf));
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200218 rate_ctr_inc(rate_ctr_group_get_ctr(tbf->ms()->ctrs, MS_CTR_DL_CTRL_MSG_SCHED));
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200219 return msg;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200220 }
Neels Hofmeyr3de6d062017-04-27 01:33:39 +0200221
222 /* schedule PACKET PAGING REQUEST, if any are pending */
Holger Hans Peter Freyther24e98d02013-10-19 18:15:44 +0200223 msg = pdch->packet_paging_request();
Andreas Eversberg07e97cf2012-08-07 16:00:56 +0200224 if (msg) {
Pau Espin Pedrol3973eb52021-03-03 20:47:07 +0100225 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "FN=%" PRIu32
226 " Scheduling paging request message at RTS\n", fn);
Andreas Eversberg07e97cf2012-08-07 16:00:56 +0200227 return msg;
228 }
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200229
Andreas Eversberg07e97cf2012-08-07 16:00:56 +0200230 return NULL;
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200231}
232
Maxd0532b52017-12-13 18:27:32 +0100233static inline enum tbf_dl_prio tbf_compute_priority(const struct gprs_rlcmac_bts *bts, struct gprs_rlcmac_dl_tbf *tbf,
234 uint8_t ts, uint32_t fn, int age)
235{
Pau Espin Pedrolb3f23972020-10-23 21:00:23 +0200236 const gprs_rlc_dl_window *w = static_cast<gprs_rlc_dl_window *>(tbf->window());
Pau Espin Pedrol924aaad2021-01-14 12:01:42 +0100237 unsigned long msecs_t3190 = osmo_tdef_get(the_pcu->T_defs, 3190, OSMO_TDEF_MS, -1);
238 unsigned long dl_tbf_idle_msec = osmo_tdef_get(the_pcu->T_defs, -2031, OSMO_TDEF_MS, -1);
Pau Espin Pedrol38cfa732019-09-09 12:55:01 +0200239 int age_thresh1 = msecs_to_frames(200);
Pau Espin Pedrol2b5c6292019-09-09 13:41:00 +0200240 int age_thresh2 = msecs_to_frames(OSMO_MIN(msecs_t3190/2, dl_tbf_idle_msec));
Maxd0532b52017-12-13 18:27:32 +0100241
242 if (tbf->is_control_ts(ts) && tbf->need_control_ts())
243 return DL_PRIO_CONTROL;
244
245 if (tbf->is_control_ts(ts) && age > age_thresh2 && age_thresh2 > 0)
246 return DL_PRIO_HIGH_AGE;
247
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200248 if ((tbf->state_is(TBF_ST_FLOW) && tbf->have_data()) || w->resend_needed() >= 0)
Maxd0532b52017-12-13 18:27:32 +0100249 return DL_PRIO_NEW_DATA;
250
251 if (tbf->is_control_ts(ts) && age > age_thresh1 && tbf->keep_open(fn))
252 return DL_PRIO_LOW_AGE;
253
254 if (!w->window_empty())
255 return DL_PRIO_SENT_DATA;
256
257 return DL_PRIO_NONE;
258}
259
Pau Espin Pedrol1a5439b2021-01-25 17:30:31 +0100260/* Check if next data block of a TBF can be encoded in GMSK [(M)CS1-4]. */
261static bool can_produce_gmsk_data_block_next(struct gprs_rlcmac_dl_tbf *tbf, enum tbf_dl_prio prio)
262{
263 const gprs_rlc_dl_window *w;
264
265 /* GPRS TBFs can always send GMSK */
266 if (!tbf->is_egprs_enabled())
267 return true;
268
269 switch (prio) {
270 case DL_PRIO_CONTROL:
271 /* Control blocks are always CS-1 */
272 return true;
273 case DL_PRIO_NEW_DATA:
274 /* We can send any new data (no block generated yet) using any
275 * MCS. However, we don't (yet) support resegmenting already
276 * sent blocks (NACKed blocks in this case) into lower MCS of
277 * the same family. See OS#4966 */
278 w = static_cast<gprs_rlc_dl_window *>(tbf->window());
279 return w->resend_needed() < 0;
280 default:
281 return false;
282 }
283}
284
Pau Espin Pedrol2ad15d52021-03-03 20:52:19 +0100285static struct msgb *sched_select_downlink(struct gprs_rlcmac_bts *bts, struct gprs_rlcmac_pdch *pdch, uint32_t fn,
286 uint8_t block_nr, enum mcs_kind req_mcs_kind, bool *is_egprs)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200287{
288 struct msgb *msg = NULL;
Jacob Erlbeck0eabffd2015-03-02 14:28:12 +0100289 struct gprs_rlcmac_dl_tbf *tbf, *prio_tbf = NULL;
Maxd0532b52017-12-13 18:27:32 +0100290 enum tbf_dl_prio prio, max_prio = DL_PRIO_NONE;
Pau Espin Pedrol2ad15d52021-03-03 20:52:19 +0100291 uint8_t ts = pdch->ts_no;
Jacob Erlbeck502bd1f2015-03-20 14:26:05 +0100292
Jacob Erlbeck0eabffd2015-03-02 14:28:12 +0100293 uint8_t i, tfi, prio_tfi;
Jacob Erlbeck502bd1f2015-03-20 14:26:05 +0100294 int age;
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200295
Holger Hans Peter Freyther90b87ea2013-10-27 10:38:31 +0100296 /* select downlink resource */
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200297 for (i = 0, tfi = pdch->next_dl_tfi; i < 32;
298 i++, tfi = (tfi + 1) & 31) {
Daniel Willmann1e0c6102014-06-04 14:56:09 +0200299 tbf = pdch->dl_tbf_by_tfi(tfi);
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200300 /* no TBF for this tfi, go next */
301 if (!tbf)
302 continue;
303 /* no DL TBF, go next */
304 if (tbf->direction != GPRS_RLCMAC_DL_TBF)
305 continue;
Holger Hans Peter Freyther483f77a2013-10-16 16:33:00 +0200306 /* no DL resources needed, go next */
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200307 if (tbf->state_is_not(TBF_ST_FLOW)
308 && tbf->state_is_not(TBF_ST_FINISHED))
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200309 continue;
310
Andreas Eversberga9be1542012-09-27 09:23:24 +0200311 /* waiting for CCCH IMM.ASS confirm */
Daniel Willmann7e994e32014-08-07 15:49:21 +0200312 if (tbf->m_wait_confirm)
Andreas Eversberga9be1542012-09-27 09:23:24 +0200313 continue;
314
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100315 /* If a GPRS (CS1-4) Dl block is required, skip EGPRS(_GSMK) tbfs: */
316 if (req_mcs_kind == GPRS && tbf->is_egprs_enabled())
317 continue;
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100318
Jacob Erlbeck502bd1f2015-03-20 14:26:05 +0100319 age = tbf->frames_since_last_poll(fn);
320
Jacob Erlbeck0eabffd2015-03-02 14:28:12 +0100321 /* compute priority */
Maxd0532b52017-12-13 18:27:32 +0100322 prio = tbf_compute_priority(bts, tbf, ts, fn, age);
323 if (prio == DL_PRIO_NONE)
Jacob Erlbeck502bd1f2015-03-20 14:26:05 +0100324 continue;
Jacob Erlbeck0eabffd2015-03-02 14:28:12 +0100325
Pau Espin Pedrol022f9e52020-11-16 18:49:39 +0100326 /* If a GPRS (CS1-4/MCS1-4) Dl block is required, downgrade MCS
327 * below instead of skipping. However, downgrade can only be
328 * done on new data BSNs (not yet sent) and control blocks. */
Pau Espin Pedrol1a5439b2021-01-25 17:30:31 +0100329 if (req_mcs_kind == EGPRS_GMSK && !can_produce_gmsk_data_block_next(tbf, prio)) {
Pau Espin Pedrol2ad15d52021-03-03 20:52:19 +0100330 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "FN=%" PRIu32
331 " Cannot downgrade EGPRS TBF with prio %d for %s\n",
332 fn, prio, tbf_name(tbf));
Pau Espin Pedrol022f9e52020-11-16 18:49:39 +0100333 continue;
334 }
335
Jacob Erlbeck0eabffd2015-03-02 14:28:12 +0100336 /* get the TBF with the highest priority */
337 if (prio > max_prio) {
338 prio_tfi = tfi;
339 prio_tbf = tbf;
340 max_prio = prio;
341 }
342 }
343
344 if (prio_tbf) {
Pau Espin Pedrol2ad15d52021-03-03 20:52:19 +0100345 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "FN=%" PRIu32
346 " Scheduling data message at RTS for DL TFI=%d prio=%d mcs_mode_restrict=%s\n",
347 fn, prio_tfi, max_prio, mode_name(req_mcs_kind));
Holger Hans Peter Freyther90b87ea2013-10-27 10:38:31 +0100348 /* next TBF to handle resource is the next one */
Jacob Erlbeck0eabffd2015-03-02 14:28:12 +0100349 pdch->next_dl_tfi = (prio_tfi + 1) & 31;
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200350 /* generate DL data block */
Pau Espin Pedrol022f9e52020-11-16 18:49:39 +0100351 msg = prio_tbf->create_dl_acked_block(fn, ts, req_mcs_kind);
Pau Espin Pedrol8f1701f2021-01-25 11:31:57 +0100352 *is_egprs = prio_tbf->is_egprs_enabled();
Andreas Eversberg3b7461c2012-07-20 11:19:59 +0200353 }
Andreas Eversberg39621c42012-06-27 15:36:27 +0200354
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200355 return msg;
356}
Holger Hans Peter Freyther40fc8f92013-11-23 23:03:14 +0100357
358static const uint8_t rlcmac_dl_idle[23] = {
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200359 0x47, /* control without optional header octets, no polling, USF=111 */
360 0x94, /* dummy downlink control message, paging mode 00 */
361 0x2b, /* no persistance level, 7 bits spare pattern */
362 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b,
363 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b
364};
365
Holger Hans Peter Freyther40fc8f92013-11-23 23:03:14 +0100366static struct msgb *sched_dummy(void)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200367{
368 struct msgb *msg;
369
370 msg = msgb_alloc(23, "rlcmac_dl_idle");
371 if (!msg)
372 return NULL;
373 memcpy(msgb_put(msg, 23), rlcmac_dl_idle, 23);
374
375 return msg;
376}
377
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100378static inline void tap_n_acc(const struct msgb *msg, struct gprs_rlcmac_bts *bts, uint8_t trx, uint8_t ts,
Max8bfa6592017-08-31 15:42:06 +0200379 uint32_t fn, enum pcu_gsmtap_category cat)
380{
381 if (!msg)
382 return;
383
384 switch(cat) {
385 case PCU_GSMTAP_C_DL_CTRL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100386 bts_do_rate_ctr_inc(bts, CTR_RLC_SENT_CONTROL);
387 bts_send_gsmtap(bts, PCU_GSMTAP_C_DL_CTRL, false, trx, ts, GSMTAP_CHANNEL_PACCH, fn, msg->data,
Max8bfa6592017-08-31 15:42:06 +0200388 msg->len);
389 break;
390 case PCU_GSMTAP_C_DL_DATA_GPRS:
Pau Espin Pedroldaa6c1c2020-11-16 14:25:06 +0100391 case PCU_GSMTAP_C_DL_DATA_EGPRS:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100392 bts_do_rate_ctr_inc(bts, CTR_RLC_SENT);
393 bts_send_gsmtap(bts, cat, false, trx, ts, GSMTAP_CHANNEL_PDTCH, fn, msg->data,
Max8bfa6592017-08-31 15:42:06 +0200394 msg->len);
395 break;
396 case PCU_GSMTAP_C_DL_DUMMY:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100397 bts_do_rate_ctr_inc(bts, CTR_RLC_SENT_DUMMY);
398 bts_send_gsmtap(bts, PCU_GSMTAP_C_DL_DUMMY, false, trx, ts, GSMTAP_CHANNEL_PACCH, fn, msg->data,
Max8bfa6592017-08-31 15:42:06 +0200399 msg->len);
400 break;
401 default:
402 break;
403 }
404}
405
Holger Hans Peter Freytherb19d7262013-10-17 18:12:18 +0200406int gprs_rlcmac_rcv_rts_block(struct gprs_rlcmac_bts *bts,
Max878bd1f2016-07-20 13:05:05 +0200407 uint8_t trx, uint8_t ts,
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200408 uint32_t fn, uint8_t block_nr)
409{
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200410 struct gprs_rlcmac_pdch *pdch;
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +0100411 struct tbf_sched_candidates tbf_cand = {0};
Pau Espin Pedrolfd1fbdb2021-03-11 16:59:50 +0100412 struct gprs_rlcmac_tbf *poll_tbf;
Pau Espin Pedrol4bab8672021-03-24 12:40:18 +0100413 struct gprs_rlcmac_ul_tbf *usf_tbf = NULL;
Pau Espin Pedrolfd1fbdb2021-03-11 16:59:50 +0100414 struct gprs_rlcmac_sba *sba;
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100415 uint8_t usf;
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200416 struct msgb *msg = NULL;
Pau Espin Pedrolfd1fbdb2021-03-11 16:59:50 +0100417 uint32_t poll_fn;
Pau Espin Pedrol4c5a7d32020-11-16 14:23:29 +0100418 enum pcu_gsmtap_category gsmtap_cat;
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100419 bool tx_is_egprs = false;
420 bool require_gprs_only;
421 enum mcs_kind req_mcs_kind; /* Restrict CS/MCS if DL Data block is to be sent */
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200422
423 if (trx >= 8 || ts >= 8)
424 return -EINVAL;
425 pdch = &bts->trx[trx].pdch[ts];
426
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200427 if (!pdch->is_enabled()) {
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200428 LOGP(DRLCMACSCHED, LOGL_ERROR, "Received RTS on disabled PDCH: "
429 "TRX=%d TS=%d\n", trx, ts);
430 return -EIO;
431 }
432
433 /* store last frame number of RTS */
434 pdch->last_rts_fn = fn;
435
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100436 /* require_gprs_only: Prioritize USF for GPRS-only MS here,
437 * since anyway we'll need to tx a Dl block with CS1-4 due to
438 * synchronization requirements. See 3GPP TS 03.64 version
439 * 8.12.0
440 */
441 require_gprs_only = (pdch->fn_without_cs14 == MS_RESYNC_NUM_FRAMES - 1);
442 if (require_gprs_only) {
443 LOGP(DRLCMACSCHED, LOGL_DEBUG, "TRX=%d TS=%d FN=%d "
444 "synchronization frame (every 18 frames), only CS1-4 allowed",
445 trx, ts, fn);
446 req_mcs_kind = GPRS; /* only GPRS CS1-4 allowed, all MS need to be able to decode it */
447 } else {
448 req_mcs_kind = EGPRS; /* all kinds are fine */
449 }
450
Pau Espin Pedrolfd1fbdb2021-03-11 16:59:50 +0100451 /* polling for next uplink block */
452 poll_fn = rts_next_fn(fn, block_nr);
Pau Espin Pedrol7bd92a32021-03-24 13:14:09 +0100453 /* check for sba */
454 if ((sba = pdch_ulc_get_sba(pdch->ulc, poll_fn))) {
455 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: "
456 "FN=%d block_nr=%d scheduling free USF for "
457 "single block allocation at FN=%d\n", fn, block_nr, sba->fn);
458 /* else, check uplink resource for polling */
459 } else if ((poll_tbf = pdch_ulc_get_tbf_poll(pdch->ulc, poll_fn))) {
Pau Espin Pedrol63254232021-05-11 14:04:33 +0200460 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: FN=%d "
461 "block_nr=%d scheduling free USF for polling at FN=%d of %s\n",
462 fn, block_nr, poll_fn, tbf_name(poll_tbf));
Pau Espin Pedrol7bd92a32021-03-24 13:14:09 +0100463 /* If POLL TBF is UL and already has a USF assigned on this TS,
464 * let's set its USF in the DL msg. This is not really needed,
465 * but it helps understand better the flow when looking at
466 * pcaps. */
Pau Espin Pedrolb5fece92021-08-23 16:58:04 +0200467 if (poll_tbf->direction == GPRS_RLCMAC_UL_TBF && as_ul_tbf(poll_tbf)->m_usf[ts] != USF_INVALID)
468 usf_tbf = as_ul_tbf(poll_tbf);
Pau Espin Pedrol7bd92a32021-03-24 13:14:09 +0100469 /* else, search for uplink tbf */
Pau Espin Pedrol63254232021-05-11 14:04:33 +0200470 } else if ((usf_tbf = sched_select_uplink(pdch, require_gprs_only))) {
471 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: FN=%d "
472 "block_nr=%d scheduling USF=%d for %s, expect answer on UL FN=%d\n",
473 fn, block_nr, usf_tbf->m_usf[pdch->ts_no], tbf_name(usf_tbf), poll_fn);
Pau Espin Pedrolc1f38c72021-03-24 14:06:15 +0100474 pdch_ulc_reserve_tbf_usf(pdch->ulc, poll_fn, usf_tbf);
Alexander Couzens9736d002016-05-18 16:41:03 +0200475 }
Pau Espin Pedrol7bd92a32021-03-24 13:14:09 +0100476 /* If MS selected for USF is GPRS-only, then it will only be
477 * able to read USF if dl block uses GMSK * (CS1-4, MCS1-4) */
478 if (usf_tbf && req_mcs_kind == EGPRS && ms_mode(usf_tbf->ms()) != EGPRS)
479 req_mcs_kind = EGPRS_GMSK;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200480
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +0200481 get_ctrl_msg_tbf_candidates(pdch, &tbf_cand);
Pau Espin Pedrolfd1fbdb2021-03-11 16:59:50 +0100482
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100483 /* Prio 1: select control message */
Pau Espin Pedrol3973eb52021-03-03 20:47:07 +0100484 if ((msg = sched_select_ctrl_msg(pdch, fn, block_nr, &tbf_cand))) {
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100485 gsmtap_cat = PCU_GSMTAP_C_DL_CTRL;
486 }
487 /* Prio 2: select data message for downlink */
Pau Espin Pedrol2ad15d52021-03-03 20:52:19 +0100488 else if((msg = sched_select_downlink(bts, pdch, fn, block_nr, req_mcs_kind, &tx_is_egprs))) {
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100489 gsmtap_cat = tx_is_egprs ? PCU_GSMTAP_C_DL_DATA_EGPRS :
490 PCU_GSMTAP_C_DL_DATA_GPRS;
491 }
Andreas Eversberg39621c42012-06-27 15:36:27 +0200492 /* Prio 3: send dummy contol message */
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100493 else if ((msg = sched_dummy())) {
Alexander Couzens2cb15472016-05-18 16:43:10 +0200494 /* increase counter */
Pau Espin Pedrol4c5a7d32020-11-16 14:23:29 +0100495 gsmtap_cat = PCU_GSMTAP_C_DL_DUMMY;
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100496 } else {
497 return -ENOMEM;
Alexander Couzens2cb15472016-05-18 16:43:10 +0200498 }
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200499
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100500 if (tx_is_egprs && pdch->has_gprs_only_tbf_attached()) {
501 pdch->fn_without_cs14 += 1;
502 } else {
503 pdch->fn_without_cs14 = 0;
504 }
505
Andreas Eversberg39621c42012-06-27 15:36:27 +0200506 /* msg is now available */
Vadim Yanitskiy82657622021-06-21 02:04:32 +0200507 bts_do_rate_ctr_add(bts, CTR_RLC_DL_BYTES, msgb_length(msg));
Andreas Eversberg39621c42012-06-27 15:36:27 +0200508
509 /* set USF */
Jacob Erlbeck580edbc2015-12-28 14:10:14 +0100510 OSMO_ASSERT(msgb_length(msg) > 0);
Pau Espin Pedrol4bab8672021-03-24 12:40:18 +0100511 usf = usf_tbf ? usf_tbf->m_usf[ts] : USF_UNUSED;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200512 msg->data[0] = (msg->data[0] & 0xf8) | usf;
513
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200514 /* Used to measure the leak rate, count all blocks */
515 gprs_bssgp_update_frames_sent();
516
Pau Espin Pedrol4c5a7d32020-11-16 14:23:29 +0100517 /* Send to GSMTAP */
518 tap_n_acc(msg, bts, trx, ts, fn, gsmtap_cat);
519
Andreas Eversberg39621c42012-06-27 15:36:27 +0200520 /* send PDTCH/PACCH to L1 */
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100521 pcu_l1if_tx_pdtch(msg, bts, trx, ts, bts->trx[trx].arfcn, fn, block_nr);
Andreas Eversberg39621c42012-06-27 15:36:27 +0200522
523 return 0;
524}