VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / net / skfp / smt.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 #include "h/types.h"
18 #include "h/fddi.h"
19 #include "h/smc.h"
20 #include "h/smt_p.h"
21
22 #define KERNEL
23 #include "h/smtstate.h"
24
25 #ifndef lint
26 static const char ID_sccs[] = "@(#)smt.c        2.43 98/11/23 (C) SK " ;
27 #endif
28
29 extern const u_char canonical[256] ;
30
31 /*
32  * FC in SMbuf
33  */
34 #define m_fc(mb)        ((mb)->sm_data[0])
35
36 #define SMT_TID_MAGIC   0x1f0a7b3c
37
38 #ifdef  DEBUG
39 static const char *const smt_type_name[] = {
40         "SMT_00??", "SMT_INFO", "SMT_02??", "SMT_03??",
41         "SMT_04??", "SMT_05??", "SMT_06??", "SMT_07??",
42         "SMT_08??", "SMT_09??", "SMT_0A??", "SMT_0B??",
43         "SMT_0C??", "SMT_0D??", "SMT_0E??", "SMT_NSA"
44 } ;
45
46 static const char *const smt_class_name[] = {
47         "UNKNOWN","NIF","SIF_CONFIG","SIF_OPER","ECF","RAF","RDF",
48         "SRF","PMF_GET","PMF_SET","ESF"
49 } ;
50 #endif
51 #define LAST_CLASS      (SMT_PMF_SET)
52
53 static const struct fddi_addr SMT_Unknown = {
54         { 0,0,0x1f,0,0,0 }
55 } ;
56
57 /*
58  * external variables
59  */
60 extern const struct fddi_addr fddi_broadcast ;
61
62 /*
63  * external functions
64  */
65 int pcm_status_twisted(struct s_smc *smc);
66
67 /*
68  * function prototypes
69  */
70 #ifdef  LITTLE_ENDIAN
71 static int smt_swap_short(u_short s);
72 #endif
73 static int mac_index(struct s_smc *smc, int mac);
74 static int phy_index(struct s_smc *smc, int phy);
75 static int mac_con_resource_index(struct s_smc *smc, int mac);
76 static int phy_con_resource_index(struct s_smc *smc, int phy);
77 static void smt_send_rdf(struct s_smc *smc, SMbuf *rej, int fc, int reason,
78                          int local);
79 static void smt_send_nif(struct s_smc *smc, const struct fddi_addr *dest, 
80                          int fc, u_long tid, int type, int local);
81 static void smt_send_ecf(struct s_smc *smc, struct fddi_addr *dest, int fc,
82                          u_long tid, int type, int len);
83 static void smt_echo_test(struct s_smc *smc, int dna);
84 static void smt_send_sif_config(struct s_smc *smc, struct fddi_addr *dest,
85                                 u_long tid, int local);
86 static void smt_send_sif_operation(struct s_smc *smc, struct fddi_addr *dest,
87                                    u_long tid, int local);
88 #ifdef LITTLE_ENDIAN
89 static void smt_string_swap(void);
90 #endif
91 static void smt_add_frame_len(SMbuf *mb, int len);
92 static void smt_fill_una(struct s_smc *smc, struct smt_p_una *una);
93 static void smt_fill_sde(struct s_smc *smc, struct smt_p_sde *sde);
94 static void smt_fill_state(struct s_smc *smc, struct smt_p_state *state);
95 static void smt_fill_timestamp(struct s_smc *smc, struct smt_p_timestamp *ts);
96 static void smt_fill_policy(struct s_smc *smc, struct smt_p_policy *policy);
97 static void smt_fill_latency(struct s_smc *smc, struct smt_p_latency *latency);
98 static void smt_fill_neighbor(struct s_smc *smc, struct smt_p_neighbor *neighbor);
99 static int smt_fill_path(struct s_smc *smc, struct smt_p_path *path);
100 static void smt_fill_mac_status(struct s_smc *smc, struct smt_p_mac_status *st);
101 static void smt_fill_lem(struct s_smc *smc, struct smt_p_lem *lem, int phy);
102 static void smt_fill_version(struct s_smc *smc, struct smt_p_version *vers);
103 static void smt_fill_fsc(struct s_smc *smc, struct smt_p_fsc *fsc);
104 static void smt_fill_mac_counter(struct s_smc *smc, struct smt_p_mac_counter *mc);
105 static void smt_fill_mac_fnc(struct s_smc *smc, struct smt_p_mac_fnc *fnc);
106 static void smt_fill_manufacturer(struct s_smc *smc, 
107                                   struct smp_p_manufacturer *man);
108 static void smt_fill_user(struct s_smc *smc, struct smp_p_user *user);
109 static void smt_fill_setcount(struct s_smc *smc, struct smt_p_setcount *setcount);
110 static void smt_fill_echo(struct s_smc *smc, struct smt_p_echo *echo, u_long seed,
111                           int len);
112
113 void smt_clear_una_dna(struct s_smc *smc);
114 static void smt_clear_old_una_dna(struct s_smc *smc);
115 #ifdef  CONCENTRATOR
116 static int entity_to_index(void);
117 #endif
118 static void update_dac(struct s_smc *smc, int report);
119 static int div_ratio(u_long upper, u_long lower);
120 #ifdef  USE_CAN_ADDR
121 void    hwm_conv_can(struct s_smc *smc, char *data, int len);
122 #else
123 #define         hwm_conv_can(smc,data,len)
124 #endif
125
126
127 static inline int is_my_addr(const struct s_smc *smc, 
128                              const struct fddi_addr *addr)
129 {
130         return(*(short *)(&addr->a[0]) ==
131                 *(short *)(&smc->mib.m[MAC0].fddiMACSMTAddress.a[0])
132           && *(short *)(&addr->a[2]) ==
133                 *(short *)(&smc->mib.m[MAC0].fddiMACSMTAddress.a[2])
134           && *(short *)(&addr->a[4]) ==
135                 *(short *)(&smc->mib.m[MAC0].fddiMACSMTAddress.a[4])) ;
136 }
137
138 static inline int is_zero(const struct fddi_addr *addr)
139 {
140         return(*(short *)(&addr->a[0]) == 0 &&
141                *(short *)(&addr->a[2]) == 0 &&
142                *(short *)(&addr->a[4]) == 0 ) ;
143 }
144
145 static inline int is_broadcast(const struct fddi_addr *addr)
146 {
147         return(*(u_short *)(&addr->a[0]) == 0xffff &&
148                *(u_short *)(&addr->a[2]) == 0xffff &&
149                *(u_short *)(&addr->a[4]) == 0xffff ) ;
150 }
151
152 static inline int is_individual(const struct fddi_addr *addr)
153 {
154         return(!(addr->a[0] & GROUP_ADDR)) ;
155 }
156
157 static inline int is_equal(const struct fddi_addr *addr1, 
158                            const struct fddi_addr *addr2)
159 {
160         return(*(u_short *)(&addr1->a[0]) == *(u_short *)(&addr2->a[0]) &&
161                *(u_short *)(&addr1->a[2]) == *(u_short *)(&addr2->a[2]) &&
162                *(u_short *)(&addr1->a[4]) == *(u_short *)(&addr2->a[4]) ) ;
163 }
164
165 /*
166  * list of mandatory paras in frames
167  */
168 static const u_short plist_nif[] = { SMT_P_UNA,SMT_P_SDE,SMT_P_STATE,0 } ;
169
170 /*
171  * init SMT agent
172  */
173 void smt_agent_init(struct s_smc *smc)
174 {
175         int             i ;
176
177         /*
178          * get MAC address
179          */
180         smc->mib.m[MAC0].fddiMACSMTAddress = smc->hw.fddi_home_addr ;
181
182         /*
183          * get OUI address from driver (bia == built-in-address)
184          */
185         smc->mib.fddiSMTStationId.sid_oem[0] = 0 ;
186         smc->mib.fddiSMTStationId.sid_oem[1] = 0 ;
187         driver_get_bia(smc,&smc->mib.fddiSMTStationId.sid_node) ;
188         for (i = 0 ; i < 6 ; i ++) {
189                 smc->mib.fddiSMTStationId.sid_node.a[i] =
190                         canonical[smc->mib.fddiSMTStationId.sid_node.a[i]] ;
191         }
192         smc->mib.fddiSMTManufacturerData[0] =
193                 smc->mib.fddiSMTStationId.sid_node.a[0] ;
194         smc->mib.fddiSMTManufacturerData[1] =
195                 smc->mib.fddiSMTStationId.sid_node.a[1] ;
196         smc->mib.fddiSMTManufacturerData[2] =
197                 smc->mib.fddiSMTStationId.sid_node.a[2] ;
198         smc->sm.smt_tid = 0 ;
199         smc->mib.m[MAC0].fddiMACDupAddressTest = DA_NONE ;
200         smc->mib.m[MAC0].fddiMACUNDA_Flag = FALSE ;
201 #ifndef SLIM_SMT
202         smt_clear_una_dna(smc) ;
203         smt_clear_old_una_dna(smc) ;
204 #endif
205         for (i = 0 ; i < SMT_MAX_TEST ; i++)
206                 smc->sm.pend[i] = 0 ;
207         smc->sm.please_reconnect = 0 ;
208         smc->sm.uniq_ticks = 0 ;
209 }
210
211 /*
212  * SMT task
213  * forever
214  *      delay 30 seconds
215  *      send NIF
216  *      check tvu & tvd
217  * end
218  */
219 void smt_agent_task(struct s_smc *smc)
220 {
221         smt_timer_start(smc,&smc->sm.smt_timer, (u_long)1000000L,
222                 EV_TOKEN(EVENT_SMT,SM_TIMER)) ;
223         DB_SMT("SMT agent task\n",0,0) ;
224 }
225
226 void smt_please_reconnect(struct s_smc *smc, int reconn_time)
227 /* struct s_smc *smc;  Pointer to SMT context */
228 /* int reconn_time;    Wait for reconnect time in seconds */
229 {
230         /*
231          * The please reconnect variable is used as a timer.
232          * It is decremented each time smt_event is called.
233          * This happens every second or when smt_force_irq is called.
234          * Note: smt_force_irq () is called on some packet receives and
235          *       when a multicast address is changed. Since nothing
236          *       is received during the disconnect and the multicast
237          *       address changes can be viewed as not very often and
238          *       the timer runs out close to its given value
239          *       (reconn_time).
240          */
241         smc->sm.please_reconnect = reconn_time ;
242 }
243
244 #ifndef SMT_REAL_TOKEN_CT
245 void smt_emulate_token_ct(struct s_smc *smc, int mac_index)
246 {
247         u_long  count;
248         u_long  time;
249
250
251         time = smt_get_time();
252         count = ((time - smc->sm.last_tok_time[mac_index]) *
253                                         100)/TICKS_PER_SECOND;
254
255         /*
256          * Only when ring is up we will have a token count. The
257          * flag is unfortunatly a single instance value. This
258          * doesn't matter now, because we currently have only
259          * one MAC instance.
260          */
261         if (smc->hw.mac_ring_is_up){
262                 smc->mib.m[mac_index].fddiMACToken_Ct += count;
263         }
264
265         /* Remember current time */
266         smc->sm.last_tok_time[mac_index] = time;
267
268 }
269 #endif
270
271 /*ARGSUSED1*/
272 void smt_event(struct s_smc *smc, int event)
273 {
274         u_long          time ;
275 #ifndef SMT_REAL_TOKEN_CT
276         int             i ;
277 #endif
278
279
280         if (smc->sm.please_reconnect) {
281                 smc->sm.please_reconnect -- ;
282                 if (smc->sm.please_reconnect == 0) {
283                         /* Counted down */
284                         queue_event(smc,EVENT_ECM,EC_CONNECT) ;
285                 }
286         }
287
288         if (event == SM_FAST)
289                 return ;
290
291         /*
292          * timer for periodic cleanup in driver
293          * reset and start the watchdog (FM2)
294          * ESS timer
295          * SBA timer
296          */
297         smt_timer_poll(smc) ;
298         smt_start_watchdog(smc) ;
299 #ifndef SLIM_SMT
300 #ifndef BOOT
301 #ifdef  ESS
302         ess_timer_poll(smc) ;
303 #endif
304 #endif
305 #ifdef  SBA
306         sba_timer_poll(smc) ;
307 #endif
308
309         smt_srf_event(smc,0,0,0) ;
310
311 #endif  /* no SLIM_SMT */
312
313         time = smt_get_time() ;
314
315         if (time - smc->sm.smt_last_lem >= TICKS_PER_SECOND*8) {
316                 /*
317                  * Use 8 sec. for the time intervall, it simplifies the
318                  * LER estimation.
319                  */
320                 struct fddi_mib_m       *mib ;
321                 u_long                  upper ;
322                 u_long                  lower ;
323                 int                     cond ;
324                 int                     port;
325                 struct s_phy            *phy ;
326                 /*
327                  * calculate LEM bit error rate
328                  */
329                 sm_lem_evaluate(smc) ;
330                 smc->sm.smt_last_lem = time ;
331
332                 /*
333                  * check conditions
334                  */
335 #ifndef SLIM_SMT
336                 mac_update_counter(smc) ;
337                 mib = smc->mib.m ;
338                 upper =
339                 (mib->fddiMACLost_Ct - mib->fddiMACOld_Lost_Ct) +
340                 (mib->fddiMACError_Ct - mib->fddiMACOld_Error_Ct) ;
341                 lower =
342                 (mib->fddiMACFrame_Ct - mib->fddiMACOld_Frame_Ct) +
343                 (mib->fddiMACLost_Ct - mib->fddiMACOld_Lost_Ct) ;
344                 mib->fddiMACFrameErrorRatio = div_ratio(upper,lower) ;
345
346                 cond =
347                         ((!mib->fddiMACFrameErrorThreshold &&
348                         mib->fddiMACError_Ct != mib->fddiMACOld_Error_Ct) ||
349                         (mib->fddiMACFrameErrorRatio >
350                         mib->fddiMACFrameErrorThreshold)) ;
351
352                 if (cond != mib->fddiMACFrameErrorFlag)
353                         smt_srf_event(smc,SMT_COND_MAC_FRAME_ERROR,
354                                 INDEX_MAC,cond) ;
355
356                 upper =
357                 (mib->fddiMACNotCopied_Ct - mib->fddiMACOld_NotCopied_Ct) ;
358                 lower =
359                 upper +
360                 (mib->fddiMACCopied_Ct - mib->fddiMACOld_Copied_Ct) ;
361                 mib->fddiMACNotCopiedRatio = div_ratio(upper,lower) ;
362
363                 cond =
364                         ((!mib->fddiMACNotCopiedThreshold &&
365                         mib->fddiMACNotCopied_Ct !=
366                                 mib->fddiMACOld_NotCopied_Ct)||
367                         (mib->fddiMACNotCopiedRatio >
368                         mib->fddiMACNotCopiedThreshold)) ;
369
370                 if (cond != mib->fddiMACNotCopiedFlag)
371                         smt_srf_event(smc,SMT_COND_MAC_NOT_COPIED,
372                                 INDEX_MAC,cond) ;
373
374                 /*
375                  * set old values
376                  */
377                 mib->fddiMACOld_Frame_Ct = mib->fddiMACFrame_Ct ;
378                 mib->fddiMACOld_Copied_Ct = mib->fddiMACCopied_Ct ;
379                 mib->fddiMACOld_Error_Ct = mib->fddiMACError_Ct ;
380                 mib->fddiMACOld_Lost_Ct = mib->fddiMACLost_Ct ;
381                 mib->fddiMACOld_NotCopied_Ct = mib->fddiMACNotCopied_Ct ;
382
383                 /*
384                  * Check port EBError Condition
385                  */
386                 for (port = 0; port < NUMPHYS; port ++) {
387                         phy = &smc->y[port] ;
388
389                         if (!phy->mib->fddiPORTHardwarePresent) {
390                                 continue;
391                         }
392
393                         cond = (phy->mib->fddiPORTEBError_Ct -
394                                 phy->mib->fddiPORTOldEBError_Ct > 5) ;
395
396                         /* If ratio is more than 5 in 8 seconds
397                          * Set the condition.
398                          */
399                         smt_srf_event(smc,SMT_COND_PORT_EB_ERROR,
400                                 (int) (INDEX_PORT+ phy->np) ,cond) ;
401
402                         /*
403                          * set old values
404                          */
405                         phy->mib->fddiPORTOldEBError_Ct =
406                                 phy->mib->fddiPORTEBError_Ct ;
407                 }
408
409 #endif  /* no SLIM_SMT */
410         }
411
412 #ifndef SLIM_SMT
413
414         if (time - smc->sm.smt_last_notify >= (u_long)
415                 (smc->mib.fddiSMTTT_Notify * TICKS_PER_SECOND) ) {
416                 /*
417                  * we can either send an announcement or a request
418                  * a request will trigger a reply so that we can update
419                  * our dna
420                  * note: same tid must be used until reply is received
421                  */
422                 if (!smc->sm.pend[SMT_TID_NIF])
423                         smc->sm.pend[SMT_TID_NIF] = smt_get_tid(smc) ;
424                 smt_send_nif(smc,&fddi_broadcast, FC_SMT_NSA,
425                         smc->sm.pend[SMT_TID_NIF], SMT_REQUEST,0) ;
426                 smc->sm.smt_last_notify = time ;
427         }
428
429         /*
430          * check timer
431          */
432         if (smc->sm.smt_tvu &&
433             time - smc->sm.smt_tvu > 228*TICKS_PER_SECOND) {
434                 DB_SMT("SMT : UNA expired\n",0,0) ;
435                 smc->sm.smt_tvu = 0 ;
436
437                 if (!is_equal(&smc->mib.m[MAC0].fddiMACUpstreamNbr,
438                         &SMT_Unknown)){
439                         /* Do not update unknown address */
440                         smc->mib.m[MAC0].fddiMACOldUpstreamNbr=
441                                 smc->mib.m[MAC0].fddiMACUpstreamNbr ;
442                 }
443                 smc->mib.m[MAC0].fddiMACUpstreamNbr = SMT_Unknown ;
444                 smc->mib.m[MAC0].fddiMACUNDA_Flag = FALSE ;
445                 /*
446                  * Make sure the fddiMACUNDA_Flag = FALSE is
447                  * included in the SRF so we don't generate
448                  * a separate SRF for the deassertion of this
449                  * condition
450                  */
451                 update_dac(smc,0) ;
452                 smt_srf_event(smc, SMT_EVENT_MAC_NEIGHBOR_CHANGE,
453                         INDEX_MAC,0) ;
454         }
455         if (smc->sm.smt_tvd &&
456             time - smc->sm.smt_tvd > 228*TICKS_PER_SECOND) {
457                 DB_SMT("SMT : DNA expired\n",0,0) ;
458                 smc->sm.smt_tvd = 0 ;
459                 if (!is_equal(&smc->mib.m[MAC0].fddiMACDownstreamNbr,
460                         &SMT_Unknown)){
461                         /* Do not update unknown address */
462                         smc->mib.m[MAC0].fddiMACOldDownstreamNbr=
463                                 smc->mib.m[MAC0].fddiMACDownstreamNbr ;
464                 }
465                 smc->mib.m[MAC0].fddiMACDownstreamNbr = SMT_Unknown ;
466                 smt_srf_event(smc, SMT_EVENT_MAC_NEIGHBOR_CHANGE,
467                         INDEX_MAC,0) ;
468         }
469
470 #endif  /* no SLIM_SMT */
471
472 #ifndef SMT_REAL_TOKEN_CT
473         /*
474          * Token counter emulation section. If hardware supports the token
475          * count, the token counter will be updated in mac_update_counter.
476          */
477         for (i = MAC0; i < NUMMACS; i++ ){
478                 if (time - smc->sm.last_tok_time[i] > 2*TICKS_PER_SECOND ){
479                         smt_emulate_token_ct( smc, i );
480                 }
481         }
482 #endif
483
484         smt_timer_start(smc,&smc->sm.smt_timer, (u_long)1000000L,
485                 EV_TOKEN(EVENT_SMT,SM_TIMER)) ;
486 }
487
488 static int div_ratio(u_long upper, u_long lower)
489 {
490         if ((upper<<16L) < upper)
491                 upper = 0xffff0000L ;
492         else
493                 upper <<= 16L ;
494         if (!lower)
495                 return(0) ;
496         return((int)(upper/lower)) ;
497 }
498
499 #ifndef SLIM_SMT
500
501 /*
502  * receive packet handler
503  */
504 void smt_received_pack(struct s_smc *smc, SMbuf *mb, int fs)
505 /* int fs;  frame status */
506 {
507         struct smt_header       *sm ;
508         int                     local ;
509
510         int                     illegal = 0 ;
511
512         switch (m_fc(mb)) {
513         case FC_SMT_INFO :
514         case FC_SMT_LAN_LOC :
515         case FC_SMT_LOC :
516         case FC_SMT_NSA :
517                 break ;
518         default :
519                 smt_free_mbuf(smc,mb) ;
520                 return ;
521         }
522
523         smc->mib.m[MAC0].fddiMACSMTCopied_Ct++ ;
524         sm = smtod(mb,struct smt_header *) ;
525         local = ((fs & L_INDICATOR) != 0) ;
526         hwm_conv_can(smc,(char *)sm,12) ;
527
528         /* check destination address */
529         if (is_individual(&sm->smt_dest) && !is_my_addr(smc,&sm->smt_dest)) {
530                 smt_free_mbuf(smc,mb) ;
531                 return ;
532         }
533 #if     0               /* for DUP recognition, do NOT filter them */
534         /* ignore loop back packets */
535         if (is_my_addr(smc,&sm->smt_source) && !local) {
536                 smt_free_mbuf(smc,mb) ;
537                 return ;
538         }
539 #endif
540
541         smt_swap_para(sm,(int) mb->sm_len,1) ;
542         DB_SMT("SMT : received packet [%s] at 0x%x\n",
543                 smt_type_name[m_fc(mb) & 0xf],sm) ;
544         DB_SMT("SMT : version %d, class %s\n",sm->smt_version,
545                 smt_class_name[(sm->smt_class>LAST_CLASS)?0 : sm->smt_class]) ;
546
547 #ifdef  SBA
548         /*
549          * check if NSA frame
550          */
551         if (m_fc(mb) == FC_SMT_NSA && sm->smt_class == SMT_NIF &&
552                 (sm->smt_type == SMT_ANNOUNCE || sm->smt_type == SMT_REQUEST)) {
553                         smc->sba.sm = sm ;
554                         sba(smc,NIF) ;
555         }
556 #endif
557
558         /*
559          * ignore any packet with NSA and A-indicator set
560          */
561         if ( (fs & A_INDICATOR) && m_fc(mb) == FC_SMT_NSA) {
562                 DB_SMT("SMT : ignoring NSA with A-indicator set from %s\n",
563                         addr_to_string(&sm->smt_source),0) ;
564                 smt_free_mbuf(smc,mb) ;
565                 return ;
566         }
567
568         /*
569          * ignore frames with illegal length
570          */
571         if (((sm->smt_class == SMT_ECF) && (sm->smt_len > SMT_MAX_ECHO_LEN)) ||
572             ((sm->smt_class != SMT_ECF) && (sm->smt_len > SMT_MAX_INFO_LEN))) {
573                 smt_free_mbuf(smc,mb) ;
574                 return ;
575         }
576
577         /*
578          * check SMT version
579          */
580         switch (sm->smt_class) {
581         case SMT_NIF :
582         case SMT_SIF_CONFIG :
583         case SMT_SIF_OPER :
584         case SMT_ECF :
585                 if (sm->smt_version != SMT_VID)
586                         illegal = 1;
587                 break ;
588         default :
589                 if (sm->smt_version != SMT_VID_2)
590                         illegal = 1;
591                 break ;
592         }
593         if (illegal) {
594                 DB_SMT("SMT : version = %d, dest = %s\n",
595                         sm->smt_version,addr_to_string(&sm->smt_source)) ;
596                 smt_send_rdf(smc,mb,m_fc(mb),SMT_RDF_VERSION,local) ;
597                 smt_free_mbuf(smc,mb) ;
598                 return ;
599         }
600         if ((sm->smt_len > mb->sm_len - sizeof(struct smt_header)) ||
601             ((sm->smt_len & 3) && (sm->smt_class != SMT_ECF))) {
602                 DB_SMT("SMT: info length error, len = %d\n",sm->smt_len,0) ;
603                 smt_send_rdf(smc,mb,m_fc(mb),SMT_RDF_LENGTH,local) ;
604                 smt_free_mbuf(smc,mb) ;
605                 return ;
606         }
607         switch (sm->smt_class) {
608         case SMT_NIF :
609                 if (smt_check_para(smc,sm,plist_nif)) {
610                         DB_SMT("SMT: NIF with para problem, ignoring\n",0,0) ;
611                         break ;
612                 } ;
613                 switch (sm->smt_type) {
614                 case SMT_ANNOUNCE :
615                 case SMT_REQUEST :
616                         if (!(fs & C_INDICATOR) && m_fc(mb) == FC_SMT_NSA
617                                 && is_broadcast(&sm->smt_dest)) {
618                                 struct smt_p_state      *st ;
619
620                                 /* set my UNA */
621                                 if (!is_equal(
622                                         &smc->mib.m[MAC0].fddiMACUpstreamNbr,
623                                         &sm->smt_source)) {
624                                         DB_SMT("SMT : updated my UNA = %s\n",
625                                         addr_to_string(&sm->smt_source),0) ;
626                                         if (!is_equal(&smc->mib.m[MAC0].
627                                             fddiMACUpstreamNbr,&SMT_Unknown)){
628                                          /* Do not update unknown address */
629                                          smc->mib.m[MAC0].fddiMACOldUpstreamNbr=
630                                          smc->mib.m[MAC0].fddiMACUpstreamNbr ;
631                                         }
632
633                                         smc->mib.m[MAC0].fddiMACUpstreamNbr =
634                                                 sm->smt_source ;
635                                         smt_srf_event(smc,
636                                                 SMT_EVENT_MAC_NEIGHBOR_CHANGE,
637                                                 INDEX_MAC,0) ;
638                                         smt_echo_test(smc,0) ;
639                                 }
640                                 smc->sm.smt_tvu = smt_get_time() ;
641                                 st = (struct smt_p_state *)
642                                         sm_to_para(smc,sm,SMT_P_STATE) ;
643                                 if (st) {
644                                         smc->mib.m[MAC0].fddiMACUNDA_Flag =
645                                         (st->st_dupl_addr & SMT_ST_MY_DUPA) ?
646                                         TRUE : FALSE ;
647                                         update_dac(smc,1) ;
648                                 }
649                         }
650                         if ((sm->smt_type == SMT_REQUEST) &&
651                             is_individual(&sm->smt_source) &&
652                             ((!(fs & A_INDICATOR) && m_fc(mb) == FC_SMT_NSA) ||
653                              (m_fc(mb) != FC_SMT_NSA))) {
654                                 DB_SMT("SMT : replying to NIF request %s\n",
655                                         addr_to_string(&sm->smt_source),0) ;
656                                 smt_send_nif(smc,&sm->smt_source,
657                                         FC_SMT_INFO,
658                                         sm->smt_tid,
659                                         SMT_REPLY,local) ;
660                         }
661                         break ;
662                 case SMT_REPLY :
663                         DB_SMT("SMT : received NIF response from %s\n",
664                                 addr_to_string(&sm->smt_source),0) ;
665                         if (fs & A_INDICATOR) {
666                                 smc->sm.pend[SMT_TID_NIF] = 0 ;
667                                 DB_SMT("SMT : duplicate address\n",0,0) ;
668                                 smc->mib.m[MAC0].fddiMACDupAddressTest =
669                                         DA_FAILED ;
670                                 smc->r.dup_addr_test = DA_FAILED ;
671                                 queue_event(smc,EVENT_RMT,RM_DUP_ADDR) ;
672                                 smc->mib.m[MAC0].fddiMACDA_Flag = TRUE ;
673                                 update_dac(smc,1) ;
674                                 break ;
675                         }
676                         if (sm->smt_tid == smc->sm.pend[SMT_TID_NIF]) {
677                                 smc->sm.pend[SMT_TID_NIF] = 0 ;
678                                 /* set my DNA */
679                                 if (!is_equal(
680                                         &smc->mib.m[MAC0].fddiMACDownstreamNbr,
681                                         &sm->smt_source)) {
682                                         DB_SMT("SMT : updated my DNA\n",0,0) ;
683                                         if (!is_equal(&smc->mib.m[MAC0].
684                                          fddiMACDownstreamNbr, &SMT_Unknown)){
685                                          /* Do not update unknown address */
686                                 smc->mib.m[MAC0].fddiMACOldDownstreamNbr =
687                                          smc->mib.m[MAC0].fddiMACDownstreamNbr ;
688                                         }
689
690                                         smc->mib.m[MAC0].fddiMACDownstreamNbr =
691                                                 sm->smt_source ;
692                                         smt_srf_event(smc,
693                                                 SMT_EVENT_MAC_NEIGHBOR_CHANGE,
694                                                 INDEX_MAC,0) ;
695                                         smt_echo_test(smc,1) ;
696                                 }
697                                 smc->mib.m[MAC0].fddiMACDA_Flag = FALSE ;
698                                 update_dac(smc,1) ;
699                                 smc->sm.smt_tvd = smt_get_time() ;
700                                 smc->mib.m[MAC0].fddiMACDupAddressTest =
701                                         DA_PASSED ;
702                                 if (smc->r.dup_addr_test != DA_PASSED) {
703                                         smc->r.dup_addr_test = DA_PASSED ;
704                                         queue_event(smc,EVENT_RMT,RM_DUP_ADDR) ;
705                                 }
706                         }
707                         else if (sm->smt_tid ==
708                                 smc->sm.pend[SMT_TID_NIF_TEST]) {
709                                 DB_SMT("SMT : NIF test TID ok\n",0,0) ;
710                         }
711                         else {
712                                 DB_SMT("SMT : expected TID %lx, got %lx\n",
713                                 smc->sm.pend[SMT_TID_NIF],sm->smt_tid) ;
714                         }
715                         break ;
716                 default :
717                         illegal = 2 ;
718                         break ;
719                 }
720                 break ;
721         case SMT_SIF_CONFIG :   /* station information */
722                 if (sm->smt_type != SMT_REQUEST)
723                         break ;
724                 DB_SMT("SMT : replying to SIF Config request from %s\n",
725                         addr_to_string(&sm->smt_source),0) ;
726                 smt_send_sif_config(smc,&sm->smt_source,sm->smt_tid,local) ;
727                 break ;
728         case SMT_SIF_OPER :     /* station information */
729                 if (sm->smt_type != SMT_REQUEST)
730                         break ;
731                 DB_SMT("SMT : replying to SIF Operation request from %s\n",
732                         addr_to_string(&sm->smt_source),0) ;
733                 smt_send_sif_operation(smc,&sm->smt_source,sm->smt_tid,local) ;
734                 break ;
735         case SMT_ECF :          /* echo frame */
736                 switch (sm->smt_type) {
737                 case SMT_REPLY :
738                         smc->mib.priv.fddiPRIVECF_Reply_Rx++ ;
739                         DB_SMT("SMT: received ECF reply from %s\n",
740                                 addr_to_string(&sm->smt_source),0) ;
741                         if (sm_to_para(smc,sm,SMT_P_ECHODATA) == 0) {
742                                 DB_SMT("SMT: ECHODATA missing\n",0,0) ;
743                                 break ;
744                         }
745                         if (sm->smt_tid == smc->sm.pend[SMT_TID_ECF]) {
746                                 DB_SMT("SMT : ECF test TID ok\n",0,0) ;
747                         }
748                         else if (sm->smt_tid == smc->sm.pend[SMT_TID_ECF_UNA]) {
749                                 DB_SMT("SMT : ECF test UNA ok\n",0,0) ;
750                         }
751                         else if (sm->smt_tid == smc->sm.pend[SMT_TID_ECF_DNA]) {
752                                 DB_SMT("SMT : ECF test DNA ok\n",0,0) ;
753                         }
754                         else {
755                                 DB_SMT("SMT : expected TID %lx, got %lx\n",
756                                         smc->sm.pend[SMT_TID_ECF],
757                                         sm->smt_tid) ;
758                         }
759                         break ;
760                 case SMT_REQUEST :
761                         smc->mib.priv.fddiPRIVECF_Req_Rx++ ;
762                         {
763                         if (sm->smt_len && !sm_to_para(smc,sm,SMT_P_ECHODATA)) {
764                         DB_SMT("SMT: ECF with para problem,sending RDF\n",0,0) ;
765                                 smt_send_rdf(smc,mb,m_fc(mb),SMT_RDF_LENGTH,
766                                         local) ;
767                                 break ;
768                         }
769                         DB_SMT("SMT - sending ECF reply to %s\n",
770                                 addr_to_string(&sm->smt_source),0) ;
771
772                         /* set destination addr.  & reply */
773                         sm->smt_dest = sm->smt_source ;
774                         sm->smt_type = SMT_REPLY ;
775                         dump_smt(smc,sm,"ECF REPLY") ;
776                         smc->mib.priv.fddiPRIVECF_Reply_Tx++ ;
777                         smt_send_frame(smc,mb,FC_SMT_INFO,local) ;
778                         return ;                /* DON'T free mbuf */
779                         }
780                 default :
781                         illegal = 1 ;
782                         break ;
783                 }
784                 break ;
785 #ifndef BOOT
786         case SMT_RAF :          /* resource allocation */
787 #ifdef  ESS
788                 DB_ESSN(2,"ESS: RAF frame received\n",0,0) ;
789                 fs = ess_raf_received_pack(smc,mb,sm,fs) ;
790 #endif
791
792 #ifdef  SBA
793                 DB_SBAN(2,"SBA: RAF frame received\n",0,0) ;
794                 sba_raf_received_pack(smc,sm,fs) ;
795 #endif
796                 break ;
797         case SMT_RDF :          /* request denied */
798                 smc->mib.priv.fddiPRIVRDF_Rx++ ;
799                 break ;
800         case SMT_ESF :          /* extended service - not supported */
801                 if (sm->smt_type == SMT_REQUEST) {
802                         DB_SMT("SMT - received ESF, sending RDF\n",0,0) ;
803                         smt_send_rdf(smc,mb,m_fc(mb),SMT_RDF_CLASS,local) ;
804                 }
805                 break ;
806         case SMT_PMF_GET :
807         case SMT_PMF_SET :
808                 if (sm->smt_type != SMT_REQUEST)
809                         break ;
810                 /* update statistics */
811                 if (sm->smt_class == SMT_PMF_GET)
812                         smc->mib.priv.fddiPRIVPMF_Get_Rx++ ;
813                 else
814                         smc->mib.priv.fddiPRIVPMF_Set_Rx++ ;
815                 /*
816                  * ignore PMF SET with I/G set
817                  */
818                 if ((sm->smt_class == SMT_PMF_SET) &&
819                         !is_individual(&sm->smt_dest)) {
820                         DB_SMT("SMT: ignoring PMF-SET with I/G set\n",0,0) ;
821                         break ;
822                 }
823                 smt_pmf_received_pack(smc,mb, local) ;
824                 break ;
825         case SMT_SRF :
826                 dump_smt(smc,sm,"SRF received") ;
827                 break ;
828         default :
829                 if (sm->smt_type != SMT_REQUEST)
830                         break ;
831                 /*
832                  * For frames with unknown class:
833                  * we need to send a RDF frame according to 8.1.3.1.1,
834                  * only if it is a REQUEST.
835                  */
836                 DB_SMT("SMT : class = %d, send RDF to %s\n",
837                         sm->smt_class, addr_to_string(&sm->smt_source)) ;
838
839                 smt_send_rdf(smc,mb,m_fc(mb),SMT_RDF_CLASS,local) ;
840                 break ;
841 #endif
842         }
843         if (illegal) {
844                 DB_SMT("SMT: discarding invalid frame, reason = %d\n",
845                         illegal,0) ;
846         }
847         smt_free_mbuf(smc,mb) ;
848 }
849
850 static void update_dac(struct s_smc *smc, int report)
851 {
852         int     cond ;
853
854         cond = ( smc->mib.m[MAC0].fddiMACUNDA_Flag |
855                 smc->mib.m[MAC0].fddiMACDA_Flag) != 0 ;
856         if (report && (cond != smc->mib.m[MAC0].fddiMACDuplicateAddressCond))
857                 smt_srf_event(smc, SMT_COND_MAC_DUP_ADDR,INDEX_MAC,cond) ;
858         else
859                 smc->mib.m[MAC0].fddiMACDuplicateAddressCond = cond ;
860 }
861
862 /*
863  * send SMT frame
864  *      set source address
865  *      set station ID
866  *      send frame
867  */
868 void smt_send_frame(struct s_smc *smc, SMbuf *mb, int fc, int local)
869 /* SMbuf *mb;   buffer to send */
870 /* int fc;      FC value */
871 {
872         struct smt_header       *sm ;
873
874         if (!smc->r.sm_ma_avail && !local) {
875                 smt_free_mbuf(smc,mb) ;
876                 return ;
877         }
878         sm = smtod(mb,struct smt_header *) ;
879         sm->smt_source = smc->mib.m[MAC0].fddiMACSMTAddress ;
880         sm->smt_sid = smc->mib.fddiSMTStationId ;
881
882         smt_swap_para(sm,(int) mb->sm_len,0) ;          /* swap para & header */
883         hwm_conv_can(smc,(char *)sm,12) ;               /* convert SA and DA */
884         smc->mib.m[MAC0].fddiMACSMTTransmit_Ct++ ;
885         smt_send_mbuf(smc,mb,local ? FC_SMT_LOC : fc) ;
886 }
887
888 /*
889  * generate and send RDF
890  */
891 static void smt_send_rdf(struct s_smc *smc, SMbuf *rej, int fc, int reason,
892                          int local)
893 /* SMbuf *rej;  mbuf of offending frame */
894 /* int fc;      FC of denied frame */
895 /* int reason;  reason code */
896 {
897         SMbuf   *mb ;
898         struct smt_header       *sm ;   /* header of offending frame */
899         struct smt_rdf  *rdf ;
900         int             len ;
901         int             frame_len ;
902
903         sm = smtod(rej,struct smt_header *) ;
904         if (sm->smt_type != SMT_REQUEST)
905                 return ;
906
907         DB_SMT("SMT: sending RDF to %s,reason = 0x%x\n",
908                 addr_to_string(&sm->smt_source),reason) ;
909
910
911         /*
912          * note: get framelength from MAC length, NOT from SMT header
913          * smt header length is included in sm_len
914          */
915         frame_len = rej->sm_len ;
916
917         if (!(mb=smt_build_frame(smc,SMT_RDF,SMT_REPLY,sizeof(struct smt_rdf))))
918                 return ;
919         rdf = smtod(mb,struct smt_rdf *) ;
920         rdf->smt.smt_tid = sm->smt_tid ;                /* use TID from sm */
921         rdf->smt.smt_dest = sm->smt_source ;            /* set dest = source */
922
923         /* set P12 */
924         rdf->reason.para.p_type = SMT_P_REASON ;
925         rdf->reason.para.p_len = sizeof(struct smt_p_reason) - PARA_LEN ;
926         rdf->reason.rdf_reason = reason ;
927
928         /* set P14 */
929         rdf->version.para.p_type = SMT_P_VERSION ;
930         rdf->version.para.p_len = sizeof(struct smt_p_version) - PARA_LEN ;
931         rdf->version.v_pad = 0 ;
932         rdf->version.v_n = 1 ;
933         rdf->version.v_index = 1 ;
934         rdf->version.v_version[0] = SMT_VID_2 ;
935         rdf->version.v_pad2 = 0 ;
936
937         /* set P13 */
938         if ((unsigned) frame_len <= SMT_MAX_INFO_LEN - sizeof(*rdf) +
939                 2*sizeof(struct smt_header))
940                 len = frame_len ;
941         else
942                 len = SMT_MAX_INFO_LEN - sizeof(*rdf) +
943                         2*sizeof(struct smt_header) ;
944         /* make length multiple of 4 */
945         len &= ~3 ;
946         rdf->refused.para.p_type = SMT_P_REFUSED ;
947         /* length of para is smt_frame + ref_fc */
948         rdf->refused.para.p_len = len + 4 ;
949         rdf->refused.ref_fc = fc ;
950
951         /* swap it back */
952         smt_swap_para(sm,frame_len,0) ;
953
954         memcpy((char *) &rdf->refused.ref_header,(char *) sm,len) ;
955
956         len -= sizeof(struct smt_header) ;
957         mb->sm_len += len ;
958         rdf->smt.smt_len += len ;
959
960         dump_smt(smc,(struct smt_header *)rdf,"RDF") ;
961         smc->mib.priv.fddiPRIVRDF_Tx++ ;
962         smt_send_frame(smc,mb,FC_SMT_INFO,local) ;
963 }
964
965 /*
966  * generate and send NIF
967  */
968 static void smt_send_nif(struct s_smc *smc, const struct fddi_addr *dest, 
969                          int fc, u_long tid, int type, int local)
970 /* struct fddi_addr *dest;      dest address */
971 /* int fc;                      frame control */
972 /* u_long tid;                  transaction id */
973 /* int type;                    frame type */
974 {
975         struct smt_nif  *nif ;
976         SMbuf           *mb ;
977
978         if (!(mb = smt_build_frame(smc,SMT_NIF,type,sizeof(struct smt_nif))))
979                 return ;
980         nif = smtod(mb, struct smt_nif *) ;
981         smt_fill_una(smc,&nif->una) ;   /* set UNA */
982         smt_fill_sde(smc,&nif->sde) ;   /* set station descriptor */
983         smt_fill_state(smc,&nif->state) ;       /* set state information */
984 #ifdef  SMT6_10
985         smt_fill_fsc(smc,&nif->fsc) ;   /* set frame status cap. */
986 #endif
987         nif->smt.smt_dest = *dest ;     /* destination address */
988         nif->smt.smt_tid = tid ;        /* transaction ID */
989         dump_smt(smc,(struct smt_header *)nif,"NIF") ;
990         smt_send_frame(smc,mb,fc,local) ;
991 }
992
993 #ifdef  DEBUG
994 /*
995  * send NIF request (test purpose)
996  */
997 static void smt_send_nif_request(struct s_smc *smc, struct fddi_addr *dest)
998 {
999         smc->sm.pend[SMT_TID_NIF_TEST] = smt_get_tid(smc) ;
1000         smt_send_nif(smc,dest, FC_SMT_INFO, smc->sm.pend[SMT_TID_NIF_TEST],
1001                 SMT_REQUEST,0) ;
1002 }
1003
1004 /*
1005  * send ECF request (test purpose)
1006  */
1007 static void smt_send_ecf_request(struct s_smc *smc, struct fddi_addr *dest,
1008                                  int len)
1009 {
1010         smc->sm.pend[SMT_TID_ECF] = smt_get_tid(smc) ;
1011         smt_send_ecf(smc,dest, FC_SMT_INFO, smc->sm.pend[SMT_TID_ECF],
1012                 SMT_REQUEST,len) ;
1013 }
1014 #endif
1015
1016 /*
1017  * echo test
1018  */
1019 static void smt_echo_test(struct s_smc *smc, int dna)
1020 {
1021         u_long  tid ;
1022
1023         smc->sm.pend[dna ? SMT_TID_ECF_DNA : SMT_TID_ECF_UNA] =
1024                 tid = smt_get_tid(smc) ;
1025         smt_send_ecf(smc, dna ?
1026                 &smc->mib.m[MAC0].fddiMACDownstreamNbr :
1027                 &smc->mib.m[MAC0].fddiMACUpstreamNbr,
1028                 FC_SMT_INFO,tid, SMT_REQUEST, (SMT_TEST_ECHO_LEN & ~3)-8) ;
1029 }
1030
1031 /*
1032  * generate and send ECF
1033  */
1034 static void smt_send_ecf(struct s_smc *smc, struct fddi_addr *dest, int fc,
1035                          u_long tid, int type, int len)
1036 /* struct fddi_addr *dest;      dest address */
1037 /* int fc;                      frame control */
1038 /* u_long tid;                  transaction id */
1039 /* int type;                    frame type */
1040 /* int len;                     frame length */
1041 {
1042         struct smt_ecf  *ecf ;
1043         SMbuf           *mb ;
1044
1045         if (!(mb = smt_build_frame(smc,SMT_ECF,type,SMT_ECF_LEN + len)))
1046                 return ;
1047         ecf = smtod(mb, struct smt_ecf *) ;
1048
1049         smt_fill_echo(smc,&ecf->ec_echo,tid,len) ;      /* set ECHO */
1050         ecf->smt.smt_dest = *dest ;     /* destination address */
1051         ecf->smt.smt_tid = tid ;        /* transaction ID */
1052         smc->mib.priv.fddiPRIVECF_Req_Tx++ ;
1053         smt_send_frame(smc,mb,fc,0) ;
1054 }
1055
1056 /*
1057  * generate and send SIF config response
1058  */
1059
1060 static void smt_send_sif_config(struct s_smc *smc, struct fddi_addr *dest,
1061                                 u_long tid, int local)
1062 /* struct fddi_addr *dest;      dest address */
1063 /* u_long tid;                  transaction id */
1064 {
1065         struct smt_sif_config   *sif ;
1066         SMbuf                   *mb ;
1067         int                     len ;
1068         if (!(mb = smt_build_frame(smc,SMT_SIF_CONFIG,SMT_REPLY,
1069                 SIZEOF_SMT_SIF_CONFIG)))
1070                 return ;
1071
1072         sif = smtod(mb, struct smt_sif_config *) ;
1073         smt_fill_timestamp(smc,&sif->ts) ;      /* set time stamp */
1074         smt_fill_sde(smc,&sif->sde) ;           /* set station descriptor */
1075         smt_fill_version(smc,&sif->version) ;   /* set version information */
1076         smt_fill_state(smc,&sif->state) ;       /* set state information */
1077         smt_fill_policy(smc,&sif->policy) ;     /* set station policy */
1078         smt_fill_latency(smc,&sif->latency);    /* set station latency */
1079         smt_fill_neighbor(smc,&sif->neighbor);  /* set station neighbor */
1080         smt_fill_setcount(smc,&sif->setcount) ; /* set count */
1081         len = smt_fill_path(smc,&sif->path);    /* set station path descriptor*/
1082         sif->smt.smt_dest = *dest ;             /* destination address */
1083         sif->smt.smt_tid = tid ;                /* transaction ID */
1084         smt_add_frame_len(mb,len) ;             /* adjust length fields */
1085         dump_smt(smc,(struct smt_header *)sif,"SIF Configuration Reply") ;
1086         smt_send_frame(smc,mb,FC_SMT_INFO,local) ;
1087 }
1088
1089 /*
1090  * generate and send SIF operation response
1091  */
1092
1093 static void smt_send_sif_operation(struct s_smc *smc, struct fddi_addr *dest,
1094                                    u_long tid, int local)
1095 /* struct fddi_addr *dest;      dest address */
1096 /* u_long tid;                  transaction id */
1097 {
1098         struct smt_sif_operation *sif ;
1099         SMbuf                   *mb ;
1100         int                     ports ;
1101         int                     i ;
1102
1103         ports = NUMPHYS ;
1104 #ifndef CONCENTRATOR
1105         if (smc->s.sas == SMT_SAS)
1106                 ports = 1 ;
1107 #endif
1108
1109         if (!(mb = smt_build_frame(smc,SMT_SIF_OPER,SMT_REPLY,
1110                 SIZEOF_SMT_SIF_OPERATION+ports*sizeof(struct smt_p_lem))))
1111                 return ;
1112         sif = smtod(mb, struct smt_sif_operation *) ;
1113         smt_fill_timestamp(smc,&sif->ts) ;      /* set time stamp */
1114         smt_fill_mac_status(smc,&sif->status) ; /* set mac status */
1115         smt_fill_mac_counter(smc,&sif->mc) ; /* set mac counter field */
1116         smt_fill_mac_fnc(smc,&sif->fnc) ; /* set frame not copied counter */
1117         smt_fill_manufacturer(smc,&sif->man) ; /* set manufacturer field */
1118         smt_fill_user(smc,&sif->user) ;         /* set user field */
1119         smt_fill_setcount(smc,&sif->setcount) ; /* set count */
1120         /*
1121          * set link error mon information
1122          */
1123         if (ports == 1) {
1124                 smt_fill_lem(smc,sif->lem,PS) ;
1125         }
1126         else {
1127                 for (i = 0 ; i < ports ; i++) {
1128                         smt_fill_lem(smc,&sif->lem[i],i) ;
1129                 }
1130         }
1131
1132         sif->smt.smt_dest = *dest ;     /* destination address */
1133         sif->smt.smt_tid = tid ;        /* transaction ID */
1134         dump_smt(smc,(struct smt_header *)sif,"SIF Operation Reply") ;
1135         smt_send_frame(smc,mb,FC_SMT_INFO,local) ;
1136 }
1137
1138 /*
1139  * get and initialize SMT frame
1140  */
1141 SMbuf *smt_build_frame(struct s_smc *smc, int class, int type,
1142                                   int length)
1143 {
1144         SMbuf                   *mb ;
1145         struct smt_header       *smt ;
1146
1147 #if     0
1148         if (!smc->r.sm_ma_avail) {
1149                 return(0) ;
1150         }
1151 #endif
1152         if (!(mb = smt_get_mbuf(smc)))
1153                 return(mb) ;
1154
1155         mb->sm_len = length ;
1156         smt = smtod(mb, struct smt_header *) ;
1157         smt->smt_dest = fddi_broadcast ; /* set dest = broadcast */
1158         smt->smt_class = class ;
1159         smt->smt_type = type ;
1160         switch (class) {
1161         case SMT_NIF :
1162         case SMT_SIF_CONFIG :
1163         case SMT_SIF_OPER :
1164         case SMT_ECF :
1165                 smt->smt_version = SMT_VID ;
1166                 break ;
1167         default :
1168                 smt->smt_version = SMT_VID_2 ;
1169                 break ;
1170         }
1171         smt->smt_tid = smt_get_tid(smc) ;       /* set transaction ID */
1172         smt->smt_pad = 0 ;
1173         smt->smt_len = length - sizeof(struct smt_header) ;
1174         return(mb) ;
1175 }
1176
1177 static void smt_add_frame_len(SMbuf *mb, int len)
1178 {
1179         struct smt_header       *smt ;
1180
1181         smt = smtod(mb, struct smt_header *) ;
1182         smt->smt_len += len ;
1183         mb->sm_len += len ;
1184 }
1185
1186
1187
1188 /*
1189  * fill values in UNA parameter
1190  */
1191 static void smt_fill_una(struct s_smc *smc, struct smt_p_una *una)
1192 {
1193         SMTSETPARA(una,SMT_P_UNA) ;
1194         una->una_pad = 0 ;
1195         una->una_node = smc->mib.m[MAC0].fddiMACUpstreamNbr ;
1196 }
1197
1198 /*
1199  * fill values in SDE parameter
1200  */
1201 static void smt_fill_sde(struct s_smc *smc, struct smt_p_sde *sde)
1202 {
1203         SMTSETPARA(sde,SMT_P_SDE) ;
1204         sde->sde_non_master = smc->mib.fddiSMTNonMaster_Ct ;
1205         sde->sde_master = smc->mib.fddiSMTMaster_Ct ;
1206         sde->sde_mac_count = NUMMACS ;          /* only 1 MAC */
1207 #ifdef  CONCENTRATOR
1208         sde->sde_type = SMT_SDE_CONCENTRATOR ;
1209 #else
1210         sde->sde_type = SMT_SDE_STATION ;
1211 #endif
1212 }
1213
1214 /*
1215  * fill in values in station state parameter
1216  */
1217 static void smt_fill_state(struct s_smc *smc, struct smt_p_state *state)
1218 {
1219         int     top ;
1220         int     twist ;
1221
1222         SMTSETPARA(state,SMT_P_STATE) ;
1223         state->st_pad = 0 ;
1224
1225         /* determine topology */
1226         top = 0 ;
1227         if (smc->mib.fddiSMTPeerWrapFlag) {
1228                 top |= SMT_ST_WRAPPED ;         /* state wrapped */
1229         }
1230 #ifdef  CONCENTRATOR
1231         if (cfm_status_unattached(smc)) {
1232                 top |= SMT_ST_UNATTACHED ;      /* unattached concentrator */
1233         }
1234 #endif
1235         if ((twist = pcm_status_twisted(smc)) & 1) {
1236                 top |= SMT_ST_TWISTED_A ;       /* twisted cable */
1237         }
1238         if (twist & 2) {
1239                 top |= SMT_ST_TWISTED_B ;       /* twisted cable */
1240         }
1241 #ifdef  OPT_SRF
1242         top |= SMT_ST_SRF ;
1243 #endif
1244         if (pcm_rooted_station(smc))
1245                 top |= SMT_ST_ROOTED_S ;
1246         if (smc->mib.a[0].fddiPATHSbaPayload != 0)
1247                 top |= SMT_ST_SYNC_SERVICE ;
1248         state->st_topology = top ;
1249         state->st_dupl_addr =
1250                 ((smc->mib.m[MAC0].fddiMACDA_Flag ? SMT_ST_MY_DUPA : 0 ) |
1251                  (smc->mib.m[MAC0].fddiMACUNDA_Flag ? SMT_ST_UNA_DUPA : 0)) ;
1252 }
1253
1254 /*
1255  * fill values in timestamp parameter
1256  */
1257 static void smt_fill_timestamp(struct s_smc *smc, struct smt_p_timestamp *ts)
1258 {
1259
1260         SMTSETPARA(ts,SMT_P_TIMESTAMP) ;
1261         smt_set_timestamp(smc,ts->ts_time) ;
1262 }
1263
1264 void smt_set_timestamp(struct s_smc *smc, u_char *p)
1265 {
1266         u_long  time ;
1267         u_long  utime ;
1268
1269         /*
1270          * timestamp is 64 bits long ; resolution is 80 nS
1271          * our clock resolution is 10mS
1272          * 10mS/80ns = 125000 ~ 2^17 = 131072
1273          */
1274         utime = smt_get_time() ;
1275         time = utime * 100 ;
1276         time /= TICKS_PER_SECOND ;
1277         p[0] = 0 ;
1278         p[1] = (u_char)((time>>(8+8+8+8-1)) & 1) ;
1279         p[2] = (u_char)(time>>(8+8+8-1)) ;
1280         p[3] = (u_char)(time>>(8+8-1)) ;
1281         p[4] = (u_char)(time>>(8-1)) ;
1282         p[5] = (u_char)(time<<1) ;
1283         p[6] = (u_char)(smc->sm.uniq_ticks>>8) ;
1284         p[7] = (u_char)smc->sm.uniq_ticks ;
1285         /*
1286          * make sure we don't wrap: restart whenever the upper digits change
1287          */
1288         if (utime != smc->sm.uniq_time) {
1289                 smc->sm.uniq_ticks = 0 ;
1290         }
1291         smc->sm.uniq_ticks++ ;
1292         smc->sm.uniq_time = utime ;
1293 }
1294
1295 /*
1296  * fill values in station policy parameter
1297  */
1298 static void smt_fill_policy(struct s_smc *smc, struct smt_p_policy *policy)
1299 {
1300         int     i ;
1301         u_char  *map ;
1302         u_short in ;
1303         u_short out ;
1304
1305         /*
1306          * MIB para 101b (fddiSMTConnectionPolicy) coding
1307          * is different from 0005 coding
1308          */
1309         static u_char   ansi_weirdness[16] = {
1310                 0,7,5,3,8,1,6,4,9,10,2,11,12,13,14,15
1311         } ;
1312         SMTSETPARA(policy,SMT_P_POLICY) ;
1313
1314         out = 0 ;
1315         in = smc->mib.fddiSMTConnectionPolicy ;
1316         for (i = 0, map = ansi_weirdness ; i < 16 ; i++) {
1317                 if (in & 1)
1318                         out |= (1<<*map) ;
1319                 in >>= 1 ;
1320                 map++ ;
1321         }
1322         policy->pl_config = smc->mib.fddiSMTConfigPolicy ;
1323         policy->pl_connect = out ;
1324 }
1325
1326 /*
1327  * fill values in latency equivalent parameter
1328  */
1329 static void smt_fill_latency(struct s_smc *smc, struct smt_p_latency *latency)
1330 {
1331         SMTSETPARA(latency,SMT_P_LATENCY) ;
1332
1333         latency->lt_phyout_idx1 = phy_index(smc,0) ;
1334         latency->lt_latency1 = 10 ;     /* in octets (byte clock) */
1335         /*
1336          * note: latency has two phy entries by definition
1337          * for a SAS, the 2nd one is null
1338          */
1339         if (smc->s.sas == SMT_DAS) {
1340                 latency->lt_phyout_idx2 = phy_index(smc,1) ;
1341                 latency->lt_latency2 = 10 ;     /* in octets (byte clock) */
1342         }
1343         else {
1344                 latency->lt_phyout_idx2 = 0 ;
1345                 latency->lt_latency2 = 0 ;
1346         }
1347 }
1348
1349 /*
1350  * fill values in MAC neighbors parameter
1351  */
1352 static void smt_fill_neighbor(struct s_smc *smc, struct smt_p_neighbor *neighbor)
1353 {
1354         SMTSETPARA(neighbor,SMT_P_NEIGHBORS) ;
1355
1356         neighbor->nb_mib_index = INDEX_MAC ;
1357         neighbor->nb_mac_index = mac_index(smc,1) ;
1358         neighbor->nb_una = smc->mib.m[MAC0].fddiMACUpstreamNbr ;
1359         neighbor->nb_dna = smc->mib.m[MAC0].fddiMACDownstreamNbr ;
1360 }
1361
1362 /*
1363  * fill values in path descriptor
1364  */
1365 #ifdef  CONCENTRATOR
1366 #define ALLPHYS NUMPHYS
1367 #else
1368 #define ALLPHYS ((smc->s.sas == SMT_SAS) ? 1 : 2)
1369 #endif
1370
1371 static int smt_fill_path(struct s_smc *smc, struct smt_p_path *path)
1372 {
1373         SK_LOC_DECL(int,type) ;
1374         SK_LOC_DECL(int,state) ;
1375         SK_LOC_DECL(int,remote) ;
1376         SK_LOC_DECL(int,mac) ;
1377         int     len ;
1378         int     p ;
1379         int     physp ;
1380         struct smt_phy_rec      *phy ;
1381         struct smt_mac_rec      *pd_mac ;
1382
1383         len =   PARA_LEN +
1384                 sizeof(struct smt_mac_rec) * NUMMACS +
1385                 sizeof(struct smt_phy_rec) * ALLPHYS ;
1386         path->para.p_type = SMT_P_PATH ;
1387         path->para.p_len = len - PARA_LEN ;
1388
1389         /* PHYs */
1390         for (p = 0,phy = path->pd_phy ; p < ALLPHYS ; p++, phy++) {
1391                 physp = p ;
1392 #ifndef CONCENTRATOR
1393                 if (smc->s.sas == SMT_SAS)
1394                         physp = PS ;
1395 #endif
1396                 pcm_status_state(smc,physp,&type,&state,&remote,&mac) ;
1397 #ifdef  LITTLE_ENDIAN
1398                 phy->phy_mib_index = smt_swap_short((u_short)p+INDEX_PORT) ;
1399 #else
1400                 phy->phy_mib_index = p+INDEX_PORT ;
1401 #endif
1402                 phy->phy_type = type ;
1403                 phy->phy_connect_state = state ;
1404                 phy->phy_remote_type = remote ;
1405                 phy->phy_remote_mac = mac ;
1406                 phy->phy_resource_idx = phy_con_resource_index(smc,p) ;
1407         }
1408
1409         /* MAC */
1410         pd_mac = (struct smt_mac_rec *) phy ;
1411         pd_mac->mac_addr = smc->mib.m[MAC0].fddiMACSMTAddress ;
1412         pd_mac->mac_resource_idx = mac_con_resource_index(smc,1) ;
1413         return(len) ;
1414 }
1415
1416 /*
1417  * fill values in mac status
1418  */
1419 static void smt_fill_mac_status(struct s_smc *smc, struct smt_p_mac_status *st)
1420 {
1421         SMTSETPARA(st,SMT_P_MAC_STATUS) ;
1422
1423         st->st_mib_index = INDEX_MAC ;
1424         st->st_mac_index = mac_index(smc,1) ;
1425
1426         mac_update_counter(smc) ;
1427         /*
1428          * timer values are represented in SMT as 2's complement numbers
1429          * units :      internal :  2's complement BCLK
1430          */
1431         st->st_t_req = smc->mib.m[MAC0].fddiMACT_Req ;
1432         st->st_t_neg = smc->mib.m[MAC0].fddiMACT_Neg ;
1433         st->st_t_max = smc->mib.m[MAC0].fddiMACT_Max ;
1434         st->st_tvx_value = smc->mib.m[MAC0].fddiMACTvxValue ;
1435         st->st_t_min = smc->mib.m[MAC0].fddiMACT_Min ;
1436
1437         st->st_sba = smc->mib.a[PATH0].fddiPATHSbaPayload ;
1438         st->st_frame_ct = smc->mib.m[MAC0].fddiMACFrame_Ct ;
1439         st->st_error_ct = smc->mib.m[MAC0].fddiMACError_Ct ;
1440         st->st_lost_ct = smc->mib.m[MAC0].fddiMACLost_Ct ;
1441 }
1442
1443 /*
1444  * fill values in LEM status
1445  */
1446 static void smt_fill_lem(struct s_smc *smc, struct smt_p_lem *lem, int phy)
1447 {
1448         struct fddi_mib_p       *mib ;
1449
1450         mib = smc->y[phy].mib ;
1451
1452         SMTSETPARA(lem,SMT_P_LEM) ;
1453         lem->lem_mib_index = phy+INDEX_PORT ;
1454         lem->lem_phy_index = phy_index(smc,phy) ;
1455         lem->lem_pad2 = 0 ;
1456         lem->lem_cutoff = mib->fddiPORTLer_Cutoff ;
1457         lem->lem_alarm = mib->fddiPORTLer_Alarm ;
1458         /* long term bit error rate */
1459         lem->lem_estimate = mib->fddiPORTLer_Estimate ;
1460         /* # of rejected connections */
1461         lem->lem_reject_ct = mib->fddiPORTLem_Reject_Ct ;
1462         lem->lem_ct = mib->fddiPORTLem_Ct ;     /* total number of errors */
1463 }
1464
1465 /*
1466  * fill version parameter
1467  */
1468 static void smt_fill_version(struct s_smc *smc, struct smt_p_version *vers)
1469 {
1470         SK_UNUSED(smc) ;
1471         SMTSETPARA(vers,SMT_P_VERSION) ;
1472         vers->v_pad = 0 ;
1473         vers->v_n = 1 ;                         /* one version is enough .. */
1474         vers->v_index = 1 ;
1475         vers->v_version[0] = SMT_VID_2 ;
1476         vers->v_pad2 = 0 ;
1477 }
1478
1479 #ifdef  SMT6_10
1480 /*
1481  * fill frame status capabilities
1482  */
1483 /*
1484  * note: this para 200B is NOT in swap table, because it's also set in
1485  * PMF add_para
1486  */
1487 static void smt_fill_fsc(struct s_smc *smc, struct smt_p_fsc *fsc)
1488 {
1489         SK_UNUSED(smc) ;
1490         SMTSETPARA(fsc,SMT_P_FSC) ;
1491         fsc->fsc_pad0 = 0 ;
1492         fsc->fsc_mac_index = INDEX_MAC ;        /* this is MIB ; MIB is NOT
1493                                                  * mac_index ()i !
1494                                                  */
1495         fsc->fsc_pad1 = 0 ;
1496         fsc->fsc_value = FSC_TYPE0 ;            /* "normal" node */
1497 #ifdef  LITTLE_ENDIAN
1498         fsc->fsc_mac_index = smt_swap_short(INDEX_MAC) ;
1499         fsc->fsc_value = smt_swap_short(FSC_TYPE0) ;
1500 #endif
1501 }
1502 #endif
1503
1504 /*
1505  * fill mac counter field
1506  */
1507 static void smt_fill_mac_counter(struct s_smc *smc, struct smt_p_mac_counter *mc)
1508 {
1509         SMTSETPARA(mc,SMT_P_MAC_COUNTER) ;
1510         mc->mc_mib_index = INDEX_MAC ;
1511         mc->mc_index = mac_index(smc,1) ;
1512         mc->mc_receive_ct = smc->mib.m[MAC0].fddiMACCopied_Ct ;
1513         mc->mc_transmit_ct =  smc->mib.m[MAC0].fddiMACTransmit_Ct ;
1514 }
1515
1516 /*
1517  * fill mac frame not copied counter
1518  */
1519 static void smt_fill_mac_fnc(struct s_smc *smc, struct smt_p_mac_fnc *fnc)
1520 {
1521         SMTSETPARA(fnc,SMT_P_MAC_FNC) ;
1522         fnc->nc_mib_index = INDEX_MAC ;
1523         fnc->nc_index = mac_index(smc,1) ;
1524         fnc->nc_counter = smc->mib.m[MAC0].fddiMACNotCopied_Ct ;
1525 }
1526
1527
1528 /*
1529  * fill manufacturer field
1530  */
1531 static void smt_fill_manufacturer(struct s_smc *smc, 
1532                                   struct smp_p_manufacturer *man)
1533 {
1534         SMTSETPARA(man,SMT_P_MANUFACTURER) ;
1535         memcpy((char *) man->mf_data,
1536                 (char *) smc->mib.fddiSMTManufacturerData,
1537                 sizeof(man->mf_data)) ;
1538 }
1539
1540 /*
1541  * fill user field
1542  */
1543 static void smt_fill_user(struct s_smc *smc, struct smp_p_user *user)
1544 {
1545         SMTSETPARA(user,SMT_P_USER) ;
1546         memcpy((char *) user->us_data,
1547                 (char *) smc->mib.fddiSMTUserData,
1548                 sizeof(user->us_data)) ;
1549 }
1550
1551 /*
1552  * fill set count
1553  */
1554 static void smt_fill_setcount(struct s_smc *smc, struct smt_p_setcount *setcount)
1555 {
1556         SK_UNUSED(smc) ;
1557         SMTSETPARA(setcount,SMT_P_SETCOUNT) ;
1558         setcount->count = smc->mib.fddiSMTSetCount.count ;
1559         memcpy((char *)setcount->timestamp,
1560                 (char *)smc->mib.fddiSMTSetCount.timestamp,8) ;
1561 }
1562
1563 /*
1564  * fill echo data
1565  */
1566 static void smt_fill_echo(struct s_smc *smc, struct smt_p_echo *echo, u_long seed,
1567                           int len)
1568 {
1569         u_char  *p ;
1570
1571         SK_UNUSED(smc) ;
1572         SMTSETPARA(echo,SMT_P_ECHODATA) ;
1573         echo->para.p_len = len ;
1574         for (p = echo->ec_data ; len ; len--) {
1575                 *p++ = (u_char) seed ;
1576                 seed += 13 ;
1577         }
1578 }
1579
1580 /*
1581  * clear DNA and UNA
1582  * called from CFM if configuration changes
1583  */
1584 void smt_clear_una_dna(struct s_smc *smc)
1585 {
1586         smc->mib.m[MAC0].fddiMACUpstreamNbr = SMT_Unknown ;
1587         smc->mib.m[MAC0].fddiMACDownstreamNbr = SMT_Unknown ;
1588 }
1589
1590 static void smt_clear_old_una_dna(struct s_smc *smc)
1591 {
1592         smc->mib.m[MAC0].fddiMACOldUpstreamNbr = SMT_Unknown ;
1593         smc->mib.m[MAC0].fddiMACOldDownstreamNbr = SMT_Unknown ;
1594 }
1595
1596 u_long smt_get_tid(struct s_smc *smc)
1597 {
1598         u_long  tid ;
1599         while ((tid = ++(smc->sm.smt_tid) ^ SMT_TID_MAGIC) == 0)
1600                 ;
1601         return(tid & 0x3fffffffL) ;
1602 }
1603
1604
1605 /*
1606  * table of parameter lengths
1607  */
1608 static const struct smt_pdef {
1609         int     ptype ;
1610         int     plen ;
1611         const char      *pswap ;
1612 } smt_pdef[] = {
1613         { SMT_P_UNA,    sizeof(struct smt_p_una) ,
1614                 SWAP_SMT_P_UNA                                  } ,
1615         { SMT_P_SDE,    sizeof(struct smt_p_sde) ,
1616                 SWAP_SMT_P_SDE                                  } ,
1617         { SMT_P_STATE,  sizeof(struct smt_p_state) ,
1618                 SWAP_SMT_P_STATE                                } ,
1619         { SMT_P_TIMESTAMP,sizeof(struct smt_p_timestamp) ,
1620                 SWAP_SMT_P_TIMESTAMP                            } ,
1621         { SMT_P_POLICY, sizeof(struct smt_p_policy) ,
1622                 SWAP_SMT_P_POLICY                               } ,
1623         { SMT_P_LATENCY,        sizeof(struct smt_p_latency) ,
1624                 SWAP_SMT_P_LATENCY                              } ,
1625         { SMT_P_NEIGHBORS,sizeof(struct smt_p_neighbor) ,
1626                 SWAP_SMT_P_NEIGHBORS                            } ,
1627         { SMT_P_PATH,   sizeof(struct smt_p_path) ,
1628                 SWAP_SMT_P_PATH                                 } ,
1629         { SMT_P_MAC_STATUS,sizeof(struct smt_p_mac_status) ,
1630                 SWAP_SMT_P_MAC_STATUS                           } ,
1631         { SMT_P_LEM,    sizeof(struct smt_p_lem) ,
1632                 SWAP_SMT_P_LEM                                  } ,
1633         { SMT_P_MAC_COUNTER,sizeof(struct smt_p_mac_counter) ,
1634                 SWAP_SMT_P_MAC_COUNTER                          } ,
1635         { SMT_P_MAC_FNC,sizeof(struct smt_p_mac_fnc) ,
1636                 SWAP_SMT_P_MAC_FNC                              } ,
1637         { SMT_P_PRIORITY,sizeof(struct smt_p_priority) ,
1638                 SWAP_SMT_P_PRIORITY                             } ,
1639         { SMT_P_EB,sizeof(struct smt_p_eb) ,
1640                 SWAP_SMT_P_EB                                   } ,
1641         { SMT_P_MANUFACTURER,sizeof(struct smp_p_manufacturer) ,
1642                 SWAP_SMT_P_MANUFACTURER                         } ,
1643         { SMT_P_REASON, sizeof(struct smt_p_reason) ,
1644                 SWAP_SMT_P_REASON                               } ,
1645         { SMT_P_REFUSED, sizeof(struct smt_p_refused) ,
1646                 SWAP_SMT_P_REFUSED                              } ,
1647         { SMT_P_VERSION, sizeof(struct smt_p_version) ,
1648                 SWAP_SMT_P_VERSION                              } ,
1649 #ifdef ESS
1650         { SMT_P0015, sizeof(struct smt_p_0015) , SWAP_SMT_P0015 } ,
1651         { SMT_P0016, sizeof(struct smt_p_0016) , SWAP_SMT_P0016 } ,
1652         { SMT_P0017, sizeof(struct smt_p_0017) , SWAP_SMT_P0017 } ,
1653         { SMT_P0018, sizeof(struct smt_p_0018) , SWAP_SMT_P0018 } ,
1654         { SMT_P0019, sizeof(struct smt_p_0019) , SWAP_SMT_P0019 } ,
1655         { SMT_P001A, sizeof(struct smt_p_001a) , SWAP_SMT_P001A } ,
1656         { SMT_P001B, sizeof(struct smt_p_001b) , SWAP_SMT_P001B } ,
1657         { SMT_P001C, sizeof(struct smt_p_001c) , SWAP_SMT_P001C } ,
1658         { SMT_P001D, sizeof(struct smt_p_001d) , SWAP_SMT_P001D } ,
1659 #endif
1660 #if     0
1661         { SMT_P_FSC,    sizeof(struct smt_p_fsc) ,
1662                 SWAP_SMT_P_FSC                                  } ,
1663 #endif
1664
1665         { SMT_P_SETCOUNT,0,     SWAP_SMT_P_SETCOUNT             } ,
1666         { SMT_P1048,    0,      SWAP_SMT_P1048                  } ,
1667         { SMT_P208C,    0,      SWAP_SMT_P208C                  } ,
1668         { SMT_P208D,    0,      SWAP_SMT_P208D                  } ,
1669         { SMT_P208E,    0,      SWAP_SMT_P208E                  } ,
1670         { SMT_P208F,    0,      SWAP_SMT_P208F                  } ,
1671         { SMT_P2090,    0,      SWAP_SMT_P2090                  } ,
1672 #ifdef  ESS
1673         { SMT_P320B, sizeof(struct smt_p_320b) , SWAP_SMT_P320B } ,
1674         { SMT_P320F, sizeof(struct smt_p_320f) , SWAP_SMT_P320F } ,
1675         { SMT_P3210, sizeof(struct smt_p_3210) , SWAP_SMT_P3210 } ,
1676 #endif
1677         { SMT_P4050,    0,      SWAP_SMT_P4050                  } ,
1678         { SMT_P4051,    0,      SWAP_SMT_P4051                  } ,
1679         { SMT_P4052,    0,      SWAP_SMT_P4052                  } ,
1680         { SMT_P4053,    0,      SWAP_SMT_P4053                  } ,
1681 } ;
1682
1683 #define N_SMT_PLEN      (sizeof(smt_pdef)/sizeof(smt_pdef[0]))
1684
1685 int smt_check_para(struct s_smc *smc, struct smt_header *sm,
1686                    const u_short list[])
1687 {
1688         const u_short           *p = list ;
1689         while (*p) {
1690                 if (!sm_to_para(smc,sm,(int) *p)) {
1691                         DB_SMT("SMT: smt_check_para - missing para %x\n",*p,0);
1692                         return(-1) ;
1693                 }
1694                 p++ ;
1695         }
1696         return(0) ;
1697 }
1698
1699 void *sm_to_para(struct s_smc *smc, struct smt_header *sm, int para)
1700 {
1701         char    *p ;
1702         int     len ;
1703         int     plen ;
1704         void    *found = NULL;
1705
1706         SK_UNUSED(smc) ;
1707
1708         len = sm->smt_len ;
1709         p = (char *)(sm+1) ;            /* pointer to info */
1710         while (len > 0 ) {
1711                 if (((struct smt_para *)p)->p_type == para)
1712                         found = (void *) p ;
1713                 plen = ((struct smt_para *)p)->p_len + PARA_LEN ;
1714                 p += plen ;
1715                 len -= plen ;
1716                 if (len < 0) {
1717                         DB_SMT("SMT : sm_to_para - length error %d\n",plen,0) ;
1718                         return NULL;
1719                 }
1720                 if ((plen & 3) && (para != SMT_P_ECHODATA)) {
1721                         DB_SMT("SMT : sm_to_para - odd length %d\n",plen,0) ;
1722                         return NULL;
1723                 }
1724                 if (found)
1725                         return(found) ;
1726         }
1727         return NULL;
1728 }
1729
1730 #if     0
1731 /*
1732  * send ANTC data test frame
1733  */
1734 void fddi_send_antc(struct s_smc *smc, struct fddi_addr *dest)
1735 {
1736         SK_UNUSED(smc) ;
1737         SK_UNUSED(dest) ;
1738 #if     0
1739         SMbuf                   *mb ;
1740         struct smt_header       *smt ;
1741         int                     i ;
1742         char                    *p ;
1743
1744         mb = smt_get_mbuf() ;
1745         mb->sm_len = 3000+12 ;
1746         p = smtod(mb, char *) + 12 ;
1747         for (i = 0 ; i < 3000 ; i++)
1748                 *p++ = 1 << (i&7) ;
1749
1750         smt = smtod(mb, struct smt_header *) ;
1751         smt->smt_dest = *dest ;
1752         smt->smt_source = smc->mib.m[MAC0].fddiMACSMTAddress ;
1753         smt_send_mbuf(smc,mb,FC_ASYNC_LLC) ;
1754 #endif
1755 }
1756 #endif
1757
1758 #ifdef  DEBUG
1759 #define hextoasc(x)     "0123456789abcdef"[x]
1760
1761 char *addr_to_string(struct fddi_addr *addr)
1762 {
1763         int     i ;
1764         static char     string[6*3] = "****" ;
1765
1766         for (i = 0 ; i < 6 ; i++) {
1767                 string[i*3] = hextoasc((addr->a[i]>>4)&0xf) ;
1768                 string[i*3+1] = hextoasc((addr->a[i])&0xf) ;
1769                 string[i*3+2] = ':' ;
1770         }
1771         string[5*3+2] = 0 ;
1772         return(string) ;
1773 }
1774 #endif
1775
1776 #ifdef  AM29K
1777 smt_ifconfig(int argc, char *argv[])
1778 {
1779         if (argc >= 2 && !strcmp(argv[0],"opt_bypass") &&
1780             !strcmp(argv[1],"yes")) {
1781                 smc->mib.fddiSMTBypassPresent = 1 ;
1782                 return(0) ;
1783         }
1784         return(amdfddi_config(0,argc,argv)) ;
1785 }
1786 #endif
1787
1788 /*
1789  * return static mac index
1790  */
1791 static int mac_index(struct s_smc *smc, int mac)
1792 {
1793         SK_UNUSED(mac) ;
1794 #ifdef  CONCENTRATOR
1795         SK_UNUSED(smc) ;
1796         return(NUMPHYS+1) ;
1797 #else
1798         return((smc->s.sas == SMT_SAS) ? 2 : 3) ;
1799 #endif
1800 }
1801
1802 /*
1803  * return static phy index
1804  */
1805 static int phy_index(struct s_smc *smc, int phy)
1806 {
1807         SK_UNUSED(smc) ;
1808         return(phy+1);
1809 }
1810
1811 /*
1812  * return dynamic mac connection resource index
1813  */
1814 static int mac_con_resource_index(struct s_smc *smc, int mac)
1815 {
1816 #ifdef  CONCENTRATOR
1817         SK_UNUSED(smc) ;
1818         SK_UNUSED(mac) ;
1819         return(entity_to_index(smc,cem_get_downstream(smc,ENTITY_MAC))) ;
1820 #else
1821         SK_UNUSED(mac) ;
1822         switch (smc->mib.fddiSMTCF_State) {
1823         case SC9_C_WRAP_A :
1824         case SC5_THRU_B :
1825         case SC11_C_WRAP_S :
1826                 return(1) ;
1827         case SC10_C_WRAP_B :
1828         case SC4_THRU_A :
1829                 return(2) ;
1830         }
1831         return(smc->s.sas == SMT_SAS ? 2 : 3) ;
1832 #endif
1833 }
1834
1835 /*
1836  * return dynamic phy connection resource index
1837  */
1838 static int phy_con_resource_index(struct s_smc *smc, int phy)
1839 {
1840 #ifdef  CONCENTRATOR
1841         return(entity_to_index(smc,cem_get_downstream(smc,ENTITY_PHY(phy)))) ;
1842 #else
1843         switch (smc->mib.fddiSMTCF_State) {
1844         case SC9_C_WRAP_A :
1845                 return(phy == PA ? 3 : 2) ;
1846         case SC10_C_WRAP_B :
1847                 return(phy == PA ? 1 : 3) ;
1848         case SC4_THRU_A :
1849                 return(phy == PA ? 3 : 1) ;
1850         case SC5_THRU_B :
1851                 return(phy == PA ? 2 : 3) ;
1852         case SC11_C_WRAP_S :
1853                 return(2) ;
1854         }
1855         return(phy) ;
1856 #endif
1857 }
1858
1859 #ifdef  CONCENTRATOR
1860 static int entity_to_index(struct s_smc *smc, int e)
1861 {
1862         if (e == ENTITY_MAC)
1863                 return(mac_index(smc,1)) ;
1864         else
1865                 return(phy_index(smc,e - ENTITY_PHY(0))) ;
1866 }
1867 #endif
1868
1869 #ifdef  LITTLE_ENDIAN
1870 static int smt_swap_short(u_short s)
1871 {
1872         return(((s>>8)&0xff)|((s&0xff)<<8)) ;
1873 }
1874
1875 void smt_swap_para(struct smt_header *sm, int len, int direction)
1876 /* int direction;       0 encode 1 decode */
1877 {
1878         struct smt_para *pa ;
1879         const  struct smt_pdef  *pd ;
1880         char    *p ;
1881         int     plen ;
1882         int     type ;
1883         int     i ;
1884
1885 /*      printf("smt_swap_para sm %x len %d dir %d\n",
1886                 sm,len,direction) ;
1887  */
1888         smt_string_swap((char *)sm,SWAP_SMTHEADER,len) ;
1889
1890         /* swap args */
1891         len -= sizeof(struct smt_header) ;
1892
1893         p = (char *) (sm + 1) ;
1894         while (len > 0) {
1895                 pa = (struct smt_para *) p ;
1896                 plen = pa->p_len ;
1897                 type = pa->p_type ;
1898                 pa->p_type = smt_swap_short(pa->p_type) ;
1899                 pa->p_len = smt_swap_short(pa->p_len) ;
1900                 if (direction) {
1901                         plen = pa->p_len ;
1902                         type = pa->p_type ;
1903                 }
1904                 /*
1905                  * note: paras can have 0 length !
1906                  */
1907                 if (plen < 0)
1908                         break ;
1909                 plen += PARA_LEN ;
1910                 for (i = N_SMT_PLEN, pd = smt_pdef; i ; i--,pd++) {
1911                         if (pd->ptype == type)
1912                                 break ;
1913                 }
1914                 if (i && pd->pswap) {
1915                         smt_string_swap(p+PARA_LEN,pd->pswap,len) ;
1916                 }
1917                 len -= plen ;
1918                 p += plen ;
1919         }
1920 }
1921
1922 static void smt_string_swap(char *data, const char *format, int len)
1923 {
1924         const char      *open_paren = 0 ;
1925         int     x ;
1926
1927         while (len > 0  && *format) {
1928                 switch (*format) {
1929                 case '[' :
1930                         open_paren = format ;
1931                         break ;
1932                 case ']' :
1933                         format = open_paren ;
1934                         break ;
1935                 case '1' :
1936                 case '2' :
1937                 case '3' :
1938                 case '4' :
1939                 case '5' :
1940                 case '6' :
1941                 case '7' :
1942                 case '8' :
1943                 case '9' :
1944                         data  += *format - '0' ;
1945                         len   -= *format - '0' ;
1946                         break ;
1947                 case 'c':
1948                         data++ ;
1949                         len-- ;
1950                         break ;
1951                 case 's' :
1952                         x = data[0] ;
1953                         data[0] = data[1] ;
1954                         data[1] = x ;
1955                         data += 2 ;
1956                         len -= 2 ;
1957                         break ;
1958                 case 'l' :
1959                         x = data[0] ;
1960                         data[0] = data[3] ;
1961                         data[3] = x ;
1962                         x = data[1] ;
1963                         data[1] = data[2] ;
1964                         data[2] = x ;
1965                         data += 4 ;
1966                         len -= 4 ;
1967                         break ;
1968                 }
1969                 format++ ;
1970         }
1971 }
1972 #else
1973 void smt_swap_para(struct smt_header *sm, int len, int direction)
1974 /* int direction;       0 encode 1 decode */
1975 {
1976         SK_UNUSED(sm) ;
1977         SK_UNUSED(len) ;
1978         SK_UNUSED(direction) ;
1979 }
1980 #endif
1981
1982 /*
1983  * PMF actions
1984  */
1985 int smt_action(struct s_smc *smc, int class, int code, int index)
1986 {
1987         int     event ;
1988         int     port ;
1989         DB_SMT("SMT: action %d code %d\n",class,code) ;
1990         switch(class) {
1991         case SMT_STATION_ACTION :
1992                 switch(code) {
1993                 case SMT_STATION_ACTION_CONNECT :
1994                         smc->mib.fddiSMTRemoteDisconnectFlag = FALSE ;
1995                         queue_event(smc,EVENT_ECM,EC_CONNECT) ;
1996                         break ;
1997                 case SMT_STATION_ACTION_DISCONNECT :
1998                         queue_event(smc,EVENT_ECM,EC_DISCONNECT) ;
1999                         smc->mib.fddiSMTRemoteDisconnectFlag = TRUE ;
2000                         RS_SET(smc,RS_DISCONNECT) ;
2001                         AIX_EVENT(smc, (u_long) FDDI_RING_STATUS, (u_long)
2002                                 FDDI_SMT_EVENT, (u_long) FDDI_REMOTE_DISCONNECT,
2003                                 smt_get_event_word(smc));
2004                         break ;
2005                 case SMT_STATION_ACTION_PATHTEST :
2006                         AIX_EVENT(smc, (u_long) FDDI_RING_STATUS, (u_long)
2007                                 FDDI_SMT_EVENT, (u_long) FDDI_PATH_TEST,
2008                                 smt_get_event_word(smc));
2009                         break ;
2010                 case SMT_STATION_ACTION_SELFTEST :
2011                         AIX_EVENT(smc, (u_long) FDDI_RING_STATUS, (u_long)
2012                                 FDDI_SMT_EVENT, (u_long) FDDI_REMOTE_SELF_TEST,
2013                                 smt_get_event_word(smc));
2014                         break ;
2015                 case SMT_STATION_ACTION_DISABLE_A :
2016                         if (smc->y[PA].pc_mode == PM_PEER) {
2017                                 RS_SET(smc,RS_EVENT) ;
2018                                 queue_event(smc,EVENT_PCM+PA,PC_DISABLE) ;
2019                         }
2020                         break ;
2021                 case SMT_STATION_ACTION_DISABLE_B :
2022                         if (smc->y[PB].pc_mode == PM_PEER) {
2023                                 RS_SET(smc,RS_EVENT) ;
2024                                 queue_event(smc,EVENT_PCM+PB,PC_DISABLE) ;
2025                         }
2026                         break ;
2027                 case SMT_STATION_ACTION_DISABLE_M :
2028                         for (port = 0 ; port <  NUMPHYS ; port++) {
2029                                 if (smc->mib.p[port].fddiPORTMy_Type != TM)
2030                                         continue ;
2031                                 RS_SET(smc,RS_EVENT) ;
2032                                 queue_event(smc,EVENT_PCM+port,PC_DISABLE) ;
2033                         }
2034                         break ;
2035                 default :
2036                         return(1) ;
2037                 }
2038                 break ;
2039         case SMT_PORT_ACTION :
2040                 switch(code) {
2041                 case SMT_PORT_ACTION_ENABLE :
2042                         event = PC_ENABLE ;
2043                         break ;
2044                 case SMT_PORT_ACTION_DISABLE :
2045                         event = PC_DISABLE ;
2046                         break ;
2047                 case SMT_PORT_ACTION_MAINT :
2048                         event = PC_MAINT ;
2049                         break ;
2050                 case SMT_PORT_ACTION_START :
2051                         event = PC_START ;
2052                         break ;
2053                 case SMT_PORT_ACTION_STOP :
2054                         event = PC_STOP ;
2055                         break ;
2056                 default :
2057                         return(1) ;
2058                 }
2059                 queue_event(smc,EVENT_PCM+index,event) ;
2060                 break ;
2061         default :
2062                 return(1) ;
2063         }
2064         return(0) ;
2065 }
2066
2067 /*
2068  * change tneg
2069  *      set T_Req in MIB (Path Attribute)
2070  *      calculate new values for MAC
2071  *      if change required
2072  *              disconnect
2073  *              set reconnect
2074  *      end
2075  */
2076 void smt_change_t_neg(struct s_smc *smc, u_long tneg)
2077 {
2078         smc->mib.a[PATH0].fddiPATHMaxT_Req = tneg ;
2079
2080         if (smt_set_mac_opvalues(smc)) {
2081                 RS_SET(smc,RS_EVENT) ;
2082                 smc->sm.please_reconnect = 1 ;
2083                 queue_event(smc,EVENT_ECM,EC_DISCONNECT) ;
2084         }
2085 }
2086
2087 /*
2088  * canonical conversion of <len> bytes beginning form *data
2089  */
2090 #ifdef  USE_CAN_ADDR
2091 void hwm_conv_can(struct s_smc *smc, char *data, int len)
2092 {
2093         int i ;
2094
2095         SK_UNUSED(smc) ;
2096
2097         for (i = len; i ; i--, data++) {
2098                 *data = canonical[*(u_char *)data] ;
2099         }
2100 }
2101 #endif
2102
2103 #endif  /* no SLIM_SMT */
2104