blob: ce278796ccf6af387d05ed2674affc9692dc60bf [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* gprs_rlcmac.h
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
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#ifndef GPRS_RLCMAC_H
21#define GPRS_RLCMAC_H
22
Ivan Kluchnikov835f91e2012-04-30 18:00:36 +040023#include <bitvector.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040024#include <gsm_rlcmac.h>
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +040025#include <gsm_timer.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040026
27extern "C" {
28#include <osmocom/core/linuxlist.h>
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +040029#include <osmocom/core/timer.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040030}
31
Andreas Eversberge6228b32012-07-03 13:36:03 +020032/* This special feature will delay assignment of downlink TBF by one second,
33 * in case there is already a TBF.
34 * This is usefull to debug downlink establishment during packet idle mode.
35 */
36//#define DEBUG_DL_ASS_IDLE
Ivan Kluchnikovc5d8c272012-06-04 21:55:02 +040037
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020038/*
39 * PDCH instanc
40 */
41
42struct gprs_rlcmac_tbf;
43
44struct gprs_rlcmac_pdch {
45 uint8_t enable; /* TS is enabled */
46 uint8_t tsc; /* TSC of this slot */
47 uint8_t next_ul_tfi; /* next uplink TBF/TFI to schedule (0..31) */
48 uint8_t next_dl_tfi; /* next downlink TBF/TFI to schedule (0..31) */
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +020049 struct gprs_rlcmac_tbf *ul_tbf[32]; /* array of UL TBF, by UL TFI */
50 struct gprs_rlcmac_tbf *dl_tbf[32]; /* array of DL TBF, by DL TFI */
Andreas Eversberg2b914642012-07-19 13:06:26 +020051 struct llist_head paging_list; /* list of paging messages */
Andreas Eversberge6228b32012-07-03 13:36:03 +020052 uint32_t last_rts_fn; /* store last frame number of RTS */
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +040053};
54
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020055struct gprs_rlcmac_trx {
56 uint16_t arfcn;
57 struct gprs_rlcmac_pdch pdch[8];
58};
59
60struct gprs_rlcmac_bts {
Andreas Eversbergb3c6f6c2012-07-06 07:40:08 +020061 uint8_t cs1;
62 uint8_t cs2;
63 uint8_t cs3;
64 uint8_t cs4;
65 uint8_t initial_cs;
66 uint8_t t3142;
67 uint8_t t3169;
68 uint8_t t3191;
69 uint16_t t3193_msec;
70 uint8_t t3195;
71 uint8_t n3101;
72 uint8_t n3103;
73 uint8_t n3105;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020074 struct gprs_rlcmac_trx trx[8];
Andreas Eversberg592e04a2012-07-15 06:25:37 +020075 int (*alloc_algorithm)(struct gprs_rlcmac_tbf *old_tbf,
76 struct gprs_rlcmac_tbf *tbf, uint32_t cust);
77 uint32_t alloc_algorithm_curst; /* options to customize algorithm */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020078};
79
80extern struct gprs_rlcmac_bts *gprs_rlcmac_bts;
81
82/*
83 * TBF instance
84 */
85
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040086#define LLC_MAX_LEN 1543
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020087#define RLC_MAX_SNS 128 /* GPRS, must be power of 2 */
88#define RLC_MAX_WS 64 /* max window size */
Andreas Eversberge6228b32012-07-03 13:36:03 +020089#define RLC_MAX_LEN 54 /* CS-4 including spare bits */
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040090
Andreas Eversberge6228b32012-07-03 13:36:03 +020091#define Tassign_agch 0,500000/* wait for assignment, before transmitting DL */
92#define Tassign_pacch 0,100000/* wait for assignment, before transmitting DL */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020093
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040094enum gprs_rlcmac_tbf_state {
Andreas Eversberge6228b32012-07-03 13:36:03 +020095 GPRS_RLCMAC_NULL = 0, /* new created TBF */
96 GPRS_RLCMAC_ASSIGN, /* wait for downlink assignment */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020097 GPRS_RLCMAC_FLOW, /* RLC/MAC flow, ressource needed */
98 GPRS_RLCMAC_FINISHED, /* flow finished, wait for release */
Andreas Eversberge6228b32012-07-03 13:36:03 +020099 GPRS_RLCMAC_WAIT_RELEASE,/* wait for release or restart of DL TBF */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200100 GPRS_RLCMAC_RELEASING, /* releasing, wait to free TBI/USF */
101};
102
103enum gprs_rlcmac_tbf_poll_state {
104 GPRS_RLCMAC_POLL_NONE = 0,
105 GPRS_RLCMAC_POLL_SCHED, /* a polling was scheduled */
106};
107
Andreas Eversberge6228b32012-07-03 13:36:03 +0200108enum gprs_rlcmac_tbf_dl_ass_state {
109 GPRS_RLCMAC_DL_ASS_NONE = 0,
110 GPRS_RLCMAC_DL_ASS_SEND_ASS, /* send downlink assignment on next RTS */
111 GPRS_RLCMAC_DL_ASS_WAIT_ACK, /* wait for PACKET CONTROL ACK */
112};
113
114enum gprs_rlcmac_tbf_ul_ass_state {
115 GPRS_RLCMAC_UL_ASS_NONE = 0,
116 GPRS_RLCMAC_UL_ASS_SEND_ASS, /* send uplink assignment on next RTS */
117 GPRS_RLCMAC_UL_ASS_WAIT_ACK, /* wait for PACKET CONTROL ACK */
118};
119
120enum gprs_rlcmac_tbf_ul_ack_state {
121 GPRS_RLCMAC_UL_ACK_NONE = 0,
122 GPRS_RLCMAC_UL_ACK_SEND_ACK, /* send acknowledge on next RTS */
123 GPRS_RLCMAC_UL_ACK_WAIT_ACK, /* wait for PACKET CONTROL ACK */
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400124};
125
126enum gprs_rlcmac_tbf_direction {
127 GPRS_RLCMAC_DL_TBF,
128 GPRS_RLCMAC_UL_TBF
129};
130
131struct gprs_rlcmac_tbf {
132 struct llist_head list;
133 enum gprs_rlcmac_tbf_state state;
134 enum gprs_rlcmac_tbf_direction direction;
135 uint8_t tfi;
136 uint32_t tlli;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200137 uint8_t tlli_valid;
Andreas Eversbergee31b782012-07-15 16:27:01 +0200138 uint8_t contention_resolution_done; /* set after done */
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200139 uint8_t trx;
140 uint16_t arfcn;
141 uint8_t tsc;
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200142 uint8_t first_ts; /* first TS used by TBF */
143 uint8_t first_common_ts; /* first TS that the phone can send and
144 reveive simultaniously */
145 uint8_t control_ts; /* timeslot control messages and polling */
Andreas Eversbergf298fa82012-07-13 14:50:57 +0200146 uint8_t ms_class;
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200147 struct gprs_rlcmac_pdch *pdch[8]; /* list of PDCHs allocated to TBF */
148 uint16_t ta;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200149 uint8_t llc_frame[LLC_MAX_LEN]; /* current DL or UL frame */
150 uint16_t llc_index; /* current write/read position of frame */
151 uint16_t llc_length; /* len of current DL LLC_frame, 0 == no frame */
152 llist_head llc_queue; /* queued LLC DL data */
153
154 enum gprs_rlcmac_tbf_dl_ass_state dl_ass_state;
155 enum gprs_rlcmac_tbf_ul_ass_state ul_ass_state;
156 enum gprs_rlcmac_tbf_ul_ack_state ul_ack_state;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200157
158 enum gprs_rlcmac_tbf_poll_state poll_state;
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200159 uint32_t poll_fn; /* frame number to poll */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200160
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200161 uint16_t ws; /* window size */
162 uint16_t sns; /* sequence number space */
Andreas Eversberge6228b32012-07-03 13:36:03 +0200163
164 /* Please note that all variables here will be reset when changing
165 * from WAIT RELEASE back to FLOW state (re-use of TBF).
166 * All states that need reset must be in this struct, so this is why
167 * variables are in both (dl and ul) structs and not outside union.
168 */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200169 union {
170 struct {
Andreas Eversberge6228b32012-07-03 13:36:03 +0200171 uint16_t bsn; /* block sequence number */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200172 uint16_t v_s; /* send state */
173 uint16_t v_a; /* ack state */
174 char v_b[RLC_MAX_SNS/2]; /* acknowledge state array */
Andreas Eversberge6228b32012-07-03 13:36:03 +0200175 int32_t tx_counter; /* count all transmitted blocks */
176 uint8_t n3105; /* N3105 counter */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200177 } dl;
178 struct {
Andreas Eversberge6228b32012-07-03 13:36:03 +0200179 uint16_t bsn; /* block sequence number */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200180 uint16_t v_r; /* receive state */
181 uint16_t v_q; /* receive window state */
182 char v_n[RLC_MAX_SNS/2]; /* receive state array */
183 int32_t rx_counter; /* count all received blocks */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200184 uint8_t n3103; /* N3103 counter */
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200185 uint8_t usf[8]; /* list USFs per PDCH (timeslot) */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200186 } ul;
187 } dir;
188 uint8_t rlc_block[RLC_MAX_SNS/2][RLC_MAX_LEN]; /* block history */
189 uint8_t rlc_block_len[RLC_MAX_SNS/2]; /* block len of history */
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +0400190
191 struct osmo_timer_list timer;
192 unsigned int T; /* Txxxx number */
193 unsigned int num_T_exp; /* number of consecutive T expirations */
194
195 struct osmo_gsm_timer_list gsm_timer;
196 unsigned int fT; /* fTxxxx number */
197 unsigned int num_fT_exp; /* number of consecutive fT expirations */
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400198};
199
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200200extern struct llist_head gprs_rlcmac_ul_tbfs; /* list of uplink TBFs */
201extern struct llist_head gprs_rlcmac_dl_tbfs; /* list of downlink TBFs */
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400202
Andreas Eversberg2b914642012-07-19 13:06:26 +0200203/*
204 * paging entry
205 */
206struct gprs_rlcmac_paging {
207 struct llist_head list;
208 uint8_t chan_needed;
209 uint8_t identity_lv[9];
210};
211
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200212int tfi_alloc(enum gprs_rlcmac_tbf_direction dir, uint8_t *_trx, uint8_t *_ts,
213 uint8_t use_trx, uint8_t first_ts);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400214
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200215struct gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_tbf *old_tbf,
216 enum gprs_rlcmac_tbf_direction dir, uint8_t tfi, uint8_t trx,
217 uint8_t first_ts, uint8_t ms_class, uint8_t single_slot);
Andreas Eversbergf298fa82012-07-13 14:50:57 +0200218
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200219int alloc_algorithm_a(struct gprs_rlcmac_tbf *old_tbf,
220 struct gprs_rlcmac_tbf *tbf, uint32_t cust);
221
222int alloc_algorithm_b(struct gprs_rlcmac_tbf *old_tbf,
223 struct gprs_rlcmac_tbf *tbf, uint32_t cust);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400224
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200225struct gprs_rlcmac_tbf *tbf_by_tfi(uint8_t tfi, uint8_t trx, uint8_t ts,
226 enum gprs_rlcmac_tbf_direction dir);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400227
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200228struct gprs_rlcmac_tbf *tbf_by_tlli(uint32_t tlli,
229 enum gprs_rlcmac_tbf_direction dir);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200230
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200231struct gprs_rlcmac_tbf *tbf_by_poll_fn(uint32_t fn, uint8_t trx, uint8_t ts);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400232
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200233void tbf_free(struct gprs_rlcmac_tbf *tbf);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400234
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200235int tbf_update(struct gprs_rlcmac_tbf *tbf);
236
237int tbf_assign_control_ts(struct gprs_rlcmac_tbf *tbf);
238
Andreas Eversberge6228b32012-07-03 13:36:03 +0200239void tbf_new_state(struct gprs_rlcmac_tbf *tbf,
240 enum gprs_rlcmac_tbf_state state);
241
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200242void tbf_timer_start(struct gprs_rlcmac_tbf *tbf, unsigned int T,
Andreas Eversberge6228b32012-07-03 13:36:03 +0200243 unsigned int seconds, unsigned int microseconds);
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200244
245void tbf_timer_stop(struct gprs_rlcmac_tbf *tbf);
246
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400247/* TS 44.060 Section 10.4.7 Table 10.4.7.1: Payload Type field */
248enum gprs_rlcmac_block_type {
249 GPRS_RLCMAC_DATA_BLOCK = 0x0,
250 GPRS_RLCMAC_CONTROL_BLOCK = 0x1,
251 GPRS_RLCMAC_CONTROL_BLOCK_OPT = 0x2,
252 GPRS_RLCMAC_RESERVED = 0x3
253};
254
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200255int gprs_rlcmac_rcv_block(uint8_t trx, uint8_t ts, uint8_t *data, uint8_t len,
256 uint32_t fn);
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400257
Andreas Eversberge6228b32012-07-03 13:36:03 +0200258int write_immediate_assignment(bitvec * dest, uint8_t downlink, uint8_t ra,
259 uint32_t fn, uint8_t ta, uint16_t arfcn, uint8_t ts, uint8_t tsc,
260 uint8_t tfi, uint8_t usf, uint32_t tlli, uint8_t polling,
261 uint32_t poll_fn);
Ivan Kluchnikovcf7b3a52012-06-26 16:54:22 +0400262
Andreas Eversberge6228b32012-07-03 13:36:03 +0200263void write_packet_uplink_assignment(bitvec * dest, uint8_t old_tfi,
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200264 uint8_t old_downlink, uint32_t tlli, uint8_t use_tlli,
265 struct gprs_rlcmac_tbf *tbf, uint8_t poll);
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400266
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400267void write_packet_downlink_assignment(RlcMacDownlink_t * block, uint8_t old_tfi,
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200268 uint8_t old_downlink, struct gprs_rlcmac_tbf *tbf, uint8_t poll);
269
270
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400271
Ivan Kluchnikovef7f28c2012-07-12 14:49:15 +0400272void write_packet_uplink_ack(RlcMacDownlink_t * block, struct gprs_rlcmac_tbf *tbf,
Andreas Eversberge6228b32012-07-03 13:36:03 +0200273 uint8_t final);
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400274
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200275int gprs_rlcmac_tx_ul_ud(gprs_rlcmac_tbf *tbf);
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400276
Andreas Eversberge6228b32012-07-03 13:36:03 +0200277void tbf_timer_cb(void *_tbf);
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400278
Andreas Eversberge6228b32012-07-03 13:36:03 +0200279int gprs_rlcmac_poll_timeout(struct gprs_rlcmac_tbf *tbf);
Andreas Eversberg0aed6542012-06-23 10:33:16 +0200280
281int gprs_rlcmac_rcv_rach(uint8_t ra, uint32_t Fn, int16_t qta);
Ivan Kluchnikov5310d452012-04-17 22:00:31 +0400282
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200283int gprs_rlcmac_rcv_control_block(bitvec *rlc_block, uint8_t trx, uint8_t ts,
284 uint32_t fn);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400285
Andreas Eversberge6228b32012-07-03 13:36:03 +0200286struct msgb *gprs_rlcmac_send_packet_uplink_assignment(
287 struct gprs_rlcmac_tbf *tbf, uint32_t fn);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400288
Andreas Eversberge6228b32012-07-03 13:36:03 +0200289struct msgb *gprs_rlcmac_send_packet_downlink_assignment(
290 struct gprs_rlcmac_tbf *tbf, uint32_t fn);
Ivan Kluchnikovc320d862012-03-18 15:04:48 +0400291
Andreas Eversberge6228b32012-07-03 13:36:03 +0200292void gprs_rlcmac_trigger_downlink_assignment(gprs_rlcmac_tbf *tbf,
Andreas Eversberge13fa2d2012-07-09 17:10:44 +0200293 uint8_t old_downlink, char *imsi);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400294
Andreas Eversberge6228b32012-07-03 13:36:03 +0200295int gprs_rlcmac_downlink_ack(struct gprs_rlcmac_tbf *tbf, uint8_t final,
296 uint8_t ssn, uint8_t *rbb);
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200297
Andreas Eversberg2b914642012-07-19 13:06:26 +0200298unsigned write_packet_paging_request(bitvec * dest);
299
300unsigned write_repeated_page_info(bitvec * dest, unsigned& wp, uint8_t len,
301 uint8_t *identity, uint8_t chan_needed);
302
Andreas Eversbergb0c7ea72012-07-13 14:46:03 +0200303int gprs_rlcmac_rcv_data_block_acknowledged(uint8_t trx, uint8_t ts,
304 uint8_t *data, uint8_t len);
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200305
Andreas Eversberge6228b32012-07-03 13:36:03 +0200306struct msgb *gprs_rlcmac_send_data_block_acknowledged(
Andreas Eversberg592e04a2012-07-15 06:25:37 +0200307 struct gprs_rlcmac_tbf *tbf, uint32_t fn, uint8_t ts);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200308
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200309struct msgb *gprs_rlcmac_send_uplink_ack(struct gprs_rlcmac_tbf *tbf,
310 uint32_t fn);
Ivan Kluchnikov9c795ab2012-05-24 23:12:59 +0400311
Andreas Eversberge6228b32012-07-03 13:36:03 +0200312int gprs_rlcmac_rcv_rts_block(uint8_t trx, uint8_t ts, uint16_t arfcn,
313 uint32_t fn, uint8_t block_nr);
314
Andreas Eversberg2b914642012-07-19 13:06:26 +0200315int gprs_rlcmac_add_paging(uint8_t chan_needed, uint8_t *identity_lv);
316
317struct gprs_rlcmac_paging *gprs_rlcmac_dequeue_paging(
318 struct gprs_rlcmac_pdch *pdch);
319
320struct msgb *gprs_rlcmac_send_packet_paging_request(
321 struct gprs_rlcmac_pdch *pdch);
322
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400323#endif // GPRS_RLCMAC_H