1 /******************************************************************************
3 * (C)Copyright 1998,1999 SysKonnect,
4 * a business unit of Schneider & Koch & Co. Datensysteme GmbH.
6 * See the file "skfddi.c" for further information.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * The information in this file is provided "AS IS" without warranty.
15 ******************************************************************************/
23 * Hardware independent state machine implemantation
24 * The following external SMT functions are referenced :
30 * The following external HW dependent functions are referenced :
32 * sm_mac_check_beacon_claim()
34 * The following HW dependent events are required :
50 #include "h/smtstate.h"
53 static const char ID_sccs[] = "@(#)rmt.c 2.13 99/07/02 (C) SK " ;
60 #define GO_STATE(x) (smc->mib.m[MAC0].fddiMACRMTState = (x)|AFLAG)
61 #define ACTIONS_DONE() (smc->mib.m[MAC0].fddiMACRMTState &= ~AFLAG)
62 #define ACTIONS(x) (x|AFLAG)
64 #define RM0_ISOLATED 0
65 #define RM1_NON_OP 1 /* not operational */
66 #define RM2_RING_OP 2 /* ring operational */
67 #define RM3_DETECT 3 /* detect dupl addresses */
68 #define RM4_NON_OP_DUP 4 /* dupl. addr detected */
69 #define RM5_RING_OP_DUP 5 /* ring oper. with dupl. addr */
70 #define RM6_DIRECTED 6 /* sending directed beacons */
71 #define RM7_TRACE 7 /* trace initiated */
75 * symbolic state names
77 static const char * const rmt_states[] = {
78 "RM0_ISOLATED","RM1_NON_OP","RM2_RING_OP","RM3_DETECT",
79 "RM4_NON_OP_DUP","RM5_RING_OP_DUP","RM6_DIRECTED",
84 * symbolic event names
86 static const char * const rmt_events[] = {
87 "NONE","RM_RING_OP","RM_RING_NON_OP","RM_MY_BEACON",
88 "RM_OTHER_BEACON","RM_MY_CLAIM","RM_TRT_EXP","RM_VALID_CLAIM",
89 "RM_JOIN","RM_LOOP","RM_DUP_ADDR","RM_ENABLE_FLAG",
90 "RM_TIMEOUT_NON_OP","RM_TIMEOUT_T_STUCK",
91 "RM_TIMEOUT_ANNOUNCE","RM_TIMEOUT_T_DIRECT",
92 "RM_TIMEOUT_D_MAX","RM_TIMEOUT_POLL","RM_TX_STATE_CHANGE"
103 * function declarations
105 static void rmt_fsm() ;
106 static void start_rmt_timer0() ;
107 static void start_rmt_timer1() ;
108 static void start_rmt_timer2() ;
109 static void stop_rmt_timer0() ;
110 static void stop_rmt_timer1() ;
111 static void stop_rmt_timer2() ;
112 static void rmt_dup_actions() ;
113 static void rmt_reinsert_actions() ;
114 static void rmt_leave_actions() ;
115 static void rmt_new_dup_actions() ;
118 extern void restart_trt_for_dbcn() ;
119 #endif /*SUPERNET_3*/
122 init RMT state machine
123 clear all RMT vars and flags
128 smc->mib.m[MAC0].fddiMACRMTState = ACTIONS(RM0_ISOLATED) ;
129 smc->r.dup_addr_test = DA_NONE ;
131 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = FALSE ;
132 smc->r.sm_ma_avail = FALSE ;
133 smc->r.loop_avail = 0 ;
136 smc->r.no_flag = TRUE ;
155 DB_RMT("RMT : state %s%s",
156 (smc->mib.m[MAC0].fddiMACRMTState & AFLAG) ? "ACTIONS " : "",
157 rmt_states[smc->mib.m[MAC0].fddiMACRMTState & ~AFLAG]) ;
158 DB_RMT(" event %s\n",rmt_events[event],0) ;
159 state = smc->mib.m[MAC0].fddiMACRMTState ;
162 } while (state != smc->mib.m[MAC0].fddiMACRMTState) ;
163 rmt_state_change(smc,(int)smc->mib.m[MAC0].fddiMACRMTState) ;
169 static void rmt_fsm(smc,cmd)
174 * RM00-RM70 : from all states
176 if (!smc->r.rm_join && !smc->r.rm_loop &&
177 smc->mib.m[MAC0].fddiMACRMTState != ACTIONS(RM0_ISOLATED) &&
178 smc->mib.m[MAC0].fddiMACRMTState != RM0_ISOLATED) {
179 RS_SET(smc,RS_NORINGOP) ;
180 rmt_indication(smc,0) ;
181 GO_STATE(RM0_ISOLATED) ;
185 switch(smc->mib.m[MAC0].fddiMACRMTState) {
186 case ACTIONS(RM0_ISOLATED) :
187 stop_rmt_timer0(smc) ;
188 stop_rmt_timer1(smc) ;
189 stop_rmt_timer2(smc) ;
194 sm_ma_control(smc,MA_OFFLINE) ;
195 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = FALSE ;
196 smc->r.loop_avail = FALSE ;
197 smc->r.sm_ma_avail = FALSE ;
198 smc->r.no_flag = TRUE ;
199 DB_RMTN(1,"RMT : ISOLATED\n",0,0) ;
204 if (smc->r.rm_join || smc->r.rm_loop) {
206 * According to the standard the MAC must be reset
207 * here. The FORMAC will be initialized and Claim
208 * and Beacon Frames will be uploaded to the MAC.
209 * So any change of Treq will take effect NOW.
211 sm_ma_control(smc,MA_RESET) ;
212 GO_STATE(RM1_NON_OP) ;
216 case ACTIONS(RM1_NON_OP) :
217 start_rmt_timer0(smc,smc->s.rmt_t_non_op,RM_TIMEOUT_NON_OP) ;
218 stop_rmt_timer1(smc) ;
219 stop_rmt_timer2(smc) ;
220 sm_ma_control(smc,MA_BEACON) ;
221 DB_RMTN(1,"RMT : RING DOWN\n",0,0) ;
222 RS_SET(smc,RS_NORINGOP) ;
223 smc->r.sm_ma_avail = FALSE ;
224 rmt_indication(smc,0) ;
229 if (cmd == RM_RING_OP) {
230 RS_SET(smc,RS_RINGOPCHANGE) ;
231 GO_STATE(RM2_RING_OP) ;
235 else if (cmd == RM_TIMEOUT_NON_OP) {
236 smc->r.bn_flag = FALSE ;
237 smc->r.no_flag = TRUE ;
238 GO_STATE(RM3_DETECT) ;
242 case ACTIONS(RM2_RING_OP) :
243 stop_rmt_timer0(smc) ;
244 stop_rmt_timer1(smc) ;
245 stop_rmt_timer2(smc) ;
246 smc->r.no_flag = FALSE ;
248 smc->r.loop_avail = TRUE ;
249 if (smc->r.rm_join) {
250 smc->r.sm_ma_avail = TRUE ;
251 if (smc->mib.m[MAC0].fddiMACMA_UnitdataEnable)
252 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = TRUE ;
254 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = FALSE ;
256 DB_RMTN(1,"RMT : RING UP\n",0,0) ;
257 RS_CLEAR(smc,RS_NORINGOP) ;
258 RS_SET(smc,RS_RINGOPCHANGE) ;
259 rmt_indication(smc,1) ;
260 smt_stat_counter(smc,0) ;
265 if (cmd == RM_RING_NON_OP) {
266 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = FALSE ;
267 smc->r.loop_avail = FALSE ;
268 RS_SET(smc,RS_RINGOPCHANGE) ;
269 GO_STATE(RM1_NON_OP) ;
273 else if (cmd == RM_ENABLE_FLAG) {
274 if (smc->mib.m[MAC0].fddiMACMA_UnitdataEnable)
275 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = TRUE ;
277 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = FALSE ;
280 else if (smc->r.dup_addr_test == DA_FAILED) {
281 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = FALSE ;
282 smc->r.loop_avail = FALSE ;
283 smc->r.da_flag = TRUE ;
284 GO_STATE(RM5_RING_OP_DUP) ;
288 case ACTIONS(RM3_DETECT) :
289 start_rmt_timer0(smc,smc->s.mac_d_max*2,RM_TIMEOUT_D_MAX) ;
290 start_rmt_timer1(smc,smc->s.rmt_t_stuck,RM_TIMEOUT_T_STUCK) ;
291 start_rmt_timer2(smc,smc->s.rmt_t_poll,RM_TIMEOUT_POLL) ;
292 sm_mac_check_beacon_claim(smc) ;
293 DB_RMTN(1,"RMT : RM3_DETECT\n",0,0) ;
297 if (cmd == RM_TIMEOUT_POLL) {
298 start_rmt_timer2(smc,smc->s.rmt_t_poll,RM_TIMEOUT_POLL);
299 sm_mac_check_beacon_claim(smc) ;
302 if (cmd == RM_TIMEOUT_D_MAX) {
303 smc->r.timer0_exp = TRUE ;
307 * We need a time ">= 2*mac_d_max" since we had finished
308 * Claim or Beacon state. So we will restart timer0 at
309 * every state change.
311 if (cmd == RM_TX_STATE_CHANGE) {
312 start_rmt_timer0(smc,
317 if (cmd == RM_RING_OP) {
318 GO_STATE(RM2_RING_OP) ;
322 else if ((cmd == RM_MY_BEACON || cmd == RM_OTHER_BEACON)
324 smc->r.bn_flag = FALSE ;
327 else if (cmd == RM_TRT_EXP && !smc->r.bn_flag) {
330 * set bn_flag only if in state T4 or T5:
331 * only if we're the beaconer should we start the
334 if ((tx = sm_mac_get_tx_state(smc)) == 4 || tx == 5) {
335 DB_RMTN(2,"RMT : DETECT && TRT_EXPIRED && T4/T5\n",0,0);
336 smc->r.bn_flag = TRUE ;
338 * If one of the upstream stations beaconed
339 * and the link to the upstream neighbor is
340 * lost we need to restart the stuck timer to
341 * check the "stuck beacon" condition.
343 start_rmt_timer1(smc,smc->s.rmt_t_stuck,
344 RM_TIMEOUT_T_STUCK) ;
347 * We do NOT need to clear smc->r.bn_flag in case of
348 * not being in state T4 or T5, because the flag
349 * must be cleared in order to get in this condition.
353 "RMT : sm_mac_get_tx_state() = %d (bn_flag = %d)\n",
357 else if (cmd == RM_MY_CLAIM && smc->r.timer0_exp) {
358 rmt_new_dup_actions(smc) ;
359 GO_STATE(RM4_NON_OP_DUP) ;
363 else if (cmd == RM_MY_BEACON && smc->r.timer0_exp) {
364 rmt_new_dup_actions(smc) ;
365 GO_STATE(RM4_NON_OP_DUP) ;
369 else if (cmd == RM_VALID_CLAIM) {
370 rmt_new_dup_actions(smc) ;
371 GO_STATE(RM4_NON_OP_DUP) ;
375 else if (cmd == RM_TIMEOUT_T_STUCK &&
376 smc->r.rm_join && smc->r.bn_flag) {
377 GO_STATE(RM6_DIRECTED) ;
381 case ACTIONS(RM4_NON_OP_DUP) :
382 start_rmt_timer0(smc,smc->s.rmt_t_announce,RM_TIMEOUT_ANNOUNCE);
383 start_rmt_timer1(smc,smc->s.rmt_t_stuck,RM_TIMEOUT_T_STUCK) ;
384 start_rmt_timer2(smc,smc->s.rmt_t_poll,RM_TIMEOUT_POLL) ;
385 sm_mac_check_beacon_claim(smc) ;
386 DB_RMTN(1,"RMT : RM4_NON_OP_DUP\n",0,0) ;
389 case RM4_NON_OP_DUP :
390 if (cmd == RM_TIMEOUT_POLL) {
391 start_rmt_timer2(smc,smc->s.rmt_t_poll,RM_TIMEOUT_POLL);
392 sm_mac_check_beacon_claim(smc) ;
396 if (!smc->r.da_flag) {
397 GO_STATE(RM1_NON_OP) ;
401 else if ((cmd == RM_MY_BEACON || cmd == RM_OTHER_BEACON) &&
403 smc->r.bn_flag = FALSE ;
406 else if (cmd == RM_TRT_EXP && !smc->r.bn_flag) {
409 * set bn_flag only if in state T4 or T5:
410 * only if we're the beaconer should we start the
413 if ((tx = sm_mac_get_tx_state(smc)) == 4 || tx == 5) {
414 DB_RMTN(2,"RMT : NOPDUP && TRT_EXPIRED && T4/T5\n",0,0);
415 smc->r.bn_flag = TRUE ;
417 * If one of the upstream stations beaconed
418 * and the link to the upstream neighbor is
419 * lost we need to restart the stuck timer to
420 * check the "stuck beacon" condition.
422 start_rmt_timer1(smc,smc->s.rmt_t_stuck,
423 RM_TIMEOUT_T_STUCK) ;
426 * We do NOT need to clear smc->r.bn_flag in case of
427 * not being in state T4 or T5, because the flag
428 * must be cleared in order to get in this condition.
432 "RMT : sm_mac_get_tx_state() = %d (bn_flag = %d)\n",
436 else if (cmd == RM_TIMEOUT_ANNOUNCE && !smc->r.bn_flag) {
437 rmt_dup_actions(smc) ;
440 else if (cmd == RM_RING_OP) {
441 smc->r.no_flag = FALSE ;
442 GO_STATE(RM5_RING_OP_DUP) ;
446 else if (cmd == RM_TIMEOUT_T_STUCK &&
447 smc->r.rm_join && smc->r.bn_flag) {
448 GO_STATE(RM6_DIRECTED) ;
452 case ACTIONS(RM5_RING_OP_DUP) :
453 stop_rmt_timer0(smc) ;
454 stop_rmt_timer1(smc) ;
455 stop_rmt_timer2(smc) ;
456 DB_RMTN(1,"RMT : RM5_RING_OP_DUP\n",0,0) ;
459 case RM5_RING_OP_DUP :
461 if (smc->r.dup_addr_test == DA_PASSED) {
462 smc->r.da_flag = FALSE ;
463 GO_STATE(RM2_RING_OP) ;
467 else if (cmd == RM_RING_NON_OP) {
468 smc->r.jm_flag = FALSE ;
469 smc->r.bn_flag = FALSE ;
470 GO_STATE(RM4_NON_OP_DUP) ;
474 case ACTIONS(RM6_DIRECTED) :
475 start_rmt_timer0(smc,smc->s.rmt_t_direct,RM_TIMEOUT_T_DIRECT) ;
476 stop_rmt_timer1(smc) ;
477 start_rmt_timer2(smc,smc->s.rmt_t_poll,RM_TIMEOUT_POLL) ;
478 sm_ma_control(smc,MA_DIRECTED) ;
479 RS_SET(smc,RS_BEACON) ;
480 DB_RMTN(1,"RMT : RM6_DIRECTED\n",0,0) ;
485 if (cmd == RM_TIMEOUT_POLL) {
486 start_rmt_timer2(smc,smc->s.rmt_t_poll,RM_TIMEOUT_POLL);
487 sm_mac_check_beacon_claim(smc) ;
489 /* Because of problems with the Supernet II chip set
490 * sending of Directed Beacon will stop after 165ms
491 * therefore restart_trt_for_dbcn(smc) will be called
494 restart_trt_for_dbcn(smc) ;
495 #endif /*SUPERNET_3*/
498 if ((cmd == RM_MY_BEACON || cmd == RM_OTHER_BEACON) &&
500 smc->r.bn_flag = FALSE ;
501 GO_STATE(RM3_DETECT) ;
505 else if ((cmd == RM_MY_BEACON || cmd == RM_OTHER_BEACON) &&
507 smc->r.bn_flag = FALSE ;
508 GO_STATE(RM4_NON_OP_DUP) ;
512 else if (cmd == RM_TIMEOUT_T_DIRECT) {
513 GO_STATE(RM7_TRACE) ;
517 case ACTIONS(RM7_TRACE) :
518 stop_rmt_timer0(smc) ;
519 stop_rmt_timer1(smc) ;
520 stop_rmt_timer2(smc) ;
521 smc->e.trace_prop |= ENTITY_BIT(ENTITY_MAC) ;
522 queue_event(smc,EVENT_ECM,EC_TRACE_PROP) ;
523 DB_RMTN(1,"RMT : RM7_TRACE\n",0,0) ;
529 SMT_PANIC(smc,SMT_E0122, SMT_E0122_MSG) ;
535 * (jd) RMT duplicate address actions
536 * leave the ring or reinsert just as configured
538 static void rmt_dup_actions(smc)
541 if (smc->r.jm_flag) {
544 if (smc->s.rmt_dup_mac_behavior) {
545 SMT_ERR_LOG(smc,SMT_E0138, SMT_E0138_MSG) ;
546 rmt_reinsert_actions(smc) ;
549 SMT_ERR_LOG(smc,SMT_E0135, SMT_E0135_MSG) ;
550 rmt_leave_actions(smc) ;
556 * Reconnect to the Ring
558 static void rmt_reinsert_actions(smc)
561 queue_event(smc,EVENT_ECM,EC_DISCONNECT) ;
562 queue_event(smc,EVENT_ECM,EC_CONNECT) ;
566 * duplicate address detected
568 static void rmt_new_dup_actions(smc)
571 smc->r.da_flag = TRUE ;
572 smc->r.bn_flag = FALSE ;
573 smc->r.jm_flag = FALSE ;
575 * we have three options : change address, jam or leave
576 * we leave the ring as default
577 * Optionally it's possible to reinsert after leaving the Ring
578 * but this will not conform with SMT Spec.
580 if (smc->s.rmt_dup_mac_behavior) {
581 SMT_ERR_LOG(smc,SMT_E0138, SMT_E0138_MSG) ;
582 rmt_reinsert_actions(smc) ;
585 SMT_ERR_LOG(smc,SMT_E0135, SMT_E0135_MSG) ;
586 rmt_leave_actions(smc) ;
594 static void rmt_leave_actions(smc)
597 queue_event(smc,EVENT_ECM,EC_DISCONNECT) ;
599 * Note: Do NOT try again later. (with please reconnect)
600 * The station must be left from the ring!
605 * SMT timer interface
608 static void start_rmt_timer0(smc,value,event)
613 smc->r.timer0_exp = FALSE ; /* clear timer event flag */
614 smt_timer_start(smc,&smc->r.rmt_timer0,value,EV_TOKEN(EVENT_RMT,event));
618 * SMT timer interface
621 static void start_rmt_timer1(smc,value,event)
626 smc->r.timer1_exp = FALSE ; /* clear timer event flag */
627 smt_timer_start(smc,&smc->r.rmt_timer1,value,EV_TOKEN(EVENT_RMT,event));
631 * SMT timer interface
634 static void start_rmt_timer2(smc,value,event)
639 smc->r.timer2_exp = FALSE ; /* clear timer event flag */
640 smt_timer_start(smc,&smc->r.rmt_timer2,value,EV_TOKEN(EVENT_RMT,event));
644 * SMT timer interface
647 static void stop_rmt_timer0(smc)
650 if (smc->r.rmt_timer0.tm_active)
651 smt_timer_stop(smc,&smc->r.rmt_timer0) ;
655 * SMT timer interface
658 static void stop_rmt_timer1(smc)
661 if (smc->r.rmt_timer1.tm_active)
662 smt_timer_stop(smc,&smc->r.rmt_timer1) ;
666 * SMT timer interface
669 static void stop_rmt_timer2(smc)
672 if (smc->r.rmt_timer2.tm_active)
673 smt_timer_stop(smc,&smc->r.rmt_timer2) ;