ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / skfp / srf.c
1 /******************************************************************************
2  *
3  *      (C)Copyright 1998,1999 SysKonnect,
4  *      a business unit of Schneider & Koch & Co. Datensysteme GmbH.
5  *
6  *      See the file "skfddi.c" for further information.
7  *
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.
12  *
13  *      The information in this file is provided "AS IS" without warranty.
14  *
15  ******************************************************************************/
16
17 /*
18         SMT 7.2 Status Response Frame Implementation
19         SRF state machine and frame generation
20 */
21
22 #include "h/types.h"
23 #include "h/fddi.h"
24 #include "h/smc.h"
25 #include "h/smt_p.h"
26
27 #define KERNEL
28 #include "h/smtstate.h"
29
30 #ifndef SLIM_SMT
31 #ifndef BOOT
32
33 #ifndef lint
34 static const char ID_sccs[] = "@(#)srf.c        1.18 97/08/04 (C) SK " ;
35 #endif
36
37
38 /*
39  * function declarations
40  */
41 static void clear_all_rep() ;
42 static void clear_reported() ;
43 static void smt_send_srf() ;
44 static struct s_srf_evc *smt_get_evc() ;
45
46 #define MAX_EVCS        (sizeof(smc->evcs)/sizeof(smc->evcs[0]))
47
48 struct evc_init {
49         u_char code ;
50         u_char index ;
51         u_char n ;
52         u_short para ;
53 }  ;
54
55 static const struct evc_init evc_inits[] = {
56         { SMT_COND_SMT_PEER_WRAP,               0,1,SMT_P1048   } ,
57
58         { SMT_COND_MAC_DUP_ADDR,                INDEX_MAC, NUMMACS,SMT_P208C } ,
59         { SMT_COND_MAC_FRAME_ERROR,             INDEX_MAC, NUMMACS,SMT_P208D } ,
60         { SMT_COND_MAC_NOT_COPIED,              INDEX_MAC, NUMMACS,SMT_P208E } ,
61         { SMT_EVENT_MAC_NEIGHBOR_CHANGE,        INDEX_MAC, NUMMACS,SMT_P208F } ,
62         { SMT_EVENT_MAC_PATH_CHANGE,            INDEX_MAC, NUMMACS,SMT_P2090 } ,
63
64         { SMT_COND_PORT_LER,                    INDEX_PORT,NUMPHYS,SMT_P4050 } ,
65         { SMT_COND_PORT_EB_ERROR,               INDEX_PORT,NUMPHYS,SMT_P4052 } ,
66         { SMT_EVENT_PORT_CONNECTION,            INDEX_PORT,NUMPHYS,SMT_P4051 } ,
67         { SMT_EVENT_PORT_PATH_CHANGE,           INDEX_PORT,NUMPHYS,SMT_P4053 } ,
68 } ;
69
70 #define MAX_INIT_EVC    (sizeof(evc_inits)/sizeof(evc_inits[0]))
71
72 void smt_init_evc(smc)
73 struct s_smc *smc ;
74 {
75         struct s_srf_evc        *evc ;
76         const struct evc_init   *init ;
77         int                     i ;
78         int                     index ;
79         int                     offset ;
80
81         static u_char           fail_safe = FALSE ;
82
83         memset((char *)smc->evcs,0,sizeof(smc->evcs)) ;
84
85         evc = smc->evcs ;
86         init = evc_inits ;
87
88         for (i = 0 ; (unsigned) i < MAX_INIT_EVC ; i++) {
89                 for (index = 0 ; index < init->n ; index++) {
90                         evc->evc_code = init->code ;
91                         evc->evc_para = init->para ;
92                         evc->evc_index = init->index + index ;
93 #ifndef DEBUG
94                         evc->evc_multiple = &fail_safe ;
95                         evc->evc_cond_state = &fail_safe ;
96 #endif
97                         evc++ ;
98                 }
99                 init++ ;
100         }
101
102         if ((unsigned) (evc - smc->evcs) > MAX_EVCS) {
103                 SMT_PANIC(smc,SMT_E0127, SMT_E0127_MSG) ;
104         }
105
106         /*
107          * conditions
108          */
109         smc->evcs[0].evc_cond_state = &smc->mib.fddiSMTPeerWrapFlag ;
110         smc->evcs[1].evc_cond_state =
111                 &smc->mib.m[MAC0].fddiMACDuplicateAddressCond ;
112         smc->evcs[2].evc_cond_state =
113                 &smc->mib.m[MAC0].fddiMACFrameErrorFlag ;
114         smc->evcs[3].evc_cond_state =
115                 &smc->mib.m[MAC0].fddiMACNotCopiedFlag ;
116
117         /*
118          * events
119          */
120         smc->evcs[4].evc_multiple = &smc->mib.m[MAC0].fddiMACMultiple_N ;
121         smc->evcs[5].evc_multiple = &smc->mib.m[MAC0].fddiMACMultiple_P ;
122
123         offset = 6 ;
124         for (i = 0 ; i < NUMPHYS ; i++) {
125                 /*
126                  * conditions
127                  */
128                 smc->evcs[offset + 0*NUMPHYS].evc_cond_state =
129                         &smc->mib.p[i].fddiPORTLerFlag ;
130                 smc->evcs[offset + 1*NUMPHYS].evc_cond_state =
131                         &smc->mib.p[i].fddiPORTEB_Condition ;
132
133                 /*
134                  * events
135                  */
136                 smc->evcs[offset + 2*NUMPHYS].evc_multiple =
137                         &smc->mib.p[i].fddiPORTMultiple_U ;
138                 smc->evcs[offset + 3*NUMPHYS].evc_multiple =
139                         &smc->mib.p[i].fddiPORTMultiple_P ;
140                 offset++ ;
141         }
142 #ifdef  DEBUG
143         for (i = 0, evc = smc->evcs ; (unsigned) i < MAX_EVCS ; i++, evc++) {
144                 if (SMT_IS_CONDITION(evc->evc_code)) {
145                         if (!evc->evc_cond_state) {
146                                 SMT_PANIC(smc,SMT_E0128, SMT_E0128_MSG) ;
147                         }
148                         evc->evc_multiple = &fail_safe ;
149                 }
150                 else {
151                         if (!evc->evc_multiple) {
152                                 SMT_PANIC(smc,SMT_E0129, SMT_E0129_MSG) ;
153                         }
154                         evc->evc_cond_state = &fail_safe ;
155                 }
156         }
157 #endif
158         smc->srf.TSR = smt_get_time() ;
159         smc->srf.sr_state = SR0_WAIT ;
160 }
161
162 static struct s_srf_evc *smt_get_evc(smc,code,index)
163 struct s_smc *smc ;
164 int code ;
165 int index ;
166 {
167         int                     i ;
168         struct s_srf_evc        *evc ;
169
170         for (i = 0, evc = smc->evcs ; (unsigned) i < MAX_EVCS ; i++, evc++) {
171                 if (evc->evc_code == code && evc->evc_index == index)
172                         return(evc) ;
173         }
174         return(0) ;
175 }
176
177 #define THRESHOLD_2     (2*TICKS_PER_SECOND)
178 #define THRESHOLD_32    (32*TICKS_PER_SECOND)
179
180 #ifdef  DEBUG
181 static const char * const srf_names[] = {
182         "None","MACPathChangeEvent",    "MACNeighborChangeEvent",
183         "PORTPathChangeEvent",          "PORTUndesiredConnectionAttemptEvent",
184         "SMTPeerWrapCondition",         "SMTHoldCondition",
185         "MACFrameErrorCondition",       "MACDuplicateAddressCondition",
186         "MACNotCopiedCondition",        "PORTEBErrorCondition",
187         "PORTLerCondition"
188 } ;
189 #endif
190
191 void smt_srf_event(smc,code,index,cond)
192 struct s_smc *smc ;
193 int code ;
194 int index ;
195 int cond ;
196 {
197         struct s_srf_evc        *evc ;
198         int                     cond_asserted = 0 ;
199         int                     cond_deasserted = 0 ;
200         int                     event_occurred = 0 ;
201         int                     tsr ;
202         int                     T_Limit = 2*TICKS_PER_SECOND ;
203
204         if (code == SMT_COND_MAC_DUP_ADDR && cond) {
205                 RS_SET(smc,RS_DUPADDR) ;
206         }
207
208         if (code) {
209                 DB_SMT("SRF: %s index %d\n",srf_names[code],index) ;
210
211                 if (!(evc = smt_get_evc(smc,code,index))) {
212                         DB_SMT("SRF : smt_get_evc() failed\n",0,0) ;
213                         return ;
214                 }
215                 /*
216                  * ignore condition if no change
217                  */
218                 if (SMT_IS_CONDITION(code)) {
219                         if (*evc->evc_cond_state == cond)
220                                 return ;
221                 }
222
223                 /*
224                  * set transition time stamp
225                  */
226                 smt_set_timestamp(smc,smc->mib.fddiSMTTransitionTimeStamp) ;
227                 if (SMT_IS_CONDITION(code)) {
228                         DB_SMT("SRF: condition is %s\n",cond ? "ON":"OFF",0) ;
229                         if (cond) {
230                                 *evc->evc_cond_state = TRUE ;
231                                 evc->evc_rep_required = TRUE ;
232                                 smc->srf.any_report = TRUE ;
233                                 cond_asserted = TRUE ;
234                         }
235                         else {
236                                 *evc->evc_cond_state = FALSE ;
237                                 cond_deasserted = TRUE ;
238                         }
239                 }
240                 else {
241                         if (evc->evc_rep_required) {
242                                 *evc->evc_multiple  = TRUE ;
243                         }
244                         else {
245                                 evc->evc_rep_required = TRUE ;
246                                 *evc->evc_multiple  = FALSE ;
247                         }
248                         smc->srf.any_report = TRUE ;
249                         event_occurred = TRUE ;
250                 }
251 #ifdef  FDDI_MIB
252                 snmp_srf_event(smc,evc) ;
253 #endif  /* FDDI_MIB */
254         }
255         tsr = smt_get_time() - smc->srf.TSR ;
256
257         switch (smc->srf.sr_state) {
258         case SR0_WAIT :
259                 /* SR01a */
260                 if (cond_asserted && tsr < T_Limit) {
261                         smc->srf.SRThreshold = THRESHOLD_2 ;
262                         smc->srf.sr_state = SR1_HOLDOFF ;
263                         break ;
264                 }
265                 /* SR01b */
266                 if (cond_deasserted && tsr < T_Limit) {
267                         smc->srf.sr_state = SR1_HOLDOFF ;
268                         break ;
269                 }
270                 /* SR01c */
271                 if (event_occurred && tsr < T_Limit) {
272                         smc->srf.sr_state = SR1_HOLDOFF ;
273                         break ;
274                 }
275                 /* SR00b */
276                 if (cond_asserted && tsr >= T_Limit) {
277                         smc->srf.SRThreshold = THRESHOLD_2 ;
278                         smc->srf.TSR = smt_get_time() ;
279                         smt_send_srf(smc) ;
280                         break ;
281                 }
282                 /* SR00c */
283                 if (cond_deasserted && tsr >= T_Limit) {
284                         smc->srf.TSR = smt_get_time() ;
285                         smt_send_srf(smc) ;
286                         break ;
287                 }
288                 /* SR00d */
289                 if (event_occurred && tsr >= T_Limit) {
290                         smc->srf.TSR = smt_get_time() ;
291                         smt_send_srf(smc) ;
292                         break ;
293                 }
294                 /* SR00e */
295                 if (smc->srf.any_report && (u_long) tsr >=
296                         smc->srf.SRThreshold) {
297                         smc->srf.SRThreshold *= 2 ;
298                         if (smc->srf.SRThreshold > THRESHOLD_32)
299                                 smc->srf.SRThreshold = THRESHOLD_32 ;
300                         smc->srf.TSR = smt_get_time() ;
301                         smt_send_srf(smc) ;
302                         break ;
303                 }
304                 /* SR02 */
305                 if (!smc->mib.fddiSMTStatRptPolicy) {
306                         smc->srf.sr_state = SR2_DISABLED ;
307                         break ;
308                 }
309                 break ;
310         case SR1_HOLDOFF :
311                 /* SR10b */
312                 if (tsr >= T_Limit) {
313                         smc->srf.sr_state = SR0_WAIT ;
314                         smc->srf.TSR = smt_get_time() ;
315                         smt_send_srf(smc) ;
316                         break ;
317                 }
318                 /* SR11a */
319                 if (cond_asserted) {
320                         smc->srf.SRThreshold = THRESHOLD_2 ;
321                 }
322                 /* SR11b */
323                 /* SR11c */
324                 /* handled above */
325                 /* SR12 */
326                 if (!smc->mib.fddiSMTStatRptPolicy) {
327                         smc->srf.sr_state = SR2_DISABLED ;
328                         break ;
329                 }
330                 break ;
331         case SR2_DISABLED :
332                 if (smc->mib.fddiSMTStatRptPolicy) {
333                         smc->srf.sr_state = SR0_WAIT ;
334                         smc->srf.TSR = smt_get_time() ;
335                         smc->srf.SRThreshold = THRESHOLD_2 ;
336                         clear_all_rep(smc) ;
337                         break ;
338                 }
339                 break ;
340         }
341 }
342
343 static void clear_all_rep(smc)
344 struct s_smc *smc ;
345 {
346         struct s_srf_evc        *evc ;
347         int                     i ;
348
349         for (i = 0, evc = smc->evcs ; (unsigned) i < MAX_EVCS ; i++, evc++) {
350                 evc->evc_rep_required = FALSE ;
351                 if (SMT_IS_CONDITION(evc->evc_code))
352                         *evc->evc_cond_state = FALSE ;
353         }
354         smc->srf.any_report = FALSE ;
355 }
356
357 static void clear_reported(smc)
358 struct s_smc *smc ;
359 {
360         struct s_srf_evc        *evc ;
361         int                     i ;
362
363         smc->srf.any_report = FALSE ;
364         for (i = 0, evc = smc->evcs ; (unsigned) i < MAX_EVCS ; i++, evc++) {
365                 if (SMT_IS_CONDITION(evc->evc_code)) {
366                         if (*evc->evc_cond_state == FALSE)
367                                 evc->evc_rep_required = FALSE ;
368                         else
369                                 smc->srf.any_report = TRUE ;
370                 }
371                 else {
372                         evc->evc_rep_required = FALSE ;
373                         *evc->evc_multiple = FALSE ;
374                 }
375         }
376 }
377
378 extern SMbuf *smt_build_frame() ;
379
380 /*
381  * build and send SMT SRF frame
382  */
383 static void smt_send_srf(smc)
384 struct s_smc *smc ;
385 {
386
387         struct smt_header       *smt ;
388         struct s_srf_evc        *evc ;
389         SK_LOC_DECL(struct s_pcon,pcon) ;
390         SMbuf                   *mb ;
391         int                     i ;
392
393         static const struct fddi_addr SMT_SRF_DA = {
394                 { 0x80, 0x01, 0x43, 0x00, 0x80, 0x08 }
395         } ;
396
397         /*
398          * build SMT header
399          */
400         if (!smc->r.sm_ma_avail)
401                 return ;
402         if (!(mb = smt_build_frame(smc,SMT_SRF,SMT_ANNOUNCE,0)))
403                 return ;
404
405         RS_SET(smc,RS_SOFTERROR) ;
406
407         smt = smtod(mb, struct smt_header *) ;
408         smt->smt_dest = SMT_SRF_DA ;            /* DA == SRF multicast */
409
410         /*
411          * setup parameter status
412          */
413         pcon.pc_len = SMT_MAX_INFO_LEN ;        /* max para length */
414         pcon.pc_err = 0 ;                       /* no error */
415         pcon.pc_badset = 0 ;                    /* no bad set count */
416         pcon.pc_p = (void *) (smt + 1) ;        /* paras start here */
417
418         smt_add_para(smc,&pcon,(u_short) SMT_P1033,0,0) ;
419         smt_add_para(smc,&pcon,(u_short) SMT_P1034,0,0) ;
420
421         for (i = 0, evc = smc->evcs ; (unsigned) i < MAX_EVCS ; i++, evc++) {
422                 if (evc->evc_rep_required) {
423                         smt_add_para(smc,&pcon,evc->evc_para,
424                                 (int)evc->evc_index,0) ;
425                 }
426         }
427         smt->smt_len = SMT_MAX_INFO_LEN - pcon.pc_len ;
428         mb->sm_len = smt->smt_len + sizeof(struct smt_header) ;
429
430         DB_SMT("SRF: sending SRF at %x, len %d \n",smt,mb->sm_len) ;
431         DB_SMT("SRF: state SR%d Threshold %d\n",
432                 smc->srf.sr_state,smc->srf.SRThreshold/TICKS_PER_SECOND) ;
433 #ifdef  DEBUG
434         dump_smt(smc,smt,"SRF Send") ;
435 #endif
436         smt_send_frame(smc,mb,FC_SMT_INFO,0) ;
437         clear_reported(smc) ;
438 }
439
440 #endif  /* no BOOT */
441 #endif  /* no SLIM_SMT */