Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 1 | /* 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 | |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 24 | uint32_t sched_poll(uint8_t trx, uint8_t ts, uint32_t fn, uint8_t block_nr, |
| 25 | struct gprs_rlcmac_tbf **poll_tbf, |
| 26 | struct gprs_rlcmac_tbf **ul_ack_tbf, |
| 27 | struct gprs_rlcmac_tbf **dl_ass_tbf, |
| 28 | struct gprs_rlcmac_tbf **ul_ass_tbf) |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 29 | { |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 30 | struct gprs_rlcmac_tbf *tbf; |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 31 | uint32_t poll_fn; |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 32 | |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 33 | /* check special TBF for events */ |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 34 | poll_fn = fn + 4; |
| 35 | if ((block_nr % 3) == 2) |
| 36 | poll_fn ++; |
| 37 | poll_fn = poll_fn % 2715648; |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 38 | llist_for_each_entry(tbf, &gprs_rlcmac_ul_tbfs, list) { |
| 39 | /* this trx, this ts */ |
| 40 | if (tbf->trx != trx || tbf->control_ts != ts) |
| 41 | continue; |
| 42 | /* polling for next uplink block */ |
| 43 | if (tbf->poll_state == GPRS_RLCMAC_POLL_SCHED |
| 44 | && tbf->poll_fn == poll_fn) |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 45 | *poll_tbf = tbf; |
Andreas Eversberg | ee31b78 | 2012-07-15 16:27:01 +0200 | [diff] [blame] | 46 | if (tbf->ul_ack_state == GPRS_RLCMAC_UL_ACK_SEND_ACK) |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 47 | *ul_ack_tbf = tbf; |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 48 | if (tbf->dl_ass_state == GPRS_RLCMAC_DL_ASS_SEND_ASS) |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 49 | *dl_ass_tbf = tbf; |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 50 | if (tbf->ul_ass_state == GPRS_RLCMAC_UL_ASS_SEND_ASS) |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 51 | *ul_ass_tbf = tbf; |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 52 | } |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 53 | llist_for_each_entry(tbf, &gprs_rlcmac_dl_tbfs, list) { |
| 54 | /* this trx, this ts */ |
| 55 | if (tbf->trx != trx || tbf->control_ts != ts) |
| 56 | continue; |
| 57 | /* polling for next uplink block */ |
| 58 | if (tbf->poll_state == GPRS_RLCMAC_POLL_SCHED |
| 59 | && tbf->poll_fn == poll_fn) |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 60 | *poll_tbf = tbf; |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 61 | if (tbf->dl_ass_state == GPRS_RLCMAC_DL_ASS_SEND_ASS) |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 62 | *dl_ass_tbf = tbf; |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 63 | if (tbf->ul_ass_state == GPRS_RLCMAC_UL_ASS_SEND_ASS) |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 64 | *ul_ass_tbf = tbf; |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 67 | return poll_fn; |
| 68 | } |
| 69 | |
| 70 | uint8_t sched_select_uplink(uint8_t trx, uint8_t ts, uint32_t fn, |
| 71 | uint8_t block_nr, struct gprs_rlcmac_pdch *pdch) |
| 72 | { |
| 73 | struct gprs_rlcmac_tbf *tbf; |
| 74 | uint8_t usf = 0x07; |
| 75 | uint8_t i, tfi; |
| 76 | |
| 77 | /* select uplink ressource */ |
| 78 | for (i = 0, tfi = pdch->next_ul_tfi; i < 32; |
| 79 | i++, tfi = (tfi + 1) & 31) { |
| 80 | tbf = pdch->ul_tbf[tfi]; |
| 81 | /* no TBF for this tfi, go next */ |
| 82 | if (!tbf) |
| 83 | continue; |
| 84 | /* no UL ressources needed, go next */ |
| 85 | /* we don't need to give ressources in FINISHED state, |
| 86 | * because we have received all blocks and only poll |
| 87 | * for packet control ack. */ |
| 88 | if (tbf->state != GPRS_RLCMAC_FLOW) |
| 89 | continue; |
| 90 | |
| 91 | /* use this USF */ |
| 92 | usf = tbf->dir.ul.usf[ts]; |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 93 | LOGP(DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: TRX=%d " |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 94 | "TS=%d FN=%d block_nr=%d scheduling USF=%d for " |
| 95 | "required uplink ressource of UL TBF=%d\n", trx, ts, fn, |
| 96 | block_nr, usf, tfi); |
| 97 | /* next TBF to handle ressource is the next one */ |
| 98 | pdch->next_ul_tfi = (tfi + 1) & 31; |
| 99 | break; |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 102 | return usf; |
| 103 | } |
| 104 | |
| 105 | struct msgb *sched_select_ctrl_msg(uint8_t trx, uint8_t ts, uint32_t fn, |
| 106 | uint8_t block_nr, struct gprs_rlcmac_pdch *pdch, |
| 107 | struct gprs_rlcmac_tbf *ul_ack_tbf, |
| 108 | struct gprs_rlcmac_tbf *dl_ass_tbf, |
| 109 | struct gprs_rlcmac_tbf *ul_ass_tbf) |
| 110 | { |
| 111 | struct msgb *msg = NULL; |
| 112 | struct gprs_rlcmac_tbf *tbf = NULL; |
| 113 | |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 114 | /* schedule PACKET DOWNLINK ASSIGNMENT */ |
| 115 | if (dl_ass_tbf) { |
| 116 | tbf = dl_ass_tbf; |
| 117 | msg = gprs_rlcmac_send_packet_downlink_assignment(tbf, fn); |
Andreas Eversberg | ee31b78 | 2012-07-15 16:27:01 +0200 | [diff] [blame] | 118 | } |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 119 | /* schedule PACKET UPLINK ASSIGNMENT */ |
Andreas Eversberg | ee31b78 | 2012-07-15 16:27:01 +0200 | [diff] [blame] | 120 | if (!msg && ul_ass_tbf) { |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 121 | tbf = ul_ass_tbf; |
| 122 | msg = gprs_rlcmac_send_packet_uplink_assignment(tbf, fn); |
Andreas Eversberg | ee31b78 | 2012-07-15 16:27:01 +0200 | [diff] [blame] | 123 | } |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 124 | /* schedule PACKET UPLINK ACK */ |
Andreas Eversberg | ee31b78 | 2012-07-15 16:27:01 +0200 | [diff] [blame] | 125 | if (!msg && ul_ack_tbf) { |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 126 | tbf = ul_ack_tbf; |
| 127 | msg = gprs_rlcmac_send_uplink_ack(tbf, fn); |
| 128 | } |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 129 | /* any message */ |
Andreas Eversberg | 592e04a | 2012-07-15 06:25:37 +0200 | [diff] [blame] | 130 | if (msg) { |
| 131 | LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling control " |
| 132 | "message at RTS for %s TBF=%d (TRX=%d, TS=%d)\n", |
| 133 | (tbf->direction == GPRS_RLCMAC_UL_TBF) |
| 134 | ? "UL" : "DL", tbf->tfi, trx, ts); |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 135 | return msg; |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 136 | } |
Andreas Eversberg | 3b7461c | 2012-07-20 11:19:59 +0200 | [diff] [blame] | 137 | /* schedule PACKET PAGING REQUEST */ |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 138 | if (llist_empty(&pdch->paging_list)) |
| 139 | return NULL; |
| 140 | msg = gprs_rlcmac_send_packet_paging_request(pdch); |
| 141 | if (msg) |
| 142 | LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling paging request " |
| 143 | "message at RTS for (TRX=%d, TS=%d)\n", trx, ts); |
| 144 | |
| 145 | return msg; |
| 146 | } |
| 147 | |
| 148 | struct msgb *sched_select_downlink(uint8_t trx, uint8_t ts, uint32_t fn, |
| 149 | uint8_t block_nr, struct gprs_rlcmac_pdch *pdch) |
| 150 | { |
| 151 | struct msgb *msg = NULL; |
| 152 | struct gprs_rlcmac_tbf *tbf = NULL; |
| 153 | uint8_t i, tfi; |
| 154 | |
| 155 | /* select downlink ressource */ |
| 156 | for (i = 0, tfi = pdch->next_dl_tfi; i < 32; |
| 157 | i++, tfi = (tfi + 1) & 31) { |
| 158 | tbf = pdch->dl_tbf[tfi]; |
| 159 | /* no TBF for this tfi, go next */ |
| 160 | if (!tbf) |
| 161 | continue; |
| 162 | /* no DL TBF, go next */ |
| 163 | if (tbf->direction != GPRS_RLCMAC_DL_TBF) |
| 164 | continue; |
| 165 | /* no DL ressources needed, go next */ |
| 166 | if (tbf->state != GPRS_RLCMAC_FLOW |
| 167 | && tbf->state != GPRS_RLCMAC_FINISHED) |
| 168 | continue; |
| 169 | |
| 170 | LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling data message at " |
| 171 | "RTS for DL TBF=%d (TRX=%d, TS=%d)\n", tfi, trx, ts); |
| 172 | /* next TBF to handle ressource is the next one */ |
| 173 | pdch->next_dl_tfi = (tfi + 1) & 31; |
| 174 | /* generate DL data block */ |
| 175 | msg = gprs_rlcmac_send_data_block_acknowledged(tbf, fn, |
| 176 | ts); |
| 177 | break; |
Andreas Eversberg | 3b7461c | 2012-07-20 11:19:59 +0200 | [diff] [blame] | 178 | } |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 179 | |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 180 | return msg; |
| 181 | } |
| 182 | static uint8_t rlcmac_dl_idle[23] = { |
| 183 | 0x47, /* control without optional header octets, no polling, USF=111 */ |
| 184 | 0x94, /* dummy downlink control message, paging mode 00 */ |
| 185 | 0x2b, /* no persistance level, 7 bits spare pattern */ |
| 186 | 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, |
| 187 | 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b |
| 188 | }; |
| 189 | |
| 190 | struct msgb *sched_dummy(void) |
| 191 | { |
| 192 | struct msgb *msg; |
| 193 | |
| 194 | msg = msgb_alloc(23, "rlcmac_dl_idle"); |
| 195 | if (!msg) |
| 196 | return NULL; |
| 197 | memcpy(msgb_put(msg, 23), rlcmac_dl_idle, 23); |
| 198 | |
| 199 | return msg; |
| 200 | } |
| 201 | |
| 202 | int gprs_rlcmac_rcv_rts_block(uint8_t trx, uint8_t ts, uint16_t arfcn, |
| 203 | uint32_t fn, uint8_t block_nr) |
| 204 | { |
| 205 | struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts; |
| 206 | struct gprs_rlcmac_pdch *pdch; |
| 207 | struct gprs_rlcmac_tbf *poll_tbf = NULL, *dl_ass_tbf = NULL, |
| 208 | *ul_ass_tbf = NULL, *ul_ack_tbf = NULL; |
| 209 | uint8_t usf = 0x7; |
| 210 | struct msgb *msg = NULL; |
| 211 | uint32_t poll_fn; |
| 212 | |
| 213 | if (trx >= 8 || ts >= 8) |
| 214 | return -EINVAL; |
| 215 | pdch = &bts->trx[trx].pdch[ts]; |
| 216 | |
| 217 | if (!pdch->enable) { |
| 218 | LOGP(DRLCMACSCHED, LOGL_ERROR, "Received RTS on disabled PDCH: " |
| 219 | "TRX=%d TS=%d\n", trx, ts); |
| 220 | return -EIO; |
| 221 | } |
| 222 | |
| 223 | /* store last frame number of RTS */ |
| 224 | pdch->last_rts_fn = fn; |
| 225 | |
| 226 | poll_fn = sched_poll(trx, ts, fn, block_nr, &poll_tbf, &ul_ack_tbf, |
| 227 | &dl_ass_tbf, &ul_ass_tbf); |
| 228 | /* check uplink ressource for polling */ |
| 229 | if (poll_tbf) |
| 230 | LOGP(DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: TRX=%d " |
| 231 | "TS=%d FN=%d block_nr=%d scheduling free USF for " |
| 232 | "polling at FN=%d of %s TFI=%d\n", trx, ts, fn, |
| 233 | block_nr, poll_fn, |
| 234 | (poll_tbf->direction == GPRS_RLCMAC_UL_TBF) |
| 235 | ? "UL" : "DL", poll_tbf->tfi); |
| 236 | /* use free USF */ |
| 237 | /* else, we search for uplink ressource */ |
| 238 | else |
| 239 | usf = sched_select_uplink(trx, ts, fn, block_nr, pdch); |
| 240 | |
| 241 | /* Prio 1: select control message */ |
| 242 | msg = sched_select_ctrl_msg(trx, ts, fn, block_nr, pdch, ul_ack_tbf, |
| 243 | dl_ass_tbf, ul_ass_tbf); |
| 244 | |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 245 | /* Prio 2: select data message for downlink */ |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 246 | if (!msg) |
| 247 | msg = sched_select_downlink(trx, ts, fn, block_nr, pdch); |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 248 | |
| 249 | /* Prio 3: send dummy contol message */ |
Andreas Eversberg | d6a8db6 | 2012-07-25 08:38:21 +0200 | [diff] [blame^] | 250 | if (!msg) |
| 251 | msg = sched_dummy(); |
| 252 | |
| 253 | if (!msg) |
| 254 | return -ENOMEM; |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 255 | /* msg is now available */ |
| 256 | |
| 257 | /* set USF */ |
| 258 | msg->data[0] = (msg->data[0] & 0xf8) | usf; |
| 259 | |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 260 | /* send PDTCH/PACCH to L1 */ |
| 261 | pcu_l1if_tx_pdtch(msg, trx, ts, arfcn, fn, block_nr); |
| 262 | |
| 263 | return 0; |
| 264 | } |