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 | |
| 24 | extern struct llist_head block_queue; |
| 25 | |
| 26 | static 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 | |
| 34 | int 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; |
| 39 | struct gprs_rlcmac_tbf *tbf; |
| 40 | uint8_t usf = 0x7; |
| 41 | struct msgb *msg = NULL; |
| 42 | uint32_t poll_fn; |
| 43 | uint8_t i, tfi; |
| 44 | |
| 45 | if (trx >= 8 || ts >= 8) |
| 46 | return -EINVAL; |
| 47 | pdch = &bts->trx[trx].pdch[ts]; |
| 48 | |
| 49 | if (!pdch->enable) { |
| 50 | LOGP(DRLCMACSCHED, LOGL_ERROR, "Received RTS on disabled PDCH: " |
| 51 | "TRX=%d TS=%d\n", trx, ts); |
| 52 | return -EIO; |
| 53 | } |
| 54 | |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 55 | /* store last frame number of RTS */ |
| 56 | pdch->last_rts_fn = fn; |
| 57 | |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 58 | /* check uplink ressource for polling */ |
| 59 | poll_fn = fn + 4; |
| 60 | if ((block_nr % 3) == 2) |
| 61 | poll_fn ++; |
| 62 | poll_fn = poll_fn % 2715648; |
| 63 | for (tfi = 0; tfi < 32; tfi++) { |
Andreas Eversberg | b0c7ea7 | 2012-07-13 14:46:03 +0200 | [diff] [blame^] | 64 | tbf = pdch->ul_tbf[tfi]; |
| 65 | if (tbf) { |
| 66 | /* no polling */ |
| 67 | if (tbf->poll_state != GPRS_RLCMAC_POLL_SCHED) |
| 68 | continue; |
| 69 | /* polling for next uplink block */ |
| 70 | if (tbf->poll_fn == poll_fn) |
| 71 | break; |
| 72 | } |
| 73 | tbf = pdch->dl_tbf[tfi]; |
| 74 | if (tbf) { |
| 75 | /* no polling */ |
| 76 | if (tbf->poll_state != GPRS_RLCMAC_POLL_SCHED) |
| 77 | continue; |
| 78 | /* polling for next uplink block */ |
| 79 | if (tbf->poll_fn == poll_fn) |
| 80 | break; |
| 81 | } |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 82 | } |
| 83 | /* found uplink where a block is polled */ |
| 84 | if (tfi < 32) { |
| 85 | LOGP(DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: TRX=%d " |
| 86 | "TS=%d FN=%d block_nr=%d scheduling free USF for " |
Andreas Eversberg | b0c7ea7 | 2012-07-13 14:46:03 +0200 | [diff] [blame^] | 87 | "polling at FN=%d of %s TFI=%d\n", trx, ts, fn, |
| 88 | block_nr, poll_fn, |
| 89 | (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", |
| 90 | tfi); |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 91 | /* use free USF */ |
| 92 | /* else, we search for uplink ressource */ |
| 93 | } else { |
| 94 | /* select uplink ressource */ |
| 95 | for (i = 0, tfi = pdch->next_ul_tfi; i < 32; |
| 96 | i++, tfi = (tfi + 1) & 31) { |
Andreas Eversberg | b0c7ea7 | 2012-07-13 14:46:03 +0200 | [diff] [blame^] | 97 | tbf = pdch->ul_tbf[tfi]; |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 98 | /* no TBF for this tfi, go next */ |
| 99 | if (!tbf) |
| 100 | continue; |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 101 | /* no UL ressources needed, go next */ |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 102 | /* we don't need to give ressources in FINISHED state, |
| 103 | * because we have received all blocks and only poll |
| 104 | * for packet control ack. */ |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 105 | if (tbf->state != GPRS_RLCMAC_FLOW) |
| 106 | continue; |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 107 | |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 108 | /* use this USF */ |
Andreas Eversberg | b0c7ea7 | 2012-07-13 14:46:03 +0200 | [diff] [blame^] | 109 | usf = tbf->dir.ul.usf[ts]; |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 110 | LOGP(DRLCMACSCHED, LOGL_DEBUG, "Received RTS for PDCH: " |
| 111 | "TRX=%d TS=%d FN=%d block_nr=%d scheduling " |
| 112 | "USF=%d for required uplink ressource of " |
Andreas Eversberg | b0c7ea7 | 2012-07-13 14:46:03 +0200 | [diff] [blame^] | 113 | "UL TBF=%d\n", trx, ts, fn, block_nr, usf, tfi); |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 114 | /* next TBF to handle ressource is the next one */ |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 115 | pdch->next_ul_tfi = (tfi + 1) & 31; |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 116 | break; |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | /* Prio 1: select control message */ |
Andreas Eversberg | b0c7ea7 | 2012-07-13 14:46:03 +0200 | [diff] [blame^] | 121 | for (i = 0; i < 64; i++) { |
| 122 | if (i < 32) |
| 123 | tbf = pdch->ul_tbf[i]; |
| 124 | else |
| 125 | tbf = pdch->dl_tbf[i & 31]; |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 126 | /* no TBF for this tfi, go next */ |
| 127 | if (!tbf) |
| 128 | continue; |
| 129 | /* schedule PACKET DOWNLINK ASSIGNMENT */ |
| 130 | if (tbf->dl_ass_state == GPRS_RLCMAC_DL_ASS_SEND_ASS) |
| 131 | msg = gprs_rlcmac_send_packet_downlink_assignment(tbf, |
| 132 | fn); |
| 133 | else |
| 134 | /* schedule PACKET UPLINK ASSIGNMENT */ |
| 135 | if (tbf->ul_ass_state == GPRS_RLCMAC_UL_ASS_SEND_ASS) |
| 136 | msg = gprs_rlcmac_send_packet_uplink_assignment(tbf, |
| 137 | fn); |
| 138 | else |
| 139 | /* schedule PACKET UPLINK ACK */ |
| 140 | if (tbf->ul_ack_state == GPRS_RLCMAC_UL_ACK_SEND_ACK) |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 141 | msg = gprs_rlcmac_send_uplink_ack(tbf, fn); |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 142 | if (msg) { |
| 143 | LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling control " |
Andreas Eversberg | b0c7ea7 | 2012-07-13 14:46:03 +0200 | [diff] [blame^] | 144 | "message at RTS for %s TBF=%d\n", |
| 145 | (tbf->direction == GPRS_RLCMAC_UL_TBF) |
| 146 | ? "UL" : "DL", tbf->tfi); |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 147 | break; |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | /* Prio 2: select data message for downlink */ |
| 152 | if (!msg) { |
| 153 | /* select downlink ressource */ |
| 154 | for (i = 0, tfi = pdch->next_dl_tfi; i < 32; |
| 155 | i++, tfi = (tfi + 1) & 31) { |
Andreas Eversberg | b0c7ea7 | 2012-07-13 14:46:03 +0200 | [diff] [blame^] | 156 | tbf = pdch->dl_tbf[tfi]; |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 157 | /* no TBF for this tfi, go next */ |
| 158 | if (!tbf) |
| 159 | continue; |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 160 | /* no DL TBF, go next */ |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 161 | if (tbf->direction != GPRS_RLCMAC_DL_TBF) |
| 162 | continue; |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 163 | /* no DL ressources needed, go next */ |
| 164 | if (tbf->state != GPRS_RLCMAC_FLOW |
| 165 | && tbf->state != GPRS_RLCMAC_FINISHED) |
| 166 | continue; |
| 167 | |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 168 | LOGP(DRLCMACSCHED, LOGL_DEBUG, "Scheduling data " |
Andreas Eversberg | b0c7ea7 | 2012-07-13 14:46:03 +0200 | [diff] [blame^] | 169 | "message at RTS for DL TBF=%d\n", tfi); |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 170 | /* next TBF to handle ressource is the next one */ |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 171 | pdch->next_dl_tfi = (tfi + 1) & 31; |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 172 | /* generate DL data block */ |
| 173 | msg = gprs_rlcmac_send_data_block_acknowledged(tbf, fn); |
| 174 | break; |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 175 | } |
Andreas Eversberg | 39621c4 | 2012-06-27 15:36:27 +0200 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* Prio 3: send dummy contol message */ |
| 179 | if (!msg) { |
| 180 | msg = msgb_alloc(23, "rlcmac_dl_idle"); |
| 181 | if (!msg) |
| 182 | return -ENOMEM; |
| 183 | memcpy(msgb_put(msg, 23), rlcmac_dl_idle, 23); |
| 184 | } |
| 185 | /* msg is now available */ |
| 186 | |
| 187 | /* set USF */ |
| 188 | msg->data[0] = (msg->data[0] & 0xf8) | usf; |
| 189 | |
| 190 | // printf("len=%d, date=%s\n", msg->len, osmo_hexdump(msg->data, msg->len)); |
| 191 | |
| 192 | /* send PDTCH/PACCH to L1 */ |
| 193 | pcu_l1if_tx_pdtch(msg, trx, ts, arfcn, fn, block_nr); |
| 194 | |
| 195 | return 0; |
| 196 | } |