This document describes the handling of TBFs
--------------------------------------------

Notes:
  TBF
    Instance for one temporary block flow.

  LLC Frame
    Current frame to be transmitted or received. If tbf->llc_length is not 0,
    the frame exists, if 0, the frame does not exist. If tbf->llc_index is not
    0, parts of the frame have been transmitted or received so far.

  LLC Queue
    Queue of next frames to be transmitted.

States:
  TBF_ST_ASSIGN
    After a downlink TBF is created, it resides in this state until the
    block flow can start. This is required to give the mobile time to listen
    to connect to downlink PDCH.

   TBF_ST_FLOW,
    During packet flow, this state indicates downlink and uplink TBF block
    flow.

   TBF_ST_FINISHED,
    Uplink TBF:
    After final block is received AND all other blocks are completely
    received, the state is entered. The PACKET CONTROL ACK is still not
    received. (Counter N3103 is counted on each poll request.)
    Downlink TBF:
    All blocks including the final block has been transmitted. Not all
    downlink blocks are acknowledged yet. (Counter N3015 is counted on each
    poll request.)

   TBF_ST_WAIT_RELEASE,
    The all blocks on downlink TBF have been acked by mobile. The penalty
    timer T3192 is running on mobile.

   TBF_ST_RELEASING,
    Wait for TFI/USF to be re-used. This state is entered when a counter
    reaches it's maximum and T3169 is running.


When downlink LLC PDU is received:

  If downlink TBF exists for given TLLI, but not in RELEASING state:
    If downlink TBF is in FLOW or FINISHED state:
      Enqueue PDU to LLC Queue of TBF.
      Done.
    If downlink TBF is in WAIT RELEASE state:
      Attach PDU to LLC Frame of TBF.
      Put TBF back into FLOW state.
      Done.
  If downlink TBF does not exists for given TLLI, or in RELEASING state:
    Create new downlink TBF.
    Attach PDU to LLC Frame of TBF.
    If uplink TBF exists for given TLLI, but not in RELEASING state:
      Assign packet channel PACCH.
      Done.
    If uplink TBF does not exists for given TLLI, or in RELEASING state:
      Assign packet channel AGCH.
      Done.

When channel request for uplink TBF is received:

  Create new uplink TBF.
  If channel request was received on RACH
    Assign packet channel AGCH.
    Done
  If channel request was received on PACCH
    Assign packet channel PACCH.
    Done

Handling of LLC Frame of downlink TBF and LLC Queue of downlink TBF:

  If a downlink TBF is created, the LLC PDU is attached to LLC Frame of TBF
  (downlink flow is assigned to MS).

  If a downlink TBF exists, the LLC PDU is enqueued to LLC Queue of TBF.

  During transfer of downlink blocks, the LLC Queue is dequeued whenever all
  segments of LLC Frame were used for generation of downlink blocks. (If a
  block is partly filled with LLC Frame segment, its remaining space is filled
  with the next LLC Frame from the Queue.)

  If the transfer is finished, no more LLC Frames are dequeued from the LLC
  Queue, in case a new frame has arrived during lifetime of TBF.

  If the all downlink blocks have been acknowledged, the LLC Queue is
  dequeued and an existing frame is attached to LLC Frame. If so, the new
  state is FLOW (downlink flow is assigned to MS), otherwise WAIT RELEASE.

  If a new LLC PDU is attached to LLC Frame during WAIT RELEASE state, the
  state is changed to FLOW (downlink flow is assigned to MS).


Handling of LLC Frame on uplink TBF:

  Received uplink blocks are appended to LLC Frame of TBF. If the PDU is
  complete, it is forwarded to the upper layer.


Control TS:
  On uplink or downlink assignment, it is required to have one timeslot that
  can be used to receive and transmit. This timeslot is used at uplink TBF
  to acknowledge and to assign other TBF. This timeslot is used at downlink
  TBF to poll acknowledgement and to assign other TBF.

  The first common TS (first_common_ts) is calculated when channels are
  allocated. After creation of TBF or after assignment to different TS layout,
  the first common TS is used for control TS.

  The first common TS (and so control TS) must not need to be allocated to
  MS as uplink TBF. (E.g. in case of non-available USF for this slot)


Polling:
  In order to poll uplink control block from MS, a special poll state and
  frame number is stored at PDCH UL Controller. The scheduler reads that value
  and will not assign uplink resource for other TBFs at that frame number.

  On receipt of an Uplink RLCMAC block, it's the duty of each specific message
  handler to release the expectancies previously stored in the PDCH UL
  Controller. After the specific handler returns, the generic return path will
  expire all reserved events up to the curent FN for that PDCH, eventually
  calling timeout functions on TBFs and SBAs owning those reservations. Hence,
  the layer 1 interface is expected to provide DATA.ind for each FN block,
  containing data=0 if decoding failed, in order to keep the clock up-to-date in
  upper layers.

  Because polling requires uplink response from MS, the polling must be
  performed at control TS.


Data structures of TBFs and PDCHs:

  There is a global structure for PCU (struct gprs_pcu the_pcu).

  A BTS is created and put into PCU's list for each PCUIF INFO_IND with a unique
  bts_nr received.

  Each BTS structure has 8 TRX structures.

  Each TRX structure has 8 PDCH structures, one for each timeslot.

  Each BTS structure has a list of MS (struct GprsMs).

  Each MS can have 1 UL TBF and 1 DL TBF, and 1 TBF is always assigned to an
  existing GprsMS structure.

  Each BTS also has two linked lists of TBF instances:
   - uplink TBFs
   - downlink TBFs

  Each TBF instance also has:
   - a direction
   - a TFI of range 0..31
   - an array of 8 PDCH structures, one for each assigned timeslot
   - in case of uplink TBF: an array of 8 USFs, one for each assigned timeslot

  Each TRX and all it's PDCH structures also have:
   - an array of 32 uplink TBFs that are assigned to this PDCH
   - an array of 32 downlink TBFs that are assigned to this PDCH

  On creation of a new TBF, it links to all assigned PDCHs.
  Each PDCH links to that TBF. The TBF is added to the list of TBFs.
  In case of uplink TBF: The allocated USFs are stored for each timeslot.

  On release of a TBF, the link to this PDCH is removed from all assigned
  PDCHs. The TBF is removed from the list of TBFs. The TBF is destroyed.

  GprsMs structures are kept alive for a while once they become unused, in order to
  have a cache of MS related information it would otherwise need to acquire
  again once it interacts with the PCU again.
