blob: f88b09b51dd288dc13d8a1d94b5bf1ddad31d3df [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;
72 if (tbf->dl_ass_state == GPRS_RLCMAC_DL_ASS_SEND_ASS)
73 dl_ass_tbf = tbf;
74 if (tbf->ul_ass_state == GPRS_RLCMAC_UL_ASS_SEND_ASS)
75 ul_ass_tbf = tbf;
76 if (tbf->ul_ack_state == GPRS_RLCMAC_UL_ACK_SEND_ACK)
77 ul_ack_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;
91 if (tbf->ul_ack_state == GPRS_RLCMAC_UL_ACK_SEND_ACK)
92 ul_ack_tbf = tbf;
93 }
94
95 /* check uplink ressource for polling */
96 if (poll_tbf) {
Andreas Eversberg39621c42012-06-27 15:36:27 +020097 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: TRX=%d "
98 "TS=%d FN=%d block_nr=%d scheduling free USF for "
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +020099 "polling at FN=%d of %s TFI=%d\n", trx, ts, fn,
100 block_nr, poll_fn,
101 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200102 poll_tbf->tfi);
Andreas Eversberg39621c42012-06-27 15:36:27 +0200103 /* use free USF */
104 /* else, we search for uplink ressource */
105 } else {
106 /* select uplink ressource */
107 for (i = 0, tfi = pdch->next_ul_tfi; i < 32;
108 i++, tfi = (tfi + 1) & 31) {
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200109 tbf = pdch->ul_tbf[tfi];
Andreas Eversberg39621c42012-06-27 15:36:27 +0200110 /* no TBF for this tfi, go next */
111 if (!tbf)
112 continue;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200113 /* no UL ressources needed, go next */
Andreas Eversberge6228b32012-07-03 13:36:03 +0200114 /* we don't need to give ressources in FINISHED state,
115 * because we have received all blocks and only poll
116 * for packet control ack. */
Andreas Eversberg39621c42012-06-27 15:36:27 +0200117 if (tbf->state != GPRS_RLCMAC_FLOW)
118 continue;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200119
Andreas Eversberg39621c42012-06-27 15:36:27 +0200120 /* use this USF */
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200121 usf = tbf->dir.ul.usf[ts];
Andreas Eversberg39621c42012-06-27 15:36:27 +0200122 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: "
123 "TRX=%d TS=%d FN=%d block_nr=%d scheduling "
124 "USF=%d for required uplink ressource of "
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200125 "UL TBF=%d\n", trx, ts, fn, block_nr, usf, tfi);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200126 /* next TBF to handle ressource is the next one */
Andreas Eversberg39621c42012-06-27 15:36:27 +0200127 pdch->next_ul_tfi = (tfi + 1) & 31;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200128 break;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200129 }
130 }
131
132 /* Prio 1: select control message */
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200133 /* schedule PACKET DOWNLINK ASSIGNMENT */
134 if (dl_ass_tbf) {
135 tbf = dl_ass_tbf;
136 msg = gprs_rlcmac_send_packet_downlink_assignment(tbf, fn);
137 } else
138 /* schedule PACKET UPLINK ASSIGNMENT */
139 if (ul_ass_tbf) {
140 tbf = ul_ass_tbf;
141 msg = gprs_rlcmac_send_packet_uplink_assignment(tbf, fn);
142 } else
143 /* schedule PACKET UPLINK ACK */
144 if (ul_ack_tbf) {
145 tbf = ul_ack_tbf;
146 msg = gprs_rlcmac_send_uplink_ack(tbf, fn);
147 }
148 if (msg) {
149 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling control "
150 "message at RTS for %s TBF=%d (TRX=%d, TS=%d)\n",
151 (tbf->direction == GPRS_RLCMAC_UL_TBF)
152 ? "UL" : "DL", tbf->tfi, trx, ts);
Andreas Eversberg39621c42012-06-27 15:36:27 +0200153 }
154
155 /* Prio 2: select data message for downlink */
156 if (!msg) {
157 /* select downlink ressource */
158 for (i = 0, tfi = pdch->next_dl_tfi; i < 32;
159 i++, tfi = (tfi + 1) & 31) {
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200160 tbf = pdch->dl_tbf[tfi];
Andreas Eversberg39621c42012-06-27 15:36:27 +0200161 /* no TBF for this tfi, go next */
162 if (!tbf)
163 continue;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200164 /* no DL TBF, go next */
Andreas Eversberg39621c42012-06-27 15:36:27 +0200165 if (tbf->direction != GPRS_RLCMAC_DL_TBF)
166 continue;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200167 /* no DL ressources needed, go next */
168 if (tbf->state != GPRS_RLCMAC_FLOW
169 && tbf->state != GPRS_RLCMAC_FINISHED)
170 continue;
171
Andreas Eversberg39621c42012-06-27 15:36:27 +0200172 LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling data "
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200173 "message at RTS for DL TBF=%d (TRX=%d, "
174 "TS=%d)\n", tfi, trx, ts);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200175 /* next TBF to handle ressource is the next one */
Andreas Eversberg39621c42012-06-27 15:36:27 +0200176 pdch->next_dl_tfi = (tfi + 1) & 31;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200177 /* generate DL data block */
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200178 msg = gprs_rlcmac_send_data_block_acknowledged(tbf, fn,
179 ts);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200180 break;
Andreas Eversberg39621c42012-06-27 15:36:27 +0200181 }
Andreas Eversberg39621c42012-06-27 15:36:27 +0200182 }
183
184 /* Prio 3: send dummy contol message */
185 if (!msg) {
186 msg = msgb_alloc(23, "rlcmac_dl_idle");
187 if (!msg)
188 return -ENOMEM;
189 memcpy(msgb_put(msg, 23), rlcmac_dl_idle, 23);
190 }
191 /* msg is now available */
192
193 /* set USF */
194 msg->data[0] = (msg->data[0] & 0xf8) | usf;
195
196// printf("len=%d, date=%s\n", msg->len, osmo_hexdump(msg->data, msg->len));
197
198 /* send PDTCH/PACCH to L1 */
199 pcu_l1if_tx_pdtch(msg, trx, ts, arfcn, fn, block_nr);
200
201 return 0;
202}