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