/* L1 state handling * * Copyright (C) 2010,2012 Avencall * Authors: * Noe Rubinstein * Guillaume Knispel * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include "xhfc.h" #include "xhfc_leb.h" /* Software-implemented timers for the S/T state machine, in milliseconds */ #define T1_MS 100 /* NT activation timer. 100..1000ms according to Cologne/ETSI TBR4 (??) 100ms in Cologne's mISDN driver. */ #define T3_MS 30000 /* TE activation timer. according to I.430 "the value depends on the subscriber loop transmission technique. The worst case value is 30s." XXX in DAHDI's mess, this was 500ms on the first time, and then the worst-case value of 30s, which I guess makes sense. Try to see if any document says to do that and maybe do the same here? Can't seem to understand how mISDN handles the matter. */ #define F6_F7_MS 2 /* Wait before signalling the change to F7. This is done because the XHFC might go to F7 for a short time during a F6->F3 transition when receiving INFO0. The wait time should be "about 1ms", this set to 2 to guarantee that the wait will be long enough. */ enum te_state { F0, F1, F2, F3, F4, F5, F6, F7, F8 }; enum nt_state { G0, G1, G2, G3, G4 }; static void activation_timer_expiry(struct xhfc_span* s); static void signal_f7_transition(struct xhfc_span* s); static void handle_state_change_nt(struct xhfc_span* s); static void handle_state_change_te(struct xhfc_span* s); static void allow_G2_G3_transition(struct xhfc_span* s); static void print_state(struct xhfc_span* s); void start_state_machine(struct xhfc_span* s) { u8 a_su_wr_sta = 0; SET_V_SU_LD_STA(a_su_wr_sta, 0); write_xhfc(s->xhfc, R_SU_SEL, s->port); write_xhfc(s->xhfc, A_SU_WR_STA, 0); } void activate_request(struct xhfc_span* s) { u8 a_su_wr_sta = 0; int sta = GET_V_SU_STA(s->state); int impossible_te = !s->nt && (sta == F2 || sta == F4 || sta == F5 || sta == F5); int impossible_nt = s->nt && (sta == G2 || sta == G3); int no_action_te = !s->nt && (sta == F0 || sta == F6 || sta == F8); if (impossible_te || impossible_nt) { printk(KERN_INFO DRIVER_NAME ": " "activate impossible by definition of layer 1" " in state %c%d on port %d\n", (s->nt ? 'G' : 'F'), sta, portno(s)); return; } else if (no_action_te) { if(DBG_ST && DBG_SPAN(s)) printk(KERN_INFO DRIVER_NAME ": " "no action for activate " "in state %c%d on port %d\n", (s->nt ? 'G' : 'F'), sta, portno(s)); return; } if (DBG_ST && DBG_SPAN(s)) printk(KERN_DEBUG DRIVER_NAME ": activate on port %d\n", portno(s)); SET_V_SU_ACT(a_su_wr_sta, V_SU_ACT_ACTIVATE); write_xhfc(s->xhfc, R_SU_SEL, s->port); write_xhfc(s->xhfc, A_SU_WR_STA, a_su_wr_sta); if (!s->nt && sta == F3 && GET_V_SU_INFO0(s->state)) s->activation_timer = s->xhfc->ticks + T3_MS; /* xref5 */ else s->activation_timer = NOT_RUNNING; /* timer for NT activated by state change handler (xref3) */ } void deactivate_request(struct xhfc_span* s) { u8 a_su_wr_sta = 0; int sta = GET_V_SU_STA(s->state); if (s->nt && (sta == G1 || sta == G4)) { printk(KERN_INFO DRIVER_NAME ": " "deactivate impossible by definition of layer 1" " in state %c%d on port %d\n", (s->nt ? 'G' : 'F'), sta, portno(s)); return; } if(DBG_ST && DBG_SPAN(s)) printk(KERN_DEBUG DRIVER_NAME ": deactivate on port %d\n", portno(s)); SET_V_SU_ACT(a_su_wr_sta, V_SU_ACT_DEACTIVATE); write_xhfc(s->xhfc, R_SU_SEL, s->port); write_xhfc(s->xhfc, A_SU_WR_STA, a_su_wr_sta); if(s->nt && (sta == G2 || sta == G3)) s->activation_timer = NOT_RUNNING; } /* This function is meant to be ran each time an interface changes state; * however, the state change is polled (during the timer interrupt; see * enable_interrupts and xhfc_interrupt). Therefore, it is possible that * two or more state changes happen on the same interface between two * interruptions and that this functions gets only called once. * Additionally, even when using the ST state change interrupt, an interruption * can still be overlooked and lead to one of these conditions. * * However, there is no notable consequence because the actions are either: * * - Starting timers: * - T3 is only started after an activation request, therefore we always now * when T3 has to start (xref5) * - T1 is only started when going to state G2, which can only yield G3 * automatically. This can be disallowed in V_G2_G3_EN so that the timer * can be started before allowing the transition with V_SU_SET_G2_G3. * (xref6) * - Stopping timers: * - T1 is only stopped on G2->G3. * From G3: * - G3->G2: if the timer is already running, stop it (xref4) * - on deactivate requests stop the timer * - T3 is stopped on going to state F7. I don't think it's possible to * guarantee this; however except F2->F7 which can not happen when T3 is * running, every transition to F7 stops the timer, so there is no * incoherent behaviour; * - Changing alarms in DAHDI, which will be done anyway. No point notifying * something that lasts less that 1ms. */ void handle_state_change(struct xhfc_span* s) { struct xhfc* x = s->xhfc; s->prev_state = s->state; write_xhfc(x, R_SU_SEL, s->port); s->state = read_xhfc(x, A_SU_RD_STA); if(DBG_ST && DBG_SPAN(s)) print_state(s); if(unlikely(s->prev_state == s->state)) { printk(KERN_WARNING DRIVER_NAME ": a state change has been " "reported on port %d but it looks like the " "state has not changed?\n", portno(s)); return; } if(s->nt) handle_state_change_nt(s); else handle_state_change_te(s); } static void handle_state_change_nt(struct xhfc_span* s) { switch (GET_V_SU_STA(s->state)) { case G0: case G1: case G4: s->span.alarms = DAHDI_ALARM_RED; break; case G2: s->span.alarms = DAHDI_ALARM_YELLOW; if(s->activation_timer) s->activation_timer = NOT_RUNNING; /* xref4 */ else { /* xref3, this should happen only if the * previous state is not G3 */ s->activation_timer = s->xhfc->ticks + T1_MS; allow_G2_G3_transition(s); /* xref6 */ } break; case G3: s->span.alarms = DAHDI_ALARM_NONE; s->activation_timer = NOT_RUNNING; break; } } static void handle_state_change_te(struct xhfc_span* s) { int sta = GET_V_SU_STA(s->state); if (sta != F7) s->f6_f7_transition = NOT_RUNNING; switch(sta) { case F0: case F1: case F2: case F3: case F4: case F8: s->span.alarms = DAHDI_ALARM_RED; break; case F5: case F6: s->span.alarms = DAHDI_ALARM_YELLOW; break; case F7: s->activation_timer = NOT_RUNNING; if ( GET_V_SU_STA(s->prev_state) == F6 && GET_V_SU_INFO0(s->state)) { s->f6_f7_transition = s->xhfc->ticks + T1_MS; break; } signal_f7_transition(s); break; } } void handle_st_timers(struct xhfc_span* s) { struct xhfc* x = s->xhfc; if(unlikely(s->activation_timer && s->activation_timer <= x->ticks)) activation_timer_expiry(s); if(unlikely(s->f6_f7_transition && s->f6_f7_transition <= x->ticks)) signal_f7_transition(s); } static void signal_f7_transition(struct xhfc_span* s) { if(DBG_ST && DBG_SPAN(s)) printk(KERN_DEBUG DRIVER_NAME ": signalling transition to F7 " "on port %d\n", portno(s)); s->span.alarms = DAHDI_ALARM_NONE; s->f6_f7_transition = NOT_RUNNING; } static void activation_timer_expiry(struct xhfc_span* s) { int sta = GET_V_SU_STA(s->state); int t1_expiry = s->nt && sta == G2; int t3_expiry = !s->nt && (sta == F4 || sta == F5 || sta == F8); if(DBG_ST && DBG_SPAN(s)) printk(KERN_INFO DRIVER_NAME ": %s%s activation timeout" " in state %c%d on port %d\n", t1_expiry || t3_expiry ? "" : "no action for ", s->nt ? "NT" : "TE", s->nt ? 'G' : 'F', sta, portno(s)); s->activation_timer = NOT_RUNNING; if (t1_expiry || t3_expiry) deactivate_request(s); } static void allow_G2_G3_transition(struct xhfc_span* s) { u8 a_su_wr_sta = 0; SET_V_SU_SET_G2_G3(a_su_wr_sta, 1); write_xhfc(s->xhfc, R_SU_SEL, s->port); write_xhfc(s->xhfc, A_SU_WR_STA, a_su_wr_sta); /* The bit is automatically cleared on G2->G3. G2->G4 is always * triggered by using activate_request which clears this bit. Therefore * this bit is always cleared correctly when exiting G2. */ } static char *state_description[2][16] = { { /* TE */ /* F0 */ "reset", /* F1 */ "inactive", /* F2 */ "sensing", /* F3 */ "deactivate", /* F4 */ "awaiting signal", /* F5 */ "identifying input", /* F6 */ "synchronized", /* F7 */ "activated", /* F8 */ "lost framing", "?", "?", "?", "?", "?", "?", "?" }, { /* NT */ /* G0 */ "reset", /* G1 */ "deactivated", /* G2 */ "pending activation", /* G3 */ "active", /* G4 */ "pending deactivation", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?" }}; static void print_state(struct xhfc_span* s) { int sta = GET_V_SU_STA(s->state); printk(KERN_DEBUG DRIVER_NAME ": port %d: %s state %c%d (%s)\n", portno(s), (s->nt ? "NT" : "TE"), (s->nt ? 'G' : 'F'), sta, state_description[s->nt][sta]); } #if 0 /* This is not used right now, but it could be in the future */ static void set_st_state(struct xhfc_span* s, int state) { u8 a_su_wr_sta = 0; s->prev_state = s->state; s->state = state; if(DBG_ST && DBG_SPAN(s)) { printk(KERN_DEBUG DRIVER_NAME "port %d: " "Forcing state change:\n", s->port +1); print_state(s); } SET_V_SU_SET_STA(a_su_wr_sta, state); SET_V_SU_LD_STA(a_su_wr_sta, 1); write_xhfc(s->xhfc, R_SU_SEL, s->port); write_xhfc(s->xhfc, A_SU_WR_STA, a_su_wr_sta); udelay(6); SET_V_SU_LD_STA(a_su_wr_sta, 0); write_xhfc(s->xhfc, R_SU_SEL, s->port); /* just in case. (XXX?) */ write_xhfc(s->xhfc, A_SU_WR_STA, a_su_wr_sta); } #endif