blob: 0a72e3b1ff179f5ee2c9cdd06981112a11eb5bdf [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.
Andreas Eversberg39621c42012-06-27 15:36:27 +020014 */
Pau Espin Pedrol488aa292019-09-25 17:48:35 +020015
Andreas Eversberg39621c42012-06-27 15:36:27 +020016#include <gprs_bssgp_pcu.h>
17#include <gprs_rlcmac.h>
18#include <pcu_l1_if.h>
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020019#include <bts.h>
Holger Hans Peter Freyther099535a2013-10-16 17:42:31 +020020#include <tbf.h>
Pau Espin Pedrol9d1cdb12019-09-25 17:47:02 +020021#include <tbf_ul.h>
Max1187a772018-01-26 13:31:42 +010022#include <gprs_debug.h>
23#include <gprs_ms.h>
24#include <rlc.h>
25#include <sba.h>
Max6dc90b82018-02-19 17:17:28 +010026#include <pdch.h>
Jacob Erlbeck502bd1f2015-03-20 14:26:05 +010027#include "pcu_utils.h"
28
Max1187a772018-01-26 13:31:42 +010029extern "C" {
30 #include <osmocom/core/gsmtap.h>
Pau Espin Pedrol106f2a02022-12-12 17:29:10 +010031 #include "nacc_fsm.h"
Max1187a772018-01-26 13:31:42 +010032}
33
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010034struct tbf_sched_candidates {
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010035 struct gprs_rlcmac_tbf *ul_ass;
36 struct gprs_rlcmac_tbf *dl_ass;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010037 struct gprs_rlcmac_tbf *nacc;
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010038 struct gprs_rlcmac_ul_tbf *ul_ack;
39};
40
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +020041static void get_ctrl_msg_tbf_candidates(const struct gprs_rlcmac_pdch *pdch,
Pau Espin Pedrol48517622021-04-30 14:15:46 +020042 struct tbf_sched_candidates *tbf_cand)
Andreas Eversberg39621c42012-06-27 15:36:27 +020043{
Daniel Willmann4f3c4202014-08-07 14:58:59 +020044 struct gprs_rlcmac_ul_tbf *ul_tbf;
45 struct gprs_rlcmac_dl_tbf *dl_tbf;
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010046 struct llist_item *pos;
Andreas Eversberge6228b32012-07-03 13:36:03 +020047
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +020048 llist_for_each_entry(pos, &pdch->trx->ul_tbfs, list) {
Pau Espin Pedrolcc30b052022-10-27 15:25:55 +020049 ul_tbf = tbf_as_ul_tbf((struct gprs_rlcmac_tbf *)pos->entry);
Pau Espin Pedrolb5fece92021-08-23 16:58:04 +020050 OSMO_ASSERT(ul_tbf);
Pau Espin Pedrol106f2a02022-12-12 17:29:10 +010051 if (tbf_ul_ack_rts(ul_tbf, pdch))
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010052 tbf_cand->ul_ack = ul_tbf;
Pau Espin Pedrol106f2a02022-12-12 17:29:10 +010053 if (tbf_dl_ass_rts(ul_tbf, pdch))
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010054 tbf_cand->dl_ass = ul_tbf;
Pau Espin Pedrol106f2a02022-12-12 17:29:10 +010055 if (tbf_ul_ass_rts(ul_tbf, pdch))
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010056 tbf_cand->ul_ass = ul_tbf;
Pau Espin Pedrol106f2a02022-12-12 17:29:10 +010057 if (tbf_nacc_rts(ul_tbf, pdch))
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010058 tbf_cand->nacc = ul_tbf;
Max5a6bcfb2017-09-01 14:36:44 +020059/* FIXME: Is this supposed to be fair? The last TBF for each wins? Maybe use llist_add_tail and skip once we have all
60states? */
Andreas Eversberg39621c42012-06-27 15:36:27 +020061 }
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +020062 llist_for_each_entry(pos, &pdch->trx->dl_tbfs, list) {
Pau Espin Pedrolcc30b052022-10-27 15:25:55 +020063 dl_tbf = tbf_as_dl_tbf((struct gprs_rlcmac_tbf *)pos->entry);
Pau Espin Pedrolb5fece92021-08-23 16:58:04 +020064 OSMO_ASSERT(dl_tbf);
Pau Espin Pedrol106f2a02022-12-12 17:29:10 +010065 if (tbf_dl_ass_rts(dl_tbf, pdch))
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010066 tbf_cand->dl_ass = dl_tbf;
Pau Espin Pedrol106f2a02022-12-12 17:29:10 +010067 if (tbf_ul_ass_rts(dl_tbf, pdch))
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +010068 tbf_cand->ul_ass = dl_tbf;
Pau Espin Pedrol106f2a02022-12-12 17:29:10 +010069 if (tbf_nacc_rts(dl_tbf, pdch))
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010070 tbf_cand->nacc = dl_tbf;
Andreas Eversberg592e04a2012-07-15 06:25:37 +020071 }
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020072}
73
Pau Espin Pedrol63254232021-05-11 14:04:33 +020074static struct gprs_rlcmac_ul_tbf *sched_select_uplink(struct gprs_rlcmac_pdch *pdch, bool require_gprs_only)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020075{
Pau Espin Pedrol107e94c2021-03-24 17:06:15 +010076 struct gprs_rlcmac_ul_tbf *tbf;
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020077 uint8_t i, tfi;
78
Holger Hans Peter Freyther90b87ea2013-10-27 10:38:31 +010079 /* select uplink resource */
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020080 for (i = 0, tfi = pdch->next_ul_tfi; i < 32;
81 i++, tfi = (tfi + 1) & 31) {
Daniel Willmann1e0c6102014-06-04 14:56:09 +020082 tbf = pdch->ul_tbf_by_tfi(tfi);
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020083 /* no TBF for this tfi, go next */
84 if (!tbf)
85 continue;
Holger Hans Peter Freyther483f77a2013-10-16 16:33:00 +020086 /* no UL resources needed, go next */
87 /* we don't need to give resources in FINISHED state,
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020088 * because we have received all blocks and only poll
89 * for packet control ack. */
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +020090 if (tbf->state_is_not(TBF_ST_FLOW))
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020091 continue;
92
Pau Espin Pedrol22862882020-11-16 17:35:10 +010093 if (require_gprs_only && tbf->is_egprs_enabled())
94 continue;
95
Pau Espin Pedrol63254232021-05-11 14:04:33 +020096 /* use this USF (tbf) */
Holger Hans Peter Freyther90b87ea2013-10-27 10:38:31 +010097 /* next TBF to handle resource is the next one */
Andreas Eversbergd6a8db62012-07-25 08:38:21 +020098 pdch->next_ul_tfi = (tfi + 1) & 31;
Pau Espin Pedrol107e94c2021-03-24 17:06:15 +010099 return tbf;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200100 }
101
Pau Espin Pedrol107e94c2021-03-24 17:06:15 +0100102 return NULL;
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200103}
104
Oliver Smithcfb63212019-09-05 17:13:33 +0200105struct msgb *sched_app_info(struct gprs_rlcmac_tbf *tbf) {
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100106 struct gprs_rlcmac_bts *bts;
Oliver Smithcfb63212019-09-05 17:13:33 +0200107 struct msgb *msg = NULL;
108
109 if (!tbf || !tbf->ms()->app_info_pending)
110 return NULL;
111
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100112 bts = tbf->bts;
Oliver Smithcfb63212019-09-05 17:13:33 +0200113
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100114 if (bts->app_info) {
Oliver Smithcfb63212019-09-05 17:13:33 +0200115 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Sending Packet Application Information message\n");
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100116 msg = msgb_copy(bts->app_info, "app_info_msg_sched");
Oliver Smithcfb63212019-09-05 17:13:33 +0200117 } else
118 LOGP(DRLCMACSCHED, LOGL_ERROR, "MS has app_info_pending flag set, but no Packet Application Information"
119 " message stored in BTS!\n");
120
121 tbf->ms()->app_info_pending = false;
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100122 bts->app_info_pending--;
Oliver Smithcfb63212019-09-05 17:13:33 +0200123
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100124 if (!bts->app_info_pending) {
Oliver Smithcfb63212019-09-05 17:13:33 +0200125 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Packet Application Information successfully sent to all MS with active"
126 " TBF\n");
Pau Espin Pedrol0ece97d2021-01-18 12:53:54 +0100127 msgb_free(bts->app_info);
128 bts->app_info = NULL;
Oliver Smithcfb63212019-09-05 17:13:33 +0200129 }
130 return msg;
131}
132
Pau Espin Pedrol3973eb52021-03-03 20:47:07 +0100133static struct msgb *sched_select_ctrl_msg(struct gprs_rlcmac_pdch *pdch, uint32_t fn,
134 uint8_t block_nr, struct tbf_sched_candidates *tbfs)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200135{
136 struct msgb *msg = NULL;
137 struct gprs_rlcmac_tbf *tbf = NULL;
Pau Espin Pedrold6b913f2020-12-15 18:55:16 +0100138 struct gprs_rlcmac_tbf *next_list[] = { tbfs->ul_ass,
139 tbfs->dl_ass,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100140 tbfs->ul_ack,
141 tbfs->nacc };
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200142
Oliver Smithcfb63212019-09-05 17:13:33 +0200143 /* Send Packet Application Information first (ETWS primary notifications) */
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +0100144 msg = sched_app_info(tbfs->dl_ass);
Holger Hans Peter Freyther705653b2013-11-26 16:39:28 +0100145
Oliver Smithcfb63212019-09-05 17:13:33 +0200146 if (!msg) {
147 for (size_t i = 0; i < ARRAY_SIZE(next_list); ++i) {
Pau Espin Pedrold6b913f2020-12-15 18:55:16 +0100148 tbf = next_list[(pdch->next_ctrl_prio + i) % ARRAY_SIZE(next_list)];
Oliver Smithcfb63212019-09-05 17:13:33 +0200149 if (!tbf)
150 continue;
151
152 /*
153 * Assignments for the same direction have lower precedence,
154 * because they may kill the TBF when the CONTROL ACK is
155 * received, thus preventing the others from being processed.
156 */
Pau Espin Pedrol6ad11a62021-07-27 12:27:08 +0200157 if (tbf == tbfs->ul_ass && tbf->ul_ass_state_is(TBF_UL_ASS_SEND_ASS_REJ))
Pau Espin Pedrol5ba3ef92022-12-12 18:02:25 +0100158 msg = tbf_ul_ass_create_rlcmac_msg(tbfs->ul_ass, pdch, fn);
Pau Espin Pedrol34f61af2021-04-26 18:02:52 +0200159 else if (tbf == tbfs->ul_ass && tbf->direction == GPRS_RLCMAC_DL_TBF)
Pau Espin Pedrol5ba3ef92022-12-12 18:02:25 +0100160 msg = tbf_ul_ass_create_rlcmac_msg(tbfs->ul_ass, pdch, fn);
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +0100161 else if (tbf == tbfs->dl_ass && tbf->direction == GPRS_RLCMAC_UL_TBF)
Pau Espin Pedrol5ba3ef92022-12-12 18:02:25 +0100162 msg = tbf_dl_ass_create_rlcmac_msg(tbfs->dl_ass, pdch, fn);
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +0100163 else if (tbf == tbfs->ul_ack)
Pau Espin Pedrol5ba3ef92022-12-12 18:02:25 +0100164 msg = tbf_ul_ack_create_rlcmac_msg(tbfs->ul_ack, pdch, fn);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100165 else if (tbf == tbfs->nacc) {
Pau Espin Pedrol5ba3ef92022-12-12 18:02:25 +0100166 msg = ms_nacc_create_rlcmac_msg(tbf->ms(), tbf, pdch, fn);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100167 }
Holger Hans Peter Freyther705653b2013-11-26 16:39:28 +0100168
Oliver Smithcfb63212019-09-05 17:13:33 +0200169 if (!msg) {
170 tbf = NULL;
171 continue;
172 }
173
Pau Espin Pedrold6b913f2020-12-15 18:55:16 +0100174 pdch->next_ctrl_prio = (pdch->next_ctrl_prio + 1) % ARRAY_SIZE(next_list);
Oliver Smithcfb63212019-09-05 17:13:33 +0200175 break;
Holger Hans Peter Freyther705653b2013-11-26 16:39:28 +0100176 }
Andreas Eversbergee31b782012-07-15 16:27:01 +0200177 }
Holger Hans Peter Freyther705653b2013-11-26 16:39:28 +0100178
Jacob Erlbeck1ff70c22016-02-01 19:42:09 +0100179 if (!msg) {
180 /*
181 * If one of these is left, the response (CONTROL ACK) from the
182 * MS will kill the current TBF, only one of them can be
183 * non-NULL
184 */
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +0100185 if (tbfs->dl_ass) {
186 tbf = tbfs->dl_ass;
Pau Espin Pedrol5ba3ef92022-12-12 18:02:25 +0100187 msg = tbf_dl_ass_create_rlcmac_msg(tbfs->dl_ass, pdch, fn);
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +0100188 } else if (tbfs->ul_ass) {
189 tbf = tbfs->ul_ass;
Pau Espin Pedrol5ba3ef92022-12-12 18:02:25 +0100190 msg = tbf_ul_ass_create_rlcmac_msg(tbfs->ul_ass, pdch, fn);
Jacob Erlbeck1ff70c22016-02-01 19:42:09 +0100191 }
192 }
193
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200194 /* any message */
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200195 if (msg) {
Neels Hofmeyr3de6d062017-04-27 01:33:39 +0200196 if (!tbf) {
Pau Espin Pedrol3973eb52021-03-03 20:47:07 +0100197 LOGPDCH(pdch, DRLCMACSCHED, LOGL_ERROR, "FN=%" PRIu32
198 " Control message to be scheduled, but no TBF\n", fn);
Neels Hofmeyr3de6d062017-04-27 01:33:39 +0200199 msgb_free(msg);
200 return NULL;
201 }
Holger Hans Peter Freyther7a5f3c22013-11-26 13:08:12 +0100202 tbf->rotate_in_list();
Pau Espin Pedrol3973eb52021-03-03 20:47:07 +0100203 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "FN=%" PRIu32
204 " Scheduling control message at RTS for %s\n",
205 fn, tbf_name(tbf));
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200206 rate_ctr_inc(rate_ctr_group_get_ctr(tbf->ms()->ctrs, MS_CTR_DL_CTRL_MSG_SCHED));
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200207 return msg;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200208 }
Neels Hofmeyr3de6d062017-04-27 01:33:39 +0200209
210 /* schedule PACKET PAGING REQUEST, if any are pending */
Holger Hans Peter Freyther24e98d02013-10-19 18:15:44 +0200211 msg = pdch->packet_paging_request();
Andreas Eversberg07e97cf2012-08-07 16:00:56 +0200212 if (msg) {
Pau Espin Pedrol3973eb52021-03-03 20:47:07 +0100213 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "FN=%" PRIu32
214 " Scheduling paging request message at RTS\n", fn);
Andreas Eversberg07e97cf2012-08-07 16:00:56 +0200215 return msg;
216 }
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200217
Andreas Eversberg07e97cf2012-08-07 16:00:56 +0200218 return NULL;
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200219}
220
Maxd0532b52017-12-13 18:27:32 +0100221static inline enum tbf_dl_prio tbf_compute_priority(const struct gprs_rlcmac_bts *bts, struct gprs_rlcmac_dl_tbf *tbf,
Pau Espin Pedrol7e1fec22022-12-12 16:56:32 +0100222 const struct gprs_rlcmac_pdch *pdch, uint32_t fn, int age)
Maxd0532b52017-12-13 18:27:32 +0100223{
Pau Espin Pedrolb3f23972020-10-23 21:00:23 +0200224 const gprs_rlc_dl_window *w = static_cast<gprs_rlc_dl_window *>(tbf->window());
Pau Espin Pedrol924aaad2021-01-14 12:01:42 +0100225 unsigned long msecs_t3190 = osmo_tdef_get(the_pcu->T_defs, 3190, OSMO_TDEF_MS, -1);
226 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 +0200227 int age_thresh1 = msecs_to_frames(200);
Pau Espin Pedrol2b5c6292019-09-09 13:41:00 +0200228 int age_thresh2 = msecs_to_frames(OSMO_MIN(msecs_t3190/2, dl_tbf_idle_msec));
Pau Espin Pedrol7e1fec22022-12-12 16:56:32 +0100229 const bool is_control_ts = tbf_is_control_ts(tbf, pdch);
Maxd0532b52017-12-13 18:27:32 +0100230
Pau Espin Pedrol7e1fec22022-12-12 16:56:32 +0100231 if (is_control_ts && tbf->need_poll_for_dl_ack_nack())
Maxd0532b52017-12-13 18:27:32 +0100232 return DL_PRIO_CONTROL;
233
Pau Espin Pedrol7e1fec22022-12-12 16:56:32 +0100234 if (is_control_ts && age > age_thresh2 && age_thresh2 > 0)
Maxd0532b52017-12-13 18:27:32 +0100235 return DL_PRIO_HIGH_AGE;
236
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200237 if ((tbf->state_is(TBF_ST_FLOW) && tbf->have_data()) || w->resend_needed() >= 0)
Maxd0532b52017-12-13 18:27:32 +0100238 return DL_PRIO_NEW_DATA;
239
Pau Espin Pedrol7e1fec22022-12-12 16:56:32 +0100240 if (is_control_ts && age > age_thresh1 && tbf->keep_open(fn))
Maxd0532b52017-12-13 18:27:32 +0100241 return DL_PRIO_LOW_AGE;
242
243 if (!w->window_empty())
244 return DL_PRIO_SENT_DATA;
245
246 return DL_PRIO_NONE;
247}
248
Pau Espin Pedrol1a5439b2021-01-25 17:30:31 +0100249/* Check if next data block of a TBF can be encoded in GMSK [(M)CS1-4]. */
250static bool can_produce_gmsk_data_block_next(struct gprs_rlcmac_dl_tbf *tbf, enum tbf_dl_prio prio)
251{
252 const gprs_rlc_dl_window *w;
253
254 /* GPRS TBFs can always send GMSK */
255 if (!tbf->is_egprs_enabled())
256 return true;
257
258 switch (prio) {
259 case DL_PRIO_CONTROL:
260 /* Control blocks are always CS-1 */
261 return true;
262 case DL_PRIO_NEW_DATA:
263 /* We can send any new data (no block generated yet) using any
264 * MCS. However, we don't (yet) support resegmenting already
265 * sent blocks (NACKed blocks in this case) into lower MCS of
266 * the same family. See OS#4966 */
267 w = static_cast<gprs_rlc_dl_window *>(tbf->window());
268 return w->resend_needed() < 0;
269 default:
270 return false;
271 }
272}
273
Pau Espin Pedrole0808082021-10-11 17:42:33 +0200274static struct msgb *sched_select_dl_data_msg(struct gprs_rlcmac_bts *bts, struct gprs_rlcmac_pdch *pdch,
275 uint32_t fn, uint8_t block_nr, enum mcs_kind req_mcs_kind,
276 bool *is_egprs)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200277{
278 struct msgb *msg = NULL;
Jacob Erlbeck0eabffd2015-03-02 14:28:12 +0100279 struct gprs_rlcmac_dl_tbf *tbf, *prio_tbf = NULL;
Maxd0532b52017-12-13 18:27:32 +0100280 enum tbf_dl_prio prio, max_prio = DL_PRIO_NONE;
Jacob Erlbeck502bd1f2015-03-20 14:26:05 +0100281
Jacob Erlbeck0eabffd2015-03-02 14:28:12 +0100282 uint8_t i, tfi, prio_tfi;
Jacob Erlbeck502bd1f2015-03-20 14:26:05 +0100283 int age;
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200284
Holger Hans Peter Freyther90b87ea2013-10-27 10:38:31 +0100285 /* select downlink resource */
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200286 for (i = 0, tfi = pdch->next_dl_tfi; i < 32;
287 i++, tfi = (tfi + 1) & 31) {
Daniel Willmann1e0c6102014-06-04 14:56:09 +0200288 tbf = pdch->dl_tbf_by_tfi(tfi);
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200289 /* no TBF for this tfi, go next */
290 if (!tbf)
291 continue;
292 /* no DL TBF, go next */
293 if (tbf->direction != GPRS_RLCMAC_DL_TBF)
294 continue;
Holger Hans Peter Freyther483f77a2013-10-16 16:33:00 +0200295 /* no DL resources needed, go next */
Pau Espin Pedroldc2aaac2021-05-14 12:50:46 +0200296 if (tbf->state_is_not(TBF_ST_FLOW)
297 && tbf->state_is_not(TBF_ST_FINISHED))
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200298 continue;
299
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100300 /* If a GPRS (CS1-4) Dl block is required, skip EGPRS(_GSMK) tbfs: */
301 if (req_mcs_kind == GPRS && tbf->is_egprs_enabled())
302 continue;
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100303
Jacob Erlbeck502bd1f2015-03-20 14:26:05 +0100304 age = tbf->frames_since_last_poll(fn);
305
Jacob Erlbeck0eabffd2015-03-02 14:28:12 +0100306 /* compute priority */
Pau Espin Pedrol7e1fec22022-12-12 16:56:32 +0100307 prio = tbf_compute_priority(bts, tbf, pdch, fn, age);
Maxd0532b52017-12-13 18:27:32 +0100308 if (prio == DL_PRIO_NONE)
Jacob Erlbeck502bd1f2015-03-20 14:26:05 +0100309 continue;
Jacob Erlbeck0eabffd2015-03-02 14:28:12 +0100310
Pau Espin Pedrol022f9e52020-11-16 18:49:39 +0100311 /* If a GPRS (CS1-4/MCS1-4) Dl block is required, downgrade MCS
312 * below instead of skipping. However, downgrade can only be
313 * done on new data BSNs (not yet sent) and control blocks. */
Pau Espin Pedrol1a5439b2021-01-25 17:30:31 +0100314 if (req_mcs_kind == EGPRS_GMSK && !can_produce_gmsk_data_block_next(tbf, prio)) {
Pau Espin Pedrol2ad15d52021-03-03 20:52:19 +0100315 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "FN=%" PRIu32
316 " Cannot downgrade EGPRS TBF with prio %d for %s\n",
317 fn, prio, tbf_name(tbf));
Pau Espin Pedrol022f9e52020-11-16 18:49:39 +0100318 continue;
319 }
320
Jacob Erlbeck0eabffd2015-03-02 14:28:12 +0100321 /* get the TBF with the highest priority */
322 if (prio > max_prio) {
323 prio_tfi = tfi;
324 prio_tbf = tbf;
325 max_prio = prio;
326 }
327 }
328
329 if (prio_tbf) {
Pau Espin Pedrol2ad15d52021-03-03 20:52:19 +0100330 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "FN=%" PRIu32
331 " Scheduling data message at RTS for DL TFI=%d prio=%d mcs_mode_restrict=%s\n",
332 fn, prio_tfi, max_prio, mode_name(req_mcs_kind));
Holger Hans Peter Freyther90b87ea2013-10-27 10:38:31 +0100333 /* next TBF to handle resource is the next one */
Jacob Erlbeck0eabffd2015-03-02 14:28:12 +0100334 pdch->next_dl_tfi = (prio_tfi + 1) & 31;
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200335 /* generate DL data block */
Pau Espin Pedrold29a1432022-12-15 18:57:35 +0100336 msg = prio_tbf->create_dl_acked_block(fn, pdch, req_mcs_kind);
Pau Espin Pedrol8f1701f2021-01-25 11:31:57 +0100337 *is_egprs = prio_tbf->is_egprs_enabled();
Andreas Eversberg3b7461c2012-07-20 11:19:59 +0200338 }
Andreas Eversberg39621c42012-06-27 15:36:27 +0200339
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200340 return msg;
341}
Holger Hans Peter Freyther40fc8f92013-11-23 23:03:14 +0100342
343static const uint8_t rlcmac_dl_idle[23] = {
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200344 0x47, /* control without optional header octets, no polling, USF=111 */
345 0x94, /* dummy downlink control message, paging mode 00 */
346 0x2b, /* no persistance level, 7 bits spare pattern */
347 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b,
348 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b
349};
350
Holger Hans Peter Freyther40fc8f92013-11-23 23:03:14 +0100351static struct msgb *sched_dummy(void)
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200352{
353 struct msgb *msg;
354
355 msg = msgb_alloc(23, "rlcmac_dl_idle");
356 if (!msg)
357 return NULL;
358 memcpy(msgb_put(msg, 23), rlcmac_dl_idle, 23);
359
360 return msg;
361}
362
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100363static 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 +0200364 uint32_t fn, enum pcu_gsmtap_category cat)
365{
366 if (!msg)
367 return;
368
369 switch(cat) {
370 case PCU_GSMTAP_C_DL_CTRL:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100371 bts_do_rate_ctr_inc(bts, CTR_RLC_SENT_CONTROL);
372 bts_send_gsmtap(bts, PCU_GSMTAP_C_DL_CTRL, false, trx, ts, GSMTAP_CHANNEL_PACCH, fn, msg->data,
Max8bfa6592017-08-31 15:42:06 +0200373 msg->len);
374 break;
375 case PCU_GSMTAP_C_DL_DATA_GPRS:
Pau Espin Pedroldaa6c1c2020-11-16 14:25:06 +0100376 case PCU_GSMTAP_C_DL_DATA_EGPRS:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100377 bts_do_rate_ctr_inc(bts, CTR_RLC_SENT);
378 bts_send_gsmtap(bts, cat, false, trx, ts, GSMTAP_CHANNEL_PDTCH, fn, msg->data,
Max8bfa6592017-08-31 15:42:06 +0200379 msg->len);
380 break;
381 case PCU_GSMTAP_C_DL_DUMMY:
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100382 bts_do_rate_ctr_inc(bts, CTR_RLC_SENT_DUMMY);
383 bts_send_gsmtap(bts, PCU_GSMTAP_C_DL_DUMMY, false, trx, ts, GSMTAP_CHANNEL_PACCH, fn, msg->data,
Max8bfa6592017-08-31 15:42:06 +0200384 msg->len);
385 break;
386 default:
387 break;
388 }
389}
390
Holger Hans Peter Freytherb19d7262013-10-17 18:12:18 +0200391int gprs_rlcmac_rcv_rts_block(struct gprs_rlcmac_bts *bts,
Max878bd1f2016-07-20 13:05:05 +0200392 uint8_t trx, uint8_t ts,
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200393 uint32_t fn, uint8_t block_nr)
394{
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200395 struct gprs_rlcmac_pdch *pdch;
Pau Espin Pedrolb77a2c92020-12-15 18:46:33 +0100396 struct tbf_sched_candidates tbf_cand = {0};
Pau Espin Pedrolfd1fbdb2021-03-11 16:59:50 +0100397 struct gprs_rlcmac_tbf *poll_tbf;
Pau Espin Pedrol4bab8672021-03-24 12:40:18 +0100398 struct gprs_rlcmac_ul_tbf *usf_tbf = NULL;
Pau Espin Pedrolfd1fbdb2021-03-11 16:59:50 +0100399 struct gprs_rlcmac_sba *sba;
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100400 uint8_t usf;
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200401 struct msgb *msg = NULL;
Pau Espin Pedrolfd1fbdb2021-03-11 16:59:50 +0100402 uint32_t poll_fn;
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200403 enum pcu_gsmtap_category gsmtap_cat = PCU_GSMTAP_C_DL_DUMMY; /* init: make gcc happy */
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100404 bool tx_is_egprs = false;
405 bool require_gprs_only;
406 enum mcs_kind req_mcs_kind; /* Restrict CS/MCS if DL Data block is to be sent */
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200407
408 if (trx >= 8 || ts >= 8)
409 return -EINVAL;
410 pdch = &bts->trx[trx].pdch[ts];
411
Holger Hans Peter Freyther17b0d832013-10-19 17:37:48 +0200412 if (!pdch->is_enabled()) {
Pau Espin Pedrol25d60ca2021-08-31 16:50:32 +0200413 LOGPDCH(pdch, DRLCMACSCHED, LOGL_INFO, "Received RTS on disabled TS\n");
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200414 return -EIO;
415 }
416
417 /* store last frame number of RTS */
418 pdch->last_rts_fn = fn;
419
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100420 /* require_gprs_only: Prioritize USF for GPRS-only MS here,
421 * since anyway we'll need to tx a Dl block with CS1-4 due to
422 * synchronization requirements. See 3GPP TS 03.64 version
423 * 8.12.0
424 */
425 require_gprs_only = (pdch->fn_without_cs14 == MS_RESYNC_NUM_FRAMES - 1);
426 if (require_gprs_only) {
427 LOGP(DRLCMACSCHED, LOGL_DEBUG, "TRX=%d TS=%d FN=%d "
428 "synchronization frame (every 18 frames), only CS1-4 allowed",
429 trx, ts, fn);
430 req_mcs_kind = GPRS; /* only GPRS CS1-4 allowed, all MS need to be able to decode it */
431 } else {
432 req_mcs_kind = EGPRS; /* all kinds are fine */
433 }
434
Pau Espin Pedrolfd1fbdb2021-03-11 16:59:50 +0100435 /* polling for next uplink block */
436 poll_fn = rts_next_fn(fn, block_nr);
Pau Espin Pedrol7bd92a32021-03-24 13:14:09 +0100437 /* check for sba */
438 if ((sba = pdch_ulc_get_sba(pdch->ulc, poll_fn))) {
439 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: "
440 "FN=%d block_nr=%d scheduling free USF for "
441 "single block allocation at FN=%d\n", fn, block_nr, sba->fn);
442 /* else, check uplink resource for polling */
443 } else if ((poll_tbf = pdch_ulc_get_tbf_poll(pdch->ulc, poll_fn))) {
Pau Espin Pedrol63254232021-05-11 14:04:33 +0200444 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: FN=%d "
445 "block_nr=%d scheduling free USF for polling at FN=%d of %s\n",
446 fn, block_nr, poll_fn, tbf_name(poll_tbf));
Pau Espin Pedrol7bd92a32021-03-24 13:14:09 +0100447 /* If POLL TBF is UL and already has a USF assigned on this TS,
448 * let's set its USF in the DL msg. This is not really needed,
449 * but it helps understand better the flow when looking at
450 * pcaps. */
Pau Espin Pedrolcc30b052022-10-27 15:25:55 +0200451 if (poll_tbf->direction == GPRS_RLCMAC_UL_TBF && tbf_as_ul_tbf(poll_tbf)->m_usf[ts] != USF_INVALID)
452 usf_tbf = tbf_as_ul_tbf(poll_tbf);
Pau Espin Pedrol7bd92a32021-03-24 13:14:09 +0100453 /* else, search for uplink tbf */
Pau Espin Pedrol63254232021-05-11 14:04:33 +0200454 } else if ((usf_tbf = sched_select_uplink(pdch, require_gprs_only))) {
455 LOGPDCH(pdch, DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: FN=%d "
456 "block_nr=%d scheduling USF=%d for %s, expect answer on UL FN=%d\n",
457 fn, block_nr, usf_tbf->m_usf[pdch->ts_no], tbf_name(usf_tbf), poll_fn);
Pau Espin Pedrolc1f38c72021-03-24 14:06:15 +0100458 pdch_ulc_reserve_tbf_usf(pdch->ulc, poll_fn, usf_tbf);
Alexander Couzens9736d002016-05-18 16:41:03 +0200459 }
Pau Espin Pedrol7bd92a32021-03-24 13:14:09 +0100460 /* If MS selected for USF is GPRS-only, then it will only be
Pau Espin Pedrolc4fe1f92021-08-25 14:35:27 +0200461 * able to read USF if dl block uses GMSK (CS1-4, MCS1-4) */
Pau Espin Pedrol7bd92a32021-03-24 13:14:09 +0100462 if (usf_tbf && req_mcs_kind == EGPRS && ms_mode(usf_tbf->ms()) != EGPRS)
463 req_mcs_kind = EGPRS_GMSK;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200464
Pau Espin Pedrol1a1557a2021-05-13 18:39:36 +0200465 get_ctrl_msg_tbf_candidates(pdch, &tbf_cand);
Pau Espin Pedrolfd1fbdb2021-03-11 16:59:50 +0100466
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100467 /* Prio 1: select control message */
Pau Espin Pedrol3973eb52021-03-03 20:47:07 +0100468 if ((msg = sched_select_ctrl_msg(pdch, fn, block_nr, &tbf_cand))) {
Vadim Yanitskiyc2eb4b72022-10-04 02:57:00 +0700469 gsmtap_cat = PCU_GSMTAP_C_DL_CTRL;
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100470 }
471 /* Prio 2: select data message for downlink */
Pau Espin Pedrole0808082021-10-11 17:42:33 +0200472 else if((msg = sched_select_dl_data_msg(bts, pdch, fn, block_nr, req_mcs_kind, &tx_is_egprs))) {
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100473 gsmtap_cat = tx_is_egprs ? PCU_GSMTAP_C_DL_DATA_EGPRS :
474 PCU_GSMTAP_C_DL_DATA_GPRS;
475 }
Pau Espin Pedrola89a2382021-08-25 16:49:48 +0200476 /* Prio 3: send dummy control message if need to poll or USF */
477 else {
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200478 /* If there's no TBF attached to this PDCH, we can submit an empty
479 * data_req since there's nothing to transmit nor to poll/USF. This
480 * way we help BTS energy saving (on TRX!=C0) by sending nothing
Pau Espin Pedrola89a2382021-08-25 16:49:48 +0200481 * instead of a dummy block. The early return is done here and
482 * not at the start of the function because the condition below
483 * (num_tbfs==0) may not be enough, because temporary dummy TBFs
484 * created to send Imm Ass Rej (see handle_tbf_reject()) don't
485 * have a TFI assigned and hence are not attached to the PDCH
486 * TS, so they don't show up in the count below.
487 */
488 const unsigned num_tbfs = pdch->num_tbfs(GPRS_RLCMAC_DL_TBF)
489 + pdch->num_tbfs(GPRS_RLCMAC_UL_TBF);
Pau Espin Pedrol0e3083d2021-09-17 17:35:34 +0200490 bool skip_idle = (num_tbfs == 0);
491#ifdef ENABLE_DIRECT_PHY
492 /* In DIRECT_PHY mode we want to always submit something to L1 in
493 * TRX0, since BTS is not preparing dummy bursts on idle TS for us */
494 skip_idle = skip_idle && trx != 0;
495#endif
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200496 if (!skip_idle && (msg = sched_dummy())) {
Pau Espin Pedrola89a2382021-08-25 16:49:48 +0200497 /* increase counter */
498 gsmtap_cat = PCU_GSMTAP_C_DL_DUMMY;
499 } else {
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200500 msg = NULL; /* submit empty frame */
Pau Espin Pedrola89a2382021-08-25 16:49:48 +0200501 }
Alexander Couzens2cb15472016-05-18 16:43:10 +0200502 }
Andreas Eversbergd6a8db62012-07-25 08:38:21 +0200503
Pau Espin Pedrol22862882020-11-16 17:35:10 +0100504 if (tx_is_egprs && pdch->has_gprs_only_tbf_attached()) {
505 pdch->fn_without_cs14 += 1;
506 } else {
507 pdch->fn_without_cs14 = 0;
508 }
509
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200510 /* Used to measure the leak rate, count all blocks */
511 gprs_bssgp_update_frames_sent();
512
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200513 if (msg) {
514 /* msg is now available */
515 bts_do_rate_ctr_add(bts, CTR_RLC_DL_BYTES, msgb_length(msg));
Pau Espin Pedrol4c5a7d32020-11-16 14:23:29 +0100516
Pau Espin Pedrolfef3da22021-09-22 13:56:27 +0200517 /* set USF */
518 OSMO_ASSERT(msgb_length(msg) > 0);
519 usf = usf_tbf ? usf_tbf->m_usf[ts] : USF_UNUSED;
520 msg->data[0] = (msg->data[0] & 0xf8) | usf;
521
522 /* Send to GSMTAP */
523 tap_n_acc(msg, bts, trx, ts, fn, gsmtap_cat);
524 }
525
526 /* send PDTCH/PACCH to L1. msg=NULL accepted too (idle block) */
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100527 pcu_l1if_tx_pdtch(msg, bts, trx, ts, bts->trx[trx].arfcn, fn, block_nr);
Andreas Eversberg39621c42012-06-27 15:36:27 +0200528
529 return 0;
530}