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