1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /******************************************************************************
4 * (C)Copyright 1998,1999 SysKonnect,
5 * a business unit of Schneider & Koch & Co. Datensysteme GmbH.
7 * See the file "skfddi.c" for further information.
9 * The information in this file is provided "AS IS" without warranty.
11 ******************************************************************************/
19 * Hardware independent state machine implemantation
20 * The following external SMT functions are referenced :
26 * The following external HW dependent functions are referenced :
28 * sm_mac_check_beacon_claim()
30 * The following HW dependent events are required :
46 #include "h/smtstate.h"
49 static const char ID_sccs[] = "@(#)rmt.c 2.13 99/07/02 (C) SK " ;
56 #define GO_STATE(x) (smc->mib.m[MAC0].fddiMACRMTState = (x)|AFLAG)
57 #define ACTIONS_DONE() (smc->mib.m[MAC0].fddiMACRMTState &= ~AFLAG)
58 #define ACTIONS(x) (x|AFLAG)
60 #define RM0_ISOLATED 0
61 #define RM1_NON_OP 1 /* not operational */
62 #define RM2_RING_OP 2 /* ring operational */
63 #define RM3_DETECT 3 /* detect dupl addresses */
64 #define RM4_NON_OP_DUP 4 /* dupl. addr detected */
65 #define RM5_RING_OP_DUP 5 /* ring oper. with dupl. addr */
66 #define RM6_DIRECTED 6 /* sending directed beacons */
67 #define RM7_TRACE 7 /* trace initiated */
70 * symbolic state names
72 static const char * const rmt_states[] = {
73 "RM0_ISOLATED","RM1_NON_OP","RM2_RING_OP","RM3_DETECT",
74 "RM4_NON_OP_DUP","RM5_RING_OP_DUP","RM6_DIRECTED",
79 * symbolic event names
81 static const char * const rmt_events[] = {
82 "NONE","RM_RING_OP","RM_RING_NON_OP","RM_MY_BEACON",
83 "RM_OTHER_BEACON","RM_MY_CLAIM","RM_TRT_EXP","RM_VALID_CLAIM",
84 "RM_JOIN","RM_LOOP","RM_DUP_ADDR","RM_ENABLE_FLAG",
85 "RM_TIMEOUT_NON_OP","RM_TIMEOUT_T_STUCK",
86 "RM_TIMEOUT_ANNOUNCE","RM_TIMEOUT_T_DIRECT",
87 "RM_TIMEOUT_D_MAX","RM_TIMEOUT_POLL","RM_TX_STATE_CHANGE"
97 * function declarations
99 static void rmt_fsm(struct s_smc *smc, int cmd);
100 static void start_rmt_timer0(struct s_smc *smc, u_long value, int event);
101 static void start_rmt_timer1(struct s_smc *smc, u_long value, int event);
102 static void start_rmt_timer2(struct s_smc *smc, u_long value, int event);
103 static void stop_rmt_timer0(struct s_smc *smc);
104 static void stop_rmt_timer1(struct s_smc *smc);
105 static void stop_rmt_timer2(struct s_smc *smc);
106 static void rmt_dup_actions(struct s_smc *smc);
107 static void rmt_reinsert_actions(struct s_smc *smc);
108 static void rmt_leave_actions(struct s_smc *smc);
109 static void rmt_new_dup_actions(struct s_smc *smc);
112 extern void restart_trt_for_dbcn() ;
113 #endif /*SUPERNET_3*/
116 init RMT state machine
117 clear all RMT vars and flags
119 void rmt_init(struct s_smc *smc)
121 smc->mib.m[MAC0].fddiMACRMTState = ACTIONS(RM0_ISOLATED) ;
122 smc->r.dup_addr_test = DA_NONE ;
124 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = FALSE ;
125 smc->r.sm_ma_avail = FALSE ;
126 smc->r.loop_avail = 0 ;
129 smc->r.no_flag = TRUE ;
141 void rmt(struct s_smc *smc, int event)
146 DB_RMT("RMT : state %s%s event %s",
147 smc->mib.m[MAC0].fddiMACRMTState & AFLAG ? "ACTIONS " : "",
148 rmt_states[smc->mib.m[MAC0].fddiMACRMTState & ~AFLAG],
150 state = smc->mib.m[MAC0].fddiMACRMTState ;
153 } while (state != smc->mib.m[MAC0].fddiMACRMTState) ;
154 rmt_state_change(smc,(int)smc->mib.m[MAC0].fddiMACRMTState) ;
160 static void rmt_fsm(struct s_smc *smc, int cmd)
163 * RM00-RM70 : from all states
165 if (!smc->r.rm_join && !smc->r.rm_loop &&
166 smc->mib.m[MAC0].fddiMACRMTState != ACTIONS(RM0_ISOLATED) &&
167 smc->mib.m[MAC0].fddiMACRMTState != RM0_ISOLATED) {
168 RS_SET(smc,RS_NORINGOP) ;
169 rmt_indication(smc,0) ;
170 GO_STATE(RM0_ISOLATED) ;
174 switch(smc->mib.m[MAC0].fddiMACRMTState) {
175 case ACTIONS(RM0_ISOLATED) :
176 stop_rmt_timer0(smc) ;
177 stop_rmt_timer1(smc) ;
178 stop_rmt_timer2(smc) ;
183 sm_ma_control(smc,MA_OFFLINE) ;
184 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = FALSE ;
185 smc->r.loop_avail = FALSE ;
186 smc->r.sm_ma_avail = FALSE ;
187 smc->r.no_flag = TRUE ;
188 DB_RMTN(1, "RMT : ISOLATED");
193 if (smc->r.rm_join || smc->r.rm_loop) {
195 * According to the standard the MAC must be reset
196 * here. The FORMAC will be initialized and Claim
197 * and Beacon Frames will be uploaded to the MAC.
198 * So any change of Treq will take effect NOW.
200 sm_ma_control(smc,MA_RESET) ;
201 GO_STATE(RM1_NON_OP) ;
205 case ACTIONS(RM1_NON_OP) :
206 start_rmt_timer0(smc,smc->s.rmt_t_non_op,RM_TIMEOUT_NON_OP) ;
207 stop_rmt_timer1(smc) ;
208 stop_rmt_timer2(smc) ;
209 sm_ma_control(smc,MA_BEACON) ;
210 DB_RMTN(1, "RMT : RING DOWN");
211 RS_SET(smc,RS_NORINGOP) ;
212 smc->r.sm_ma_avail = FALSE ;
213 rmt_indication(smc,0) ;
218 if (cmd == RM_RING_OP) {
219 RS_SET(smc,RS_RINGOPCHANGE) ;
220 GO_STATE(RM2_RING_OP) ;
224 else if (cmd == RM_TIMEOUT_NON_OP) {
225 smc->r.bn_flag = FALSE ;
226 smc->r.no_flag = TRUE ;
227 GO_STATE(RM3_DETECT) ;
231 case ACTIONS(RM2_RING_OP) :
232 stop_rmt_timer0(smc) ;
233 stop_rmt_timer1(smc) ;
234 stop_rmt_timer2(smc) ;
235 smc->r.no_flag = FALSE ;
237 smc->r.loop_avail = TRUE ;
238 if (smc->r.rm_join) {
239 smc->r.sm_ma_avail = TRUE ;
240 if (smc->mib.m[MAC0].fddiMACMA_UnitdataEnable)
241 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = TRUE ;
243 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = FALSE ;
245 DB_RMTN(1, "RMT : RING UP");
246 RS_CLEAR(smc,RS_NORINGOP) ;
247 RS_SET(smc,RS_RINGOPCHANGE) ;
248 rmt_indication(smc,1) ;
249 smt_stat_counter(smc,0) ;
254 if (cmd == RM_RING_NON_OP) {
255 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = FALSE ;
256 smc->r.loop_avail = FALSE ;
257 RS_SET(smc,RS_RINGOPCHANGE) ;
258 GO_STATE(RM1_NON_OP) ;
262 else if (cmd == RM_ENABLE_FLAG) {
263 if (smc->mib.m[MAC0].fddiMACMA_UnitdataEnable)
264 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = TRUE ;
266 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = FALSE ;
269 else if (smc->r.dup_addr_test == DA_FAILED) {
270 smc->mib.m[MAC0].fddiMACMA_UnitdataAvailable = FALSE ;
271 smc->r.loop_avail = FALSE ;
272 smc->r.da_flag = TRUE ;
273 GO_STATE(RM5_RING_OP_DUP) ;
277 case ACTIONS(RM3_DETECT) :
278 start_rmt_timer0(smc,smc->s.mac_d_max*2,RM_TIMEOUT_D_MAX) ;
279 start_rmt_timer1(smc,smc->s.rmt_t_stuck,RM_TIMEOUT_T_STUCK) ;
280 start_rmt_timer2(smc,smc->s.rmt_t_poll,RM_TIMEOUT_POLL) ;
281 sm_mac_check_beacon_claim(smc) ;
282 DB_RMTN(1, "RMT : RM3_DETECT");
286 if (cmd == RM_TIMEOUT_POLL) {
287 start_rmt_timer2(smc,smc->s.rmt_t_poll,RM_TIMEOUT_POLL);
288 sm_mac_check_beacon_claim(smc) ;
291 if (cmd == RM_TIMEOUT_D_MAX) {
292 smc->r.timer0_exp = TRUE ;
296 * We need a time ">= 2*mac_d_max" since we had finished
297 * Claim or Beacon state. So we will restart timer0 at
298 * every state change.
300 if (cmd == RM_TX_STATE_CHANGE) {
301 start_rmt_timer0(smc,
306 if (cmd == RM_RING_OP) {
307 GO_STATE(RM2_RING_OP) ;
311 else if ((cmd == RM_MY_BEACON || cmd == RM_OTHER_BEACON)
313 smc->r.bn_flag = FALSE ;
316 else if (cmd == RM_TRT_EXP && !smc->r.bn_flag) {
319 * set bn_flag only if in state T4 or T5:
320 * only if we're the beaconer should we start the
323 if ((tx = sm_mac_get_tx_state(smc)) == 4 || tx == 5) {
324 DB_RMTN(2, "RMT : DETECT && TRT_EXPIRED && T4/T5");
325 smc->r.bn_flag = TRUE ;
327 * If one of the upstream stations beaconed
328 * and the link to the upstream neighbor is
329 * lost we need to restart the stuck timer to
330 * check the "stuck beacon" condition.
332 start_rmt_timer1(smc,smc->s.rmt_t_stuck,
333 RM_TIMEOUT_T_STUCK) ;
336 * We do NOT need to clear smc->r.bn_flag in case of
337 * not being in state T4 or T5, because the flag
338 * must be cleared in order to get in this condition.
341 DB_RMTN(2, "RMT : sm_mac_get_tx_state() = %d (bn_flag = %d)",
345 else if (cmd == RM_MY_CLAIM && smc->r.timer0_exp) {
346 rmt_new_dup_actions(smc) ;
347 GO_STATE(RM4_NON_OP_DUP) ;
351 else if (cmd == RM_MY_BEACON && smc->r.timer0_exp) {
352 rmt_new_dup_actions(smc) ;
353 GO_STATE(RM4_NON_OP_DUP) ;
357 else if (cmd == RM_VALID_CLAIM) {
358 rmt_new_dup_actions(smc) ;
359 GO_STATE(RM4_NON_OP_DUP) ;
363 else if (cmd == RM_TIMEOUT_T_STUCK &&
364 smc->r.rm_join && smc->r.bn_flag) {
365 GO_STATE(RM6_DIRECTED) ;
369 case ACTIONS(RM4_NON_OP_DUP) :
370 start_rmt_timer0(smc,smc->s.rmt_t_announce,RM_TIMEOUT_ANNOUNCE);
371 start_rmt_timer1(smc,smc->s.rmt_t_stuck,RM_TIMEOUT_T_STUCK) ;
372 start_rmt_timer2(smc,smc->s.rmt_t_poll,RM_TIMEOUT_POLL) ;
373 sm_mac_check_beacon_claim(smc) ;
374 DB_RMTN(1, "RMT : RM4_NON_OP_DUP");
377 case RM4_NON_OP_DUP :
378 if (cmd == RM_TIMEOUT_POLL) {
379 start_rmt_timer2(smc,smc->s.rmt_t_poll,RM_TIMEOUT_POLL);
380 sm_mac_check_beacon_claim(smc) ;
384 if (!smc->r.da_flag) {
385 GO_STATE(RM1_NON_OP) ;
389 else if ((cmd == RM_MY_BEACON || cmd == RM_OTHER_BEACON) &&
391 smc->r.bn_flag = FALSE ;
394 else if (cmd == RM_TRT_EXP && !smc->r.bn_flag) {
397 * set bn_flag only if in state T4 or T5:
398 * only if we're the beaconer should we start the
401 if ((tx = sm_mac_get_tx_state(smc)) == 4 || tx == 5) {
402 DB_RMTN(2, "RMT : NOPDUP && TRT_EXPIRED && T4/T5");
403 smc->r.bn_flag = TRUE ;
405 * If one of the upstream stations beaconed
406 * and the link to the upstream neighbor is
407 * lost we need to restart the stuck timer to
408 * check the "stuck beacon" condition.
410 start_rmt_timer1(smc,smc->s.rmt_t_stuck,
411 RM_TIMEOUT_T_STUCK) ;
414 * We do NOT need to clear smc->r.bn_flag in case of
415 * not being in state T4 or T5, because the flag
416 * must be cleared in order to get in this condition.
419 DB_RMTN(2, "RMT : sm_mac_get_tx_state() = %d (bn_flag = %d)",
423 else if (cmd == RM_TIMEOUT_ANNOUNCE && !smc->r.bn_flag) {
424 rmt_dup_actions(smc) ;
427 else if (cmd == RM_RING_OP) {
428 smc->r.no_flag = FALSE ;
429 GO_STATE(RM5_RING_OP_DUP) ;
433 else if (cmd == RM_TIMEOUT_T_STUCK &&
434 smc->r.rm_join && smc->r.bn_flag) {
435 GO_STATE(RM6_DIRECTED) ;
439 case ACTIONS(RM5_RING_OP_DUP) :
440 stop_rmt_timer0(smc) ;
441 stop_rmt_timer1(smc) ;
442 stop_rmt_timer2(smc) ;
443 DB_RMTN(1, "RMT : RM5_RING_OP_DUP");
446 case RM5_RING_OP_DUP :
448 if (smc->r.dup_addr_test == DA_PASSED) {
449 smc->r.da_flag = FALSE ;
450 GO_STATE(RM2_RING_OP) ;
454 else if (cmd == RM_RING_NON_OP) {
455 smc->r.jm_flag = FALSE ;
456 smc->r.bn_flag = FALSE ;
457 GO_STATE(RM4_NON_OP_DUP) ;
461 case ACTIONS(RM6_DIRECTED) :
462 start_rmt_timer0(smc,smc->s.rmt_t_direct,RM_TIMEOUT_T_DIRECT) ;
463 stop_rmt_timer1(smc) ;
464 start_rmt_timer2(smc,smc->s.rmt_t_poll,RM_TIMEOUT_POLL) ;
465 sm_ma_control(smc,MA_DIRECTED) ;
466 RS_SET(smc,RS_BEACON) ;
467 DB_RMTN(1, "RMT : RM6_DIRECTED");
472 if (cmd == RM_TIMEOUT_POLL) {
473 start_rmt_timer2(smc,smc->s.rmt_t_poll,RM_TIMEOUT_POLL);
474 sm_mac_check_beacon_claim(smc) ;
476 /* Because of problems with the Supernet II chip set
477 * sending of Directed Beacon will stop after 165ms
478 * therefore restart_trt_for_dbcn(smc) will be called
481 restart_trt_for_dbcn(smc) ;
482 #endif /*SUPERNET_3*/
485 if ((cmd == RM_MY_BEACON || cmd == RM_OTHER_BEACON) &&
487 smc->r.bn_flag = FALSE ;
488 GO_STATE(RM3_DETECT) ;
492 else if ((cmd == RM_MY_BEACON || cmd == RM_OTHER_BEACON) &&
494 smc->r.bn_flag = FALSE ;
495 GO_STATE(RM4_NON_OP_DUP) ;
499 else if (cmd == RM_TIMEOUT_T_DIRECT) {
500 GO_STATE(RM7_TRACE) ;
504 case ACTIONS(RM7_TRACE) :
505 stop_rmt_timer0(smc) ;
506 stop_rmt_timer1(smc) ;
507 stop_rmt_timer2(smc) ;
508 smc->e.trace_prop |= ENTITY_BIT(ENTITY_MAC) ;
509 queue_event(smc,EVENT_ECM,EC_TRACE_PROP) ;
510 DB_RMTN(1, "RMT : RM7_TRACE");
516 SMT_PANIC(smc,SMT_E0122, SMT_E0122_MSG) ;
522 * (jd) RMT duplicate address actions
523 * leave the ring or reinsert just as configured
525 static void rmt_dup_actions(struct s_smc *smc)
527 if (smc->r.jm_flag) {
530 if (smc->s.rmt_dup_mac_behavior) {
531 SMT_ERR_LOG(smc,SMT_E0138, SMT_E0138_MSG) ;
532 rmt_reinsert_actions(smc) ;
535 SMT_ERR_LOG(smc,SMT_E0135, SMT_E0135_MSG) ;
536 rmt_leave_actions(smc) ;
542 * Reconnect to the Ring
544 static void rmt_reinsert_actions(struct s_smc *smc)
546 queue_event(smc,EVENT_ECM,EC_DISCONNECT) ;
547 queue_event(smc,EVENT_ECM,EC_CONNECT) ;
551 * duplicate address detected
553 static void rmt_new_dup_actions(struct s_smc *smc)
555 smc->r.da_flag = TRUE ;
556 smc->r.bn_flag = FALSE ;
557 smc->r.jm_flag = FALSE ;
559 * we have three options : change address, jam or leave
560 * we leave the ring as default
561 * Optionally it's possible to reinsert after leaving the Ring
562 * but this will not conform with SMT Spec.
564 if (smc->s.rmt_dup_mac_behavior) {
565 SMT_ERR_LOG(smc,SMT_E0138, SMT_E0138_MSG) ;
566 rmt_reinsert_actions(smc) ;
569 SMT_ERR_LOG(smc,SMT_E0135, SMT_E0135_MSG) ;
570 rmt_leave_actions(smc) ;
578 static void rmt_leave_actions(struct s_smc *smc)
580 queue_event(smc,EVENT_ECM,EC_DISCONNECT) ;
582 * Note: Do NOT try again later. (with please reconnect)
583 * The station must be left from the ring!
588 * SMT timer interface
591 static void start_rmt_timer0(struct s_smc *smc, u_long value, int event)
593 smc->r.timer0_exp = FALSE ; /* clear timer event flag */
594 smt_timer_start(smc,&smc->r.rmt_timer0,value,EV_TOKEN(EVENT_RMT,event));
598 * SMT timer interface
601 static void start_rmt_timer1(struct s_smc *smc, u_long value, int event)
603 smc->r.timer1_exp = FALSE ; /* clear timer event flag */
604 smt_timer_start(smc,&smc->r.rmt_timer1,value,EV_TOKEN(EVENT_RMT,event));
608 * SMT timer interface
611 static void start_rmt_timer2(struct s_smc *smc, u_long value, int event)
613 smc->r.timer2_exp = FALSE ; /* clear timer event flag */
614 smt_timer_start(smc,&smc->r.rmt_timer2,value,EV_TOKEN(EVENT_RMT,event));
618 * SMT timer interface
621 static void stop_rmt_timer0(struct s_smc *smc)
623 if (smc->r.rmt_timer0.tm_active)
624 smt_timer_stop(smc,&smc->r.rmt_timer0) ;
628 * SMT timer interface
631 static void stop_rmt_timer1(struct s_smc *smc)
633 if (smc->r.rmt_timer1.tm_active)
634 smt_timer_stop(smc,&smc->r.rmt_timer1) ;
638 * SMT timer interface
641 static void stop_rmt_timer2(struct s_smc *smc)
643 if (smc->r.rmt_timer2.tm_active)
644 smt_timer_stop(smc,&smc->r.rmt_timer2) ;