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