blob: cd99516698e5f65a285418e01621361a1056d5d5 [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 */
19
20#include <gprs_bssgp_pcu.h>
21#include <gprs_rlcmac.h>
22#include <pcu_l1_if.h>
23
24extern struct llist_head block_queue;
25
26static uint8_t rlcmac_dl_idle[23] = {
27 0x47, /* control without optional header octets, no polling, USF=111 */
28 0x94, /* dummy downlink control message, paging mode 00 */
29 0x2b, /* no persistance level, 7 bits spare pattern */
30 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b,
31 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b
32};
33
34int gprs_rlcmac_rcv_rts_block(uint8_t trx, uint8_t ts, uint16_t arfcn,
35 uint32_t fn, uint8_t block_nr)
36{
37 struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts;
38 struct gprs_rlcmac_pdch *pdch;
Andreas Eversberg592e04a2012-07-15 06:25:37 +020039 struct gprs_rlcmac_tbf *tbf, *poll_tbf = NULL, *dl_ass_tbf = NULL,
40 *ul_ass_tbf = NULL, *ul_ack_tbf = NULL;
Andreas Eversberg39621c42012-06-27 15:36:27 +020041 uint8_t usf = 0x7;
42 struct msgb *msg = NULL;
43 uint32_t poll_fn;
44 uint8_t i, tfi;
45
46 if (trx >= 8 || ts >= 8)
47 return -EINVAL;
48 pdch = &bts->trx[trx].pdch[ts];
49
50 if (!pdch->enable) {
51 LOGP(DRLCMACSCHED, LOGL_ERROR, "Received RTS on disabled PDCH: "
52 "TRX=%d TS=%d\n", trx, ts);
53 return -EIO;
54 }
55
Andreas Eversberge6228b32012-07-03 13:36:03 +020056 /* store last frame number of RTS */
57 pdch->last_rts_fn = fn;
58
Andreas Eversberg592e04a2012-07-15 06:25:37 +020059 /* check special TBF for events */
Andreas Eversberg39621c42012-06-27 15:36:27 +020060 poll_fn = fn + 4;
61 if ((block_nr % 3) == 2)
62 poll_fn ++;
63 poll_fn = poll_fn % 2715648;
Andreas Eversberg592e04a2012-07-15 06:25:37 +020064 llist_for_each_entry(tbf, &gprs_rlcmac_ul_tbfs, list) {
65 /* this trx, this ts */
66 if (tbf->trx != trx || tbf->control_ts != ts)
67 continue;
68 /* polling for next uplink block */
69 if (tbf->poll_state == GPRS_RLCMAC_POLL_SCHED
70 && tbf->poll_fn == poll_fn)
71 poll_tbf = tbf;
Andreas Eversbergee31b782012-07-15 16:27:01 +020072 if (tbf->ul_ack_state == GPRS_RLCMAC_UL_ACK_SEND_ACK)
73 ul_ack_tbf = tbf;
Andreas Eversberg592e04a2012-07-15 06:25:37 +020074 if (tbf->dl_ass_state == GPRS_RLCMAC_DL_ASS_SEND_ASS)
75 dl_ass_tbf = tbf;
76 if (tbf->ul_ass_state == GPRS_RLCMAC_UL_ASS_SEND_ASS)
77 ul_ass_tbf = tbf;
Andreas Eversberg39621c42012-06-27 15:36:27 +020078 }
Andreas Eversberg592e04a2012-07-15 06:25:37 +020079 llist_for_each_entry(tbf, &gprs_rlcmac_dl_tbfs, list) {
80 /* this trx, this ts */
81 if (tbf->trx != trx || tbf->control_ts != ts)
82 continue;
83 /* polling for next uplink block */
84 if (tbf->poll_state == GPRS_RLCMAC_POLL_SCHED
85 && tbf->poll_fn == poll_fn)
86 poll_tbf = tbf;
87 if (tbf->dl_ass_state == GPRS_RLCMAC_DL_ASS_SEND_ASS)
88 dl_ass_tbf = tbf;
89 if (tbf->ul_ass_state == GPRS_RLCMAC_UL_ASS_SEND_ASS)
90 ul_ass_tbf = tbf;
Andreas Eversberg592e04a2012-07-15 06:25:37 +020091 }
92
93 /* check uplink ressource for polling */
94 if (poll_tbf) {
Andreas Eversberg39621c42012-06-27 15:36:27 +020095 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: TRX=%d "
96 "TS=%d FN=%d block_nr=%d scheduling free USF for "
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +020097 "polling at FN=%d of %s TFI=%d\n", trx, ts, fn,
98 block_nr, poll_fn,
99 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200100 poll_tbf->tfi);
Andreas Eversberg39621c42012-06-27 15:36:27 +0200101 /* use free USF */
102 /* else, we search for uplink ressource */
103 } else {
104 /* select uplink ressource */
105 for (i = 0, tfi = pdch->next_ul_tfi; i < 32;
106 i++, tfi = (tfi + 1) & 31) {
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200107 tbf = pdch->ul_tbf[tfi];
Andreas Eversberg39621c42012-06-27 15:36:27 +0200108 /* no TBF for this tfi, go next */
109 if (!tbf)
110 continue;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200111 /* no UL ressources needed, go next */
Andreas Eversberge6228b32012-07-03 13:36:03 +0200112 /* we don't need to give ressources in FINISHED state,
113 * because we have received all blocks and only poll
114 * for packet control ack. */
Andreas Eversberg39621c42012-06-27 15:36:27 +0200115 if (tbf->state != GPRS_RLCMAC_FLOW)
116 continue;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200117
Andreas Eversberg39621c42012-06-27 15:36:27 +0200118 /* use this USF */
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200119 usf = tbf->dir.ul.usf[ts];
Andreas Eversberg39621c42012-06-27 15:36:27 +0200120 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: "
121 "TRX=%d TS=%d FN=%d block_nr=%d scheduling "
122 "USF=%d for required uplink ressource of "
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200123 "UL TBF=%d\n", trx, ts, fn, block_nr, usf, tfi);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200124 /* next TBF to handle ressource is the next one */
Andreas Eversberg39621c42012-06-27 15:36:27 +0200125 pdch->next_ul_tfi = (tfi + 1) & 31;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200126 break;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200127 }
128 }
129
130 /* Prio 1: select control message */
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200131 /* schedule PACKET DOWNLINK ASSIGNMENT */
132 if (dl_ass_tbf) {
133 tbf = dl_ass_tbf;
134 msg = gprs_rlcmac_send_packet_downlink_assignment(tbf, fn);
Andreas Eversbergee31b782012-07-15 16:27:01 +0200135 }
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200136 /* schedule PACKET UPLINK ASSIGNMENT */
Andreas Eversbergee31b782012-07-15 16:27:01 +0200137 if (!msg && ul_ass_tbf) {
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200138 tbf = ul_ass_tbf;
139 msg = gprs_rlcmac_send_packet_uplink_assignment(tbf, fn);
Andreas Eversbergee31b782012-07-15 16:27:01 +0200140 }
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200141 /* schedule PACKET UPLINK ACK */
Andreas Eversbergee31b782012-07-15 16:27:01 +0200142 if (!msg && ul_ack_tbf) {
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200143 tbf = ul_ack_tbf;
144 msg = gprs_rlcmac_send_uplink_ack(tbf, fn);
145 }
146 if (msg) {
147 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling control "
148 "message at RTS for %s TBF=%d (TRX=%d, TS=%d)\n",
149 (tbf->direction == GPRS_RLCMAC_UL_TBF)
150 ? "UL" : "DL", tbf->tfi, trx, ts);
Andreas Eversberg39621c42012-06-27 15:36:27 +0200151 }
152
153 /* Prio 2: select data message for downlink */
154 if (!msg) {
155 /* select downlink ressource */
156 for (i = 0, tfi = pdch->next_dl_tfi; i < 32;
157 i++, tfi = (tfi + 1) & 31) {
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200158 tbf = pdch->dl_tbf[tfi];
Andreas Eversberg39621c42012-06-27 15:36:27 +0200159 /* no TBF for this tfi, go next */
160 if (!tbf)
161 continue;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200162 /* no DL TBF, go next */
Andreas Eversberg39621c42012-06-27 15:36:27 +0200163 if (tbf->direction != GPRS_RLCMAC_DL_TBF)
164 continue;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200165 /* no DL ressources needed, go next */
166 if (tbf->state != GPRS_RLCMAC_FLOW
167 && tbf->state != GPRS_RLCMAC_FINISHED)
168 continue;
169
Andreas Eversberg39621c42012-06-27 15:36:27 +0200170 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling data "
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200171 "message at RTS for DL TBF=%d (TRX=%d, "
172 "TS=%d)\n", tfi, trx, ts);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200173 /* next TBF to handle ressource is the next one */
Andreas Eversberg39621c42012-06-27 15:36:27 +0200174 pdch->next_dl_tfi = (tfi + 1) & 31;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200175 /* generate DL data block */
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200176 msg = gprs_rlcmac_send_data_block_acknowledged(tbf, fn,
177 ts);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200178 break;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200179 }
Andreas Eversberg39621c42012-06-27 15:36:27 +0200180 }
181
182 /* Prio 3: send dummy contol message */
183 if (!msg) {
184 msg = msgb_alloc(23, "rlcmac_dl_idle");
185 if (!msg)
186 return -ENOMEM;
187 memcpy(msgb_put(msg, 23), rlcmac_dl_idle, 23);
188 }
189 /* msg is now available */
190
191 /* set USF */
192 msg->data[0] = (msg->data[0] & 0xf8) | usf;
193
194// printf("len=%d, date=%s\n", msg->len, osmo_hexdump(msg->data, msg->len));
195
196 /* send PDTCH/PACCH to L1 */
197 pcu_l1if_tx_pdtch(msg, trx, ts, arfcn, fn, block_nr);
198
199 return 0;
200}