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
27static 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
38static 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
45static 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
52static const struct fddi_addr SMT_Unknown = {
53	{ 0,0,0x1f,0,0,0 }
54} ;
55
56/*
57 * external variables
58 */
59extern const struct fddi_addr fddi_broadcast ;
60
61/*
62 * external functions
63 */
64int pcm_status_twisted(struct s_smc *smc);
65
66/*
67 * function prototypes
68 */
69#ifdef	LITTLE_ENDIAN
70static int smt_swap_short(u_short s);
71#endif
72static int mac_index(struct s_smc *smc, int mac);
73static int phy_index(struct s_smc *smc, int phy);
74static int mac_con_resource_index(struct s_smc *smc, int mac);
75static int phy_con_resource_index(struct s_smc *smc, int phy);
76static void smt_send_rdf(struct s_smc *smc, SMbuf *rej, int fc, int reason,
77			 int local);
78static void smt_send_nif(struct s_smc *smc, const struct fddi_addr *dest,
79			 int fc, u_long tid, int type, int local);
80static void smt_send_ecf(struct s_smc *smc, struct fddi_addr *dest, int fc,
81                         u_long tid, int type, int len);
82static void smt_echo_test(struct s_smc *smc, int dna);
83static void smt_send_sif_config(struct s_smc *smc, struct fddi_addr *dest,
84				u_long tid, int local);
85static void smt_send_sif_operation(struct s_smc *smc, struct fddi_addr *dest,
86				   u_long tid, int local);
87#ifdef LITTLE_ENDIAN
88static void smt_string_swap(char *data, const char *format, int len);
89#endif
90static void smt_add_frame_len(SMbuf *mb, int len);
91static void smt_fill_una(struct s_smc *smc, struct smt_p_una *una);
92static void smt_fill_sde(struct s_smc *smc, struct smt_p_sde *sde);
93static void smt_fill_state(struct s_smc *smc, struct smt_p_state *state);
94static void smt_fill_timestamp(struct s_smc *smc, struct smt_p_timestamp *ts);
95static void smt_fill_policy(struct s_smc *smc, struct smt_p_policy *policy);
96static void smt_fill_latency(struct s_smc *smc, struct smt_p_latency *latency);
97static void smt_fill_neighbor(struct s_smc *smc, struct smt_p_neighbor *neighbor);
98static int smt_fill_path(struct s_smc *smc, struct smt_p_path *path);
99static void smt_fill_mac_status(struct s_smc *smc, struct smt_p_mac_status *st);
100static void smt_fill_lem(struct s_smc *smc, struct smt_p_lem *lem, int phy);
101static void smt_fill_version(struct s_smc *smc, struct smt_p_version *vers);
102static void smt_fill_fsc(struct s_smc *smc, struct smt_p_fsc *fsc);
103static void smt_fill_mac_counter(struct s_smc *smc, struct smt_p_mac_counter *mc);
104static void smt_fill_mac_fnc(struct s_smc *smc, struct smt_p_mac_fnc *fnc);
105static void smt_fill_manufacturer(struct s_smc *smc,
106				  struct smp_p_manufacturer *man);
107static void smt_fill_user(struct s_smc *smc, struct smp_p_user *user);
108static void smt_fill_setcount(struct s_smc *smc, struct smt_p_setcount *setcount);
109static void smt_fill_echo(struct s_smc *smc, struct smt_p_echo *echo, u_long seed,
110			  int len);
111
112static void smt_clear_una_dna(struct s_smc *smc);
113static void smt_clear_old_una_dna(struct s_smc *smc);
114#ifdef	CONCENTRATOR
115static int entity_to_index(void);
116#endif
117static void update_dac(struct s_smc *smc, int report);
118static int div_ratio(u_long upper, u_long lower);
119#ifdef  USE_CAN_ADDR
120static 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
126static 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
137static 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
144static inline int is_individual(const struct fddi_addr *addr)
145{
146	return(!(addr->a[0] & GROUP_ADDR)) ;
147}
148
149static 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 */
160static const u_short plist_nif[] = { SMT_P_UNA,SMT_P_SDE,SMT_P_STATE,0 } ;
161
162/*
163 * init SMT agent
164 */
165void 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 */
211void 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
219void 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*/
246void 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
462static 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 */
478void 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
508	smt_swap_para(sm,(int) mb->sm_len,1) ;
509	DB_SMT("SMT : received packet [%s] at 0x%x\n",
510		smt_type_name[m_fc(mb) & 0xf],sm) ;
511	DB_SMT("SMT : version %d, class %s\n",sm->smt_version,
512		smt_class_name[(sm->smt_class>LAST_CLASS)?0 : sm->smt_class]) ;
513
514#ifdef	SBA
515	/*
516	 * check if NSA frame
517	 */
518	if (m_fc(mb) == FC_SMT_NSA && sm->smt_class == SMT_NIF &&
519		(sm->smt_type == SMT_ANNOUNCE || sm->smt_type == SMT_REQUEST)) {
520			smc->sba.sm = sm ;
521			sba(smc,NIF) ;
522	}
523#endif
524
525	/*
526	 * ignore any packet with NSA and A-indicator set
527	 */
528	if ( (fs & A_INDICATOR) && m_fc(mb) == FC_SMT_NSA) {
529		DB_SMT("SMT : ignoring NSA with A-indicator set from %s\n",
530			addr_to_string(&sm->smt_source),0) ;
531		smt_free_mbuf(smc,mb) ;
532		return ;
533	}
534
535	/*
536	 * ignore frames with illegal length
537	 */
538	if (((sm->smt_class == SMT_ECF) && (sm->smt_len > SMT_MAX_ECHO_LEN)) ||
539	    ((sm->smt_class != SMT_ECF) && (sm->smt_len > SMT_MAX_INFO_LEN))) {
540		smt_free_mbuf(smc,mb) ;
541		return ;
542	}
543
544	/*
545	 * check SMT version
546	 */
547	switch (sm->smt_class) {
548	case SMT_NIF :
549	case SMT_SIF_CONFIG :
550	case SMT_SIF_OPER :
551	case SMT_ECF :
552		if (sm->smt_version != SMT_VID)
553			illegal = 1;
554		break ;
555	default :
556		if (sm->smt_version != SMT_VID_2)
557			illegal = 1;
558		break ;
559	}
560	if (illegal) {
561		DB_SMT("SMT : version = %d, dest = %s\n",
562			sm->smt_version,addr_to_string(&sm->smt_source)) ;
563		smt_send_rdf(smc,mb,m_fc(mb),SMT_RDF_VERSION,local) ;
564		smt_free_mbuf(smc,mb) ;
565		return ;
566	}
567	if ((sm->smt_len > mb->sm_len - sizeof(struct smt_header)) ||
568	    ((sm->smt_len & 3) && (sm->smt_class != SMT_ECF))) {
569		DB_SMT("SMT: info length error, len = %d\n",sm->smt_len,0) ;
570		smt_send_rdf(smc,mb,m_fc(mb),SMT_RDF_LENGTH,local) ;
571		smt_free_mbuf(smc,mb) ;
572		return ;
573	}
574	switch (sm->smt_class) {
575	case SMT_NIF :
576		if (smt_check_para(smc,sm,plist_nif)) {
577			DB_SMT("SMT: NIF with para problem, ignoring\n",0,0) ;
578			break ;
579		} ;
580		switch (sm->smt_type) {
581		case SMT_ANNOUNCE :
582		case SMT_REQUEST :
583			if (!(fs & C_INDICATOR) && m_fc(mb) == FC_SMT_NSA
584				&& is_broadcast(&sm->smt_dest)) {
585				struct smt_p_state	*st ;
586
587				/* set my UNA */
588				if (!is_equal(
589					&smc->mib.m[MAC0].fddiMACUpstreamNbr,
590					&sm->smt_source)) {
591					DB_SMT("SMT : updated my UNA = %s\n",
592					addr_to_string(&sm->smt_source),0) ;
593					if (!is_equal(&smc->mib.m[MAC0].
594					    fddiMACUpstreamNbr,&SMT_Unknown)){
595					 /* Do not update unknown address */
596					 smc->mib.m[MAC0].fddiMACOldUpstreamNbr=
597					 smc->mib.m[MAC0].fddiMACUpstreamNbr ;
598					}
599
600					smc->mib.m[MAC0].fddiMACUpstreamNbr =
601						sm->smt_source ;
602					smt_srf_event(smc,
603						SMT_EVENT_MAC_NEIGHBOR_CHANGE,
604						INDEX_MAC,0) ;
605					smt_echo_test(smc,0) ;
606				}
607				smc->sm.smt_tvu = smt_get_time() ;
608				st = (struct smt_p_state *)
609					sm_to_para(smc,sm,SMT_P_STATE) ;
610				if (st) {
611					smc->mib.m[MAC0].fddiMACUNDA_Flag =
612					(st->st_dupl_addr & SMT_ST_MY_DUPA) ?
613					TRUE : FALSE ;
614					update_dac(smc,1) ;
615				}
616			}
617			if ((sm->smt_type == SMT_REQUEST) &&
618			    is_individual(&sm->smt_source) &&
619			    ((!(fs & A_INDICATOR) && m_fc(mb) == FC_SMT_NSA) ||
620			     (m_fc(mb) != FC_SMT_NSA))) {
621				DB_SMT("SMT : replying to NIF request %s\n",
622					addr_to_string(&sm->smt_source),0) ;
623				smt_send_nif(smc,&sm->smt_source,
624					FC_SMT_INFO,
625					sm->smt_tid,
626					SMT_REPLY,local) ;
627			}
628			break ;
629		case SMT_REPLY :
630			DB_SMT("SMT : received NIF response from %s\n",
631				addr_to_string(&sm->smt_source),0) ;
632			if (fs & A_INDICATOR) {
633				smc->sm.pend[SMT_TID_NIF] = 0 ;
634				DB_SMT("SMT : duplicate address\n",0,0) ;
635				smc->mib.m[MAC0].fddiMACDupAddressTest =
636					DA_FAILED ;
637				smc->r.dup_addr_test = DA_FAILED ;
638				queue_event(smc,EVENT_RMT,RM_DUP_ADDR) ;
639				smc->mib.m[MAC0].fddiMACDA_Flag = TRUE ;
640				update_dac(smc,1) ;
641				break ;
642			}
643			if (sm->smt_tid == smc->sm.pend[SMT_TID_NIF]) {
644				smc->sm.pend[SMT_TID_NIF] = 0 ;
645				/* set my DNA */
646				if (!is_equal(
647					&smc->mib.m[MAC0].fddiMACDownstreamNbr,
648					&sm->smt_source)) {
649					DB_SMT("SMT : updated my DNA\n",0,0) ;
650					if (!is_equal(&smc->mib.m[MAC0].
651					 fddiMACDownstreamNbr, &SMT_Unknown)){
652					 /* Do not update unknown address */
653				smc->mib.m[MAC0].fddiMACOldDownstreamNbr =
654					 smc->mib.m[MAC0].fddiMACDownstreamNbr ;
655					}
656
657					smc->mib.m[MAC0].fddiMACDownstreamNbr =
658						sm->smt_source ;
659					smt_srf_event(smc,
660						SMT_EVENT_MAC_NEIGHBOR_CHANGE,
661						INDEX_MAC,0) ;
662					smt_echo_test(smc,1) ;
663				}
664				smc->mib.m[MAC0].fddiMACDA_Flag = FALSE ;
665				update_dac(smc,1) ;
666				smc->sm.smt_tvd = smt_get_time() ;
667				smc->mib.m[MAC0].fddiMACDupAddressTest =
668					DA_PASSED ;
669				if (smc->r.dup_addr_test != DA_PASSED) {
670					smc->r.dup_addr_test = DA_PASSED ;
671					queue_event(smc,EVENT_RMT,RM_DUP_ADDR) ;
672				}
673			}
674			else if (sm->smt_tid ==
675				smc->sm.pend[SMT_TID_NIF_TEST]) {
676				DB_SMT("SMT : NIF test TID ok\n",0,0) ;
677			}
678			else {
679				DB_SMT("SMT : expected TID %lx, got %lx\n",
680				smc->sm.pend[SMT_TID_NIF],sm->smt_tid) ;
681			}
682			break ;
683		default :
684			illegal = 2 ;
685			break ;
686		}
687		break ;
688	case SMT_SIF_CONFIG :	/* station information */
689		if (sm->smt_type != SMT_REQUEST)
690			break ;
691		DB_SMT("SMT : replying to SIF Config request from %s\n",
692			addr_to_string(&sm->smt_source),0) ;
693		smt_send_sif_config(smc,&sm->smt_source,sm->smt_tid,local) ;
694		break ;
695	case SMT_SIF_OPER :	/* station information */
696		if (sm->smt_type != SMT_REQUEST)
697			break ;
698		DB_SMT("SMT : replying to SIF Operation request from %s\n",
699			addr_to_string(&sm->smt_source),0) ;
700		smt_send_sif_operation(smc,&sm->smt_source,sm->smt_tid,local) ;
701		break ;
702	case SMT_ECF :		/* echo frame */
703		switch (sm->smt_type) {
704		case SMT_REPLY :
705			smc->mib.priv.fddiPRIVECF_Reply_Rx++ ;
706			DB_SMT("SMT: received ECF reply from %s\n",
707				addr_to_string(&sm->smt_source),0) ;
708			if (sm_to_para(smc,sm,SMT_P_ECHODATA) == 0) {
709				DB_SMT("SMT: ECHODATA missing\n",0,0) ;
710				break ;
711			}
712			if (sm->smt_tid == smc->sm.pend[SMT_TID_ECF]) {
713				DB_SMT("SMT : ECF test TID ok\n",0,0) ;
714			}
715			else if (sm->smt_tid == smc->sm.pend[SMT_TID_ECF_UNA]) {
716				DB_SMT("SMT : ECF test UNA ok\n",0,0) ;
717			}
718			else if (sm->smt_tid == smc->sm.pend[SMT_TID_ECF_DNA]) {
719				DB_SMT("SMT : ECF test DNA ok\n",0,0) ;
720			}
721			else {
722				DB_SMT("SMT : expected TID %lx, got %lx\n",
723					smc->sm.pend[SMT_TID_ECF],
724					sm->smt_tid) ;
725			}
726			break ;
727		case SMT_REQUEST :
728			smc->mib.priv.fddiPRIVECF_Req_Rx++ ;
729			{
730			if (sm->smt_len && !sm_to_para(smc,sm,SMT_P_ECHODATA)) {
731			DB_SMT("SMT: ECF with para problem,sending RDF\n",0,0) ;
732				smt_send_rdf(smc,mb,m_fc(mb),SMT_RDF_LENGTH,
733					local) ;
734				break ;
735			}
736			DB_SMT("SMT - sending ECF reply to %s\n",
737				addr_to_string(&sm->smt_source),0) ;
738
739			/* set destination addr.  & reply */
740			sm->smt_dest = sm->smt_source ;
741			sm->smt_type = SMT_REPLY ;
742			dump_smt(smc,sm,"ECF REPLY") ;
743			smc->mib.priv.fddiPRIVECF_Reply_Tx++ ;
744			smt_send_frame(smc,mb,FC_SMT_INFO,local) ;
745			return ;		/* DON'T free mbuf */
746			}
747		default :
748			illegal = 1 ;
749			break ;
750		}
751		break ;
752#ifndef	BOOT
753	case SMT_RAF :		/* resource allocation */
754#ifdef	ESS
755		DB_ESSN(2,"ESS: RAF frame received\n",0,0) ;
756		fs = ess_raf_received_pack(smc,mb,sm,fs) ;
757#endif
758
759#ifdef	SBA
760		DB_SBAN(2,"SBA: RAF frame received\n",0,0) ;
761		sba_raf_received_pack(smc,sm,fs) ;
762#endif
763		break ;
764	case SMT_RDF :		/* request denied */
765		smc->mib.priv.fddiPRIVRDF_Rx++ ;
766		break ;
767	case SMT_ESF :		/* extended service - not supported */
768		if (sm->smt_type == SMT_REQUEST) {
769			DB_SMT("SMT - received ESF, sending RDF\n",0,0) ;
770			smt_send_rdf(smc,mb,m_fc(mb),SMT_RDF_CLASS,local) ;
771		}
772		break ;
773	case SMT_PMF_GET :
774	case SMT_PMF_SET :
775		if (sm->smt_type != SMT_REQUEST)
776			break ;
777		/* update statistics */
778		if (sm->smt_class == SMT_PMF_GET)
779			smc->mib.priv.fddiPRIVPMF_Get_Rx++ ;
780		else
781			smc->mib.priv.fddiPRIVPMF_Set_Rx++ ;
782		/*
783		 * ignore PMF SET with I/G set
784		 */
785		if ((sm->smt_class == SMT_PMF_SET) &&
786			!is_individual(&sm->smt_dest)) {
787			DB_SMT("SMT: ignoring PMF-SET with I/G set\n",0,0) ;
788			break ;
789		}
790		smt_pmf_received_pack(smc,mb, local) ;
791		break ;
792	case SMT_SRF :
793		dump_smt(smc,sm,"SRF received") ;
794		break ;
795	default :
796		if (sm->smt_type != SMT_REQUEST)
797			break ;
798		/*
799		 * For frames with unknown class:
800		 * we need to send a RDF frame according to 8.1.3.1.1,
801		 * only if it is a REQUEST.
802		 */
803		DB_SMT("SMT : class = %d, send RDF to %s\n",
804			sm->smt_class, addr_to_string(&sm->smt_source)) ;
805
806		smt_send_rdf(smc,mb,m_fc(mb),SMT_RDF_CLASS,local) ;
807		break ;
808#endif
809	}
810	if (illegal) {
811		DB_SMT("SMT: discarding invalid frame, reason = %d\n",
812			illegal,0) ;
813	}
814	smt_free_mbuf(smc,mb) ;
815}
816
817static void update_dac(struct s_smc *smc, int report)
818{
819	int	cond ;
820
821	cond = ( smc->mib.m[MAC0].fddiMACUNDA_Flag |
822		smc->mib.m[MAC0].fddiMACDA_Flag) != 0 ;
823	if (report && (cond != smc->mib.m[MAC0].fddiMACDuplicateAddressCond))
824		smt_srf_event(smc, SMT_COND_MAC_DUP_ADDR,INDEX_MAC,cond) ;
825	else
826		smc->mib.m[MAC0].fddiMACDuplicateAddressCond = cond ;
827}
828
829/*
830 * send SMT frame
831 *	set source address
832 *	set station ID
833 *	send frame
834 */
835void smt_send_frame(struct s_smc *smc, SMbuf *mb, int fc, int local)
836/* SMbuf *mb;	buffer to send */
837/* int fc;	FC value */
838{
839	struct smt_header	*sm ;
840
841	if (!smc->r.sm_ma_avail && !local) {
842		smt_free_mbuf(smc,mb) ;
843		return ;
844	}
845	sm = smtod(mb,struct smt_header *) ;
846	sm->smt_source = smc->mib.m[MAC0].fddiMACSMTAddress ;
847	sm->smt_sid = smc->mib.fddiSMTStationId ;
848
849	smt_swap_para(sm,(int) mb->sm_len,0) ;		/* swap para & header */
850	hwm_conv_can(smc,(char *)sm,12) ;		/* convert SA and DA */
851	smc->mib.m[MAC0].fddiMACSMTTransmit_Ct++ ;
852	smt_send_mbuf(smc,mb,local ? FC_SMT_LOC : fc) ;
853}
854
855/*
856 * generate and send RDF
857 */
858static void smt_send_rdf(struct s_smc *smc, SMbuf *rej, int fc, int reason,
859			 int local)
860/* SMbuf *rej;	mbuf of offending frame */
861/* int fc;	FC of denied frame */
862/* int reason;	reason code */
863{
864	SMbuf	*mb ;
865	struct smt_header	*sm ;	/* header of offending frame */
866	struct smt_rdf	*rdf ;
867	int		len ;
868	int		frame_len ;
869
870	sm = smtod(rej,struct smt_header *) ;
871	if (sm->smt_type != SMT_REQUEST)
872		return ;
873
874	DB_SMT("SMT: sending RDF to %s,reason = 0x%x\n",
875		addr_to_string(&sm->smt_source),reason) ;
876
877
878	/*
879	 * note: get framelength from MAC length, NOT from SMT header
880	 * smt header length is included in sm_len
881	 */
882	frame_len = rej->sm_len ;
883
884	if (!(mb=smt_build_frame(smc,SMT_RDF,SMT_REPLY,sizeof(struct smt_rdf))))
885		return ;
886	rdf = smtod(mb,struct smt_rdf *) ;
887	rdf->smt.smt_tid = sm->smt_tid ;		/* use TID from sm */
888	rdf->smt.smt_dest = sm->smt_source ;		/* set dest = source */
889
890	/* set P12 */
891	rdf->reason.para.p_type = SMT_P_REASON ;
892	rdf->reason.para.p_len = sizeof(struct smt_p_reason) - PARA_LEN ;
893	rdf->reason.rdf_reason = reason ;
894
895	/* set P14 */
896	rdf->version.para.p_type = SMT_P_VERSION ;
897	rdf->version.para.p_len = sizeof(struct smt_p_version) - PARA_LEN ;
898	rdf->version.v_pad = 0 ;
899	rdf->version.v_n = 1 ;
900	rdf->version.v_index = 1 ;
901	rdf->version.v_version[0] = SMT_VID_2 ;
902	rdf->version.v_pad2 = 0 ;
903
904	/* set P13 */
905	if ((unsigned) frame_len <= SMT_MAX_INFO_LEN - sizeof(*rdf) +
906		2*sizeof(struct smt_header))
907		len = frame_len ;
908	else
909		len = SMT_MAX_INFO_LEN - sizeof(*rdf) +
910			2*sizeof(struct smt_header) ;
911	/* make length multiple of 4 */
912	len &= ~3 ;
913	rdf->refused.para.p_type = SMT_P_REFUSED ;
914	/* length of para is smt_frame + ref_fc */
915	rdf->refused.para.p_len = len + 4 ;
916	rdf->refused.ref_fc = fc ;
917
918	/* swap it back */
919	smt_swap_para(sm,frame_len,0) ;
920
921	memcpy((char *) &rdf->refused.ref_header,(char *) sm,len) ;
922
923	len -= sizeof(struct smt_header) ;
924	mb->sm_len += len ;
925	rdf->smt.smt_len += len ;
926
927	dump_smt(smc,(struct smt_header *)rdf,"RDF") ;
928	smc->mib.priv.fddiPRIVRDF_Tx++ ;
929	smt_send_frame(smc,mb,FC_SMT_INFO,local) ;
930}
931
932/*
933 * generate and send NIF
934 */
935static void smt_send_nif(struct s_smc *smc, const struct fddi_addr *dest,
936			 int fc, u_long tid, int type, int local)
937/* struct fddi_addr *dest;	dest address */
938/* int fc;			frame control */
939/* u_long tid;			transaction id */
940/* int type;			frame type */
941{
942	struct smt_nif	*nif ;
943	SMbuf		*mb ;
944
945	if (!(mb = smt_build_frame(smc,SMT_NIF,type,sizeof(struct smt_nif))))
946		return ;
947	nif = smtod(mb, struct smt_nif *) ;
948	smt_fill_una(smc,&nif->una) ;	/* set UNA */
949	smt_fill_sde(smc,&nif->sde) ;	/* set station descriptor */
950	smt_fill_state(smc,&nif->state) ;	/* set state information */
951#ifdef	SMT6_10
952	smt_fill_fsc(smc,&nif->fsc) ;	/* set frame status cap. */
953#endif
954	nif->smt.smt_dest = *dest ;	/* destination address */
955	nif->smt.smt_tid = tid ;	/* transaction ID */
956	dump_smt(smc,(struct smt_header *)nif,"NIF") ;
957	smt_send_frame(smc,mb,fc,local) ;
958}
959
960#ifdef	DEBUG
961/*
962 * send NIF request (test purpose)
963 */
964static void smt_send_nif_request(struct s_smc *smc, struct fddi_addr *dest)
965{
966	smc->sm.pend[SMT_TID_NIF_TEST] = smt_get_tid(smc) ;
967	smt_send_nif(smc,dest, FC_SMT_INFO, smc->sm.pend[SMT_TID_NIF_TEST],
968		SMT_REQUEST,0) ;
969}
970
971/*
972 * send ECF request (test purpose)
973 */
974static void smt_send_ecf_request(struct s_smc *smc, struct fddi_addr *dest,
975				 int len)
976{
977	smc->sm.pend[SMT_TID_ECF] = smt_get_tid(smc) ;
978	smt_send_ecf(smc,dest, FC_SMT_INFO, smc->sm.pend[SMT_TID_ECF],
979		SMT_REQUEST,len) ;
980}
981#endif
982
983/*
984 * echo test
985 */
986static void smt_echo_test(struct s_smc *smc, int dna)
987{
988	u_long	tid ;
989
990	smc->sm.pend[dna ? SMT_TID_ECF_DNA : SMT_TID_ECF_UNA] =
991		tid = smt_get_tid(smc) ;
992	smt_send_ecf(smc, dna ?
993		&smc->mib.m[MAC0].fddiMACDownstreamNbr :
994		&smc->mib.m[MAC0].fddiMACUpstreamNbr,
995		FC_SMT_INFO,tid, SMT_REQUEST, (SMT_TEST_ECHO_LEN & ~3)-8) ;
996}
997
998/*
999 * generate and send ECF
1000 */
1001static void smt_send_ecf(struct s_smc *smc, struct fddi_addr *dest, int fc,
1002			 u_long tid, int type, int len)
1003/* struct fddi_addr *dest;	dest address */
1004/* int fc;			frame control */
1005/* u_long tid;			transaction id */
1006/* int type;			frame type */
1007/* int len;			frame length */
1008{
1009	struct smt_ecf	*ecf ;
1010	SMbuf		*mb ;
1011
1012	if (!(mb = smt_build_frame(smc,SMT_ECF,type,SMT_ECF_LEN + len)))
1013		return ;
1014	ecf = smtod(mb, struct smt_ecf *) ;
1015
1016	smt_fill_echo(smc,&ecf->ec_echo,tid,len) ;	/* set ECHO */
1017	ecf->smt.smt_dest = *dest ;	/* destination address */
1018	ecf->smt.smt_tid = tid ;	/* transaction ID */
1019	smc->mib.priv.fddiPRIVECF_Req_Tx++ ;
1020	smt_send_frame(smc,mb,fc,0) ;
1021}
1022
1023/*
1024 * generate and send SIF config response
1025 */
1026
1027static void smt_send_sif_config(struct s_smc *smc, struct fddi_addr *dest,
1028				u_long tid, int local)
1029/* struct fddi_addr *dest;	dest address */
1030/* u_long tid;			transaction id */
1031{
1032	struct smt_sif_config	*sif ;
1033	SMbuf			*mb ;
1034	int			len ;
1035	if (!(mb = smt_build_frame(smc,SMT_SIF_CONFIG,SMT_REPLY,
1036		SIZEOF_SMT_SIF_CONFIG)))
1037		return ;
1038
1039	sif = smtod(mb, struct smt_sif_config *) ;
1040	smt_fill_timestamp(smc,&sif->ts) ;	/* set time stamp */
1041	smt_fill_sde(smc,&sif->sde) ;		/* set station descriptor */
1042	smt_fill_version(smc,&sif->version) ;	/* set version information */
1043	smt_fill_state(smc,&sif->state) ;	/* set state information */
1044	smt_fill_policy(smc,&sif->policy) ;	/* set station policy */
1045	smt_fill_latency(smc,&sif->latency);	/* set station latency */
1046	smt_fill_neighbor(smc,&sif->neighbor);	/* set station neighbor */
1047	smt_fill_setcount(smc,&sif->setcount) ;	/* set count */
1048	len = smt_fill_path(smc,&sif->path);	/* set station path descriptor*/
1049	sif->smt.smt_dest = *dest ;		/* destination address */
1050	sif->smt.smt_tid = tid ;		/* transaction ID */
1051	smt_add_frame_len(mb,len) ;		/* adjust length fields */
1052	dump_smt(smc,(struct smt_header *)sif,"SIF Configuration Reply") ;
1053	smt_send_frame(smc,mb,FC_SMT_INFO,local) ;
1054}
1055
1056/*
1057 * generate and send SIF operation response
1058 */
1059
1060static void smt_send_sif_operation(struct s_smc *smc, struct fddi_addr *dest,
1061				   u_long tid, int local)
1062/* struct fddi_addr *dest;	dest address */
1063/* u_long tid;			transaction id */
1064{
1065	struct smt_sif_operation *sif ;
1066	SMbuf			*mb ;
1067	int			ports ;
1068	int			i ;
1069
1070	ports = NUMPHYS ;
1071#ifndef	CONCENTRATOR
1072	if (smc->s.sas == SMT_SAS)
1073		ports = 1 ;
1074#endif
1075
1076	if (!(mb = smt_build_frame(smc,SMT_SIF_OPER,SMT_REPLY,
1077		SIZEOF_SMT_SIF_OPERATION+ports*sizeof(struct smt_p_lem))))
1078		return ;
1079	sif = smtod(mb, struct smt_sif_operation *) ;
1080	smt_fill_timestamp(smc,&sif->ts) ;	/* set time stamp */
1081	smt_fill_mac_status(smc,&sif->status) ; /* set mac status */
1082	smt_fill_mac_counter(smc,&sif->mc) ; /* set mac counter field */
1083	smt_fill_mac_fnc(smc,&sif->fnc) ; /* set frame not copied counter */
1084	smt_fill_manufacturer(smc,&sif->man) ; /* set manufacturer field */
1085	smt_fill_user(smc,&sif->user) ;		/* set user field */
1086	smt_fill_setcount(smc,&sif->setcount) ;	/* set count */
1087	/*
1088	 * set link error mon information
1089	 */
1090	if (ports == 1) {
1091		smt_fill_lem(smc,sif->lem,PS) ;
1092	}
1093	else {
1094		for (i = 0 ; i < ports ; i++) {
1095			smt_fill_lem(smc,&sif->lem[i],i) ;
1096		}
1097	}
1098
1099	sif->smt.smt_dest = *dest ;	/* destination address */
1100	sif->smt.smt_tid = tid ;	/* transaction ID */
1101	dump_smt(smc,(struct smt_header *)sif,"SIF Operation Reply") ;
1102	smt_send_frame(smc,mb,FC_SMT_INFO,local) ;
1103}
1104
1105/*
1106 * get and initialize SMT frame
1107 */
1108SMbuf *smt_build_frame(struct s_smc *smc, int class, int type,
1109				  int length)
1110{
1111	SMbuf			*mb ;
1112	struct smt_header	*smt ;
1113
1114	if (!(mb = smt_get_mbuf(smc)))
1115		return(mb) ;
1116
1117	mb->sm_len = length ;
1118	smt = smtod(mb, struct smt_header *) ;
1119	smt->smt_dest = fddi_broadcast ; /* set dest = broadcast */
1120	smt->smt_class = class ;
1121	smt->smt_type = type ;
1122	switch (class) {
1123	case SMT_NIF :
1124	case SMT_SIF_CONFIG :
1125	case SMT_SIF_OPER :
1126	case SMT_ECF :
1127		smt->smt_version = SMT_VID ;
1128		break ;
1129	default :
1130		smt->smt_version = SMT_VID_2 ;
1131		break ;
1132	}
1133	smt->smt_tid = smt_get_tid(smc) ;	/* set transaction ID */
1134	smt->smt_pad = 0 ;
1135	smt->smt_len = length - sizeof(struct smt_header) ;
1136	return(mb) ;
1137}
1138
1139static void smt_add_frame_len(SMbuf *mb, int len)
1140{
1141	struct smt_header	*smt ;
1142
1143	smt = smtod(mb, struct smt_header *) ;
1144	smt->smt_len += len ;
1145	mb->sm_len += len ;
1146}
1147
1148
1149
1150/*
1151 * fill values in UNA parameter
1152 */
1153static void smt_fill_una(struct s_smc *smc, struct smt_p_una *una)
1154{
1155	SMTSETPARA(una,SMT_P_UNA) ;
1156	una->una_pad = 0 ;
1157	una->una_node = smc->mib.m[MAC0].fddiMACUpstreamNbr ;
1158}
1159
1160/*
1161 * fill values in SDE parameter
1162 */
1163static void smt_fill_sde(struct s_smc *smc, struct smt_p_sde *sde)
1164{
1165	SMTSETPARA(sde,SMT_P_SDE) ;
1166	sde->sde_non_master = smc->mib.fddiSMTNonMaster_Ct ;
1167	sde->sde_master = smc->mib.fddiSMTMaster_Ct ;
1168	sde->sde_mac_count = NUMMACS ;		/* only 1 MAC */
1169#ifdef	CONCENTRATOR
1170	sde->sde_type = SMT_SDE_CONCENTRATOR ;
1171#else
1172	sde->sde_type = SMT_SDE_STATION ;
1173#endif
1174}
1175
1176/*
1177 * fill in values in station state parameter
1178 */
1179static void smt_fill_state(struct s_smc *smc, struct smt_p_state *state)
1180{
1181	int	top ;
1182	int	twist ;
1183
1184	SMTSETPARA(state,SMT_P_STATE) ;
1185	state->st_pad = 0 ;
1186
1187	/* determine topology */
1188	top = 0 ;
1189	if (smc->mib.fddiSMTPeerWrapFlag) {
1190		top |= SMT_ST_WRAPPED ;		/* state wrapped */
1191	}
1192#ifdef	CONCENTRATOR
1193	if (cfm_status_unattached(smc)) {
1194		top |= SMT_ST_UNATTACHED ;	/* unattached concentrator */
1195	}
1196#endif
1197	if ((twist = pcm_status_twisted(smc)) & 1) {
1198		top |= SMT_ST_TWISTED_A ;	/* twisted cable */
1199	}
1200	if (twist & 2) {
1201		top |= SMT_ST_TWISTED_B ;	/* twisted cable */
1202	}
1203#ifdef	OPT_SRF
1204	top |= SMT_ST_SRF ;
1205#endif
1206	if (pcm_rooted_station(smc))
1207		top |= SMT_ST_ROOTED_S ;
1208	if (smc->mib.a[0].fddiPATHSbaPayload != 0)
1209		top |= SMT_ST_SYNC_SERVICE ;
1210	state->st_topology = top ;
1211	state->st_dupl_addr =
1212		((smc->mib.m[MAC0].fddiMACDA_Flag ? SMT_ST_MY_DUPA : 0 ) |
1213		 (smc->mib.m[MAC0].fddiMACUNDA_Flag ? SMT_ST_UNA_DUPA : 0)) ;
1214}
1215
1216/*
1217 * fill values in timestamp parameter
1218 */
1219static void smt_fill_timestamp(struct s_smc *smc, struct smt_p_timestamp *ts)
1220{
1221
1222	SMTSETPARA(ts,SMT_P_TIMESTAMP) ;
1223	smt_set_timestamp(smc,ts->ts_time) ;
1224}
1225
1226void smt_set_timestamp(struct s_smc *smc, u_char *p)
1227{
1228	u_long	time ;
1229	u_long	utime ;
1230
1231	/*
1232	 * timestamp is 64 bits long ; resolution is 80 nS
1233	 * our clock resolution is 10mS
1234	 * 10mS/80ns = 125000 ~ 2^17 = 131072
1235	 */
1236	utime = smt_get_time() ;
1237	time = utime * 100 ;
1238	time /= TICKS_PER_SECOND ;
1239	p[0] = 0 ;
1240	p[1] = (u_char)((time>>(8+8+8+8-1)) & 1) ;
1241	p[2] = (u_char)(time>>(8+8+8-1)) ;
1242	p[3] = (u_char)(time>>(8+8-1)) ;
1243	p[4] = (u_char)(time>>(8-1)) ;
1244	p[5] = (u_char)(time<<1) ;
1245	p[6] = (u_char)(smc->sm.uniq_ticks>>8) ;
1246	p[7] = (u_char)smc->sm.uniq_ticks ;
1247	/*
1248	 * make sure we don't wrap: restart whenever the upper digits change
1249	 */
1250	if (utime != smc->sm.uniq_time) {
1251		smc->sm.uniq_ticks = 0 ;
1252	}
1253	smc->sm.uniq_ticks++ ;
1254	smc->sm.uniq_time = utime ;
1255}
1256
1257/*
1258 * fill values in station policy parameter
1259 */
1260static void smt_fill_policy(struct s_smc *smc, struct smt_p_policy *policy)
1261{
1262	int	i ;
1263	u_char	*map ;
1264	u_short	in ;
1265	u_short	out ;
1266
1267	/*
1268	 * MIB para 101b (fddiSMTConnectionPolicy) coding
1269	 * is different from 0005 coding
1270	 */
1271	static u_char	ansi_weirdness[16] = {
1272		0,7,5,3,8,1,6,4,9,10,2,11,12,13,14,15
1273	} ;
1274	SMTSETPARA(policy,SMT_P_POLICY) ;
1275
1276	out = 0 ;
1277	in = smc->mib.fddiSMTConnectionPolicy ;
1278	for (i = 0, map = ansi_weirdness ; i < 16 ; i++) {
1279		if (in & 1)
1280			out |= (1<<*map) ;
1281		in >>= 1 ;
1282		map++ ;
1283	}
1284	policy->pl_config = smc->mib.fddiSMTConfigPolicy ;
1285	policy->pl_connect = out ;
1286}
1287
1288/*
1289 * fill values in latency equivalent parameter
1290 */
1291static void smt_fill_latency(struct s_smc *smc, struct smt_p_latency *latency)
1292{
1293	SMTSETPARA(latency,SMT_P_LATENCY) ;
1294
1295	latency->lt_phyout_idx1 = phy_index(smc,0) ;
1296	latency->lt_latency1 = 10 ;	/* in octets (byte clock) */
1297	/*
1298	 * note: latency has two phy entries by definition
1299	 * for a SAS, the 2nd one is null
1300	 */
1301	if (smc->s.sas == SMT_DAS) {
1302		latency->lt_phyout_idx2 = phy_index(smc,1) ;
1303		latency->lt_latency2 = 10 ;	/* in octets (byte clock) */
1304	}
1305	else {
1306		latency->lt_phyout_idx2 = 0 ;
1307		latency->lt_latency2 = 0 ;
1308	}
1309}
1310
1311/*
1312 * fill values in MAC neighbors parameter
1313 */
1314static void smt_fill_neighbor(struct s_smc *smc, struct smt_p_neighbor *neighbor)
1315{
1316	SMTSETPARA(neighbor,SMT_P_NEIGHBORS) ;
1317
1318	neighbor->nb_mib_index = INDEX_MAC ;
1319	neighbor->nb_mac_index = mac_index(smc,1) ;
1320	neighbor->nb_una = smc->mib.m[MAC0].fddiMACUpstreamNbr ;
1321	neighbor->nb_dna = smc->mib.m[MAC0].fddiMACDownstreamNbr ;
1322}
1323
1324/*
1325 * fill values in path descriptor
1326 */
1327#ifdef	CONCENTRATOR
1328#define ALLPHYS	NUMPHYS
1329#else
1330#define ALLPHYS	((smc->s.sas == SMT_SAS) ? 1 : 2)
1331#endif
1332
1333static int smt_fill_path(struct s_smc *smc, struct smt_p_path *path)
1334{
1335	SK_LOC_DECL(int,type) ;
1336	SK_LOC_DECL(int,state) ;
1337	SK_LOC_DECL(int,remote) ;
1338	SK_LOC_DECL(int,mac) ;
1339	int	len ;
1340	int	p ;
1341	int	physp ;
1342	struct smt_phy_rec	*phy ;
1343	struct smt_mac_rec	*pd_mac ;
1344
1345	len =	PARA_LEN +
1346		sizeof(struct smt_mac_rec) * NUMMACS +
1347		sizeof(struct smt_phy_rec) * ALLPHYS ;
1348	path->para.p_type = SMT_P_PATH ;
1349	path->para.p_len = len - PARA_LEN ;
1350
1351	/* PHYs */
1352	for (p = 0,phy = path->pd_phy ; p < ALLPHYS ; p++, phy++) {
1353		physp = p ;
1354#ifndef	CONCENTRATOR
1355		if (smc->s.sas == SMT_SAS)
1356			physp = PS ;
1357#endif
1358		pcm_status_state(smc,physp,&type,&state,&remote,&mac) ;
1359#ifdef	LITTLE_ENDIAN
1360		phy->phy_mib_index = smt_swap_short((u_short)p+INDEX_PORT) ;
1361#else
1362		phy->phy_mib_index = p+INDEX_PORT ;
1363#endif
1364		phy->phy_type = type ;
1365		phy->phy_connect_state = state ;
1366		phy->phy_remote_type = remote ;
1367		phy->phy_remote_mac = mac ;
1368		phy->phy_resource_idx = phy_con_resource_index(smc,p) ;
1369	}
1370
1371	/* MAC */
1372	pd_mac = (struct smt_mac_rec *) phy ;
1373	pd_mac->mac_addr = smc->mib.m[MAC0].fddiMACSMTAddress ;
1374	pd_mac->mac_resource_idx = mac_con_resource_index(smc,1) ;
1375	return(len) ;
1376}
1377
1378/*
1379 * fill values in mac status
1380 */
1381static void smt_fill_mac_status(struct s_smc *smc, struct smt_p_mac_status *st)
1382{
1383	SMTSETPARA(st,SMT_P_MAC_STATUS) ;
1384
1385	st->st_mib_index = INDEX_MAC ;
1386	st->st_mac_index = mac_index(smc,1) ;
1387
1388	mac_update_counter(smc) ;
1389	/*
1390	 * timer values are represented in SMT as 2's complement numbers
1391	 * units :	internal :  2's complement BCLK
1392	 */
1393	st->st_t_req = smc->mib.m[MAC0].fddiMACT_Req ;
1394	st->st_t_neg = smc->mib.m[MAC0].fddiMACT_Neg ;
1395	st->st_t_max = smc->mib.m[MAC0].fddiMACT_Max ;
1396	st->st_tvx_value = smc->mib.m[MAC0].fddiMACTvxValue ;
1397	st->st_t_min = smc->mib.m[MAC0].fddiMACT_Min ;
1398
1399	st->st_sba = smc->mib.a[PATH0].fddiPATHSbaPayload ;
1400	st->st_frame_ct = smc->mib.m[MAC0].fddiMACFrame_Ct ;
1401	st->st_error_ct = smc->mib.m[MAC0].fddiMACError_Ct ;
1402	st->st_lost_ct = smc->mib.m[MAC0].fddiMACLost_Ct ;
1403}
1404
1405/*
1406 * fill values in LEM status
1407 */
1408static void smt_fill_lem(struct s_smc *smc, struct smt_p_lem *lem, int phy)
1409{
1410	struct fddi_mib_p	*mib ;
1411
1412	mib = smc->y[phy].mib ;
1413
1414	SMTSETPARA(lem,SMT_P_LEM) ;
1415	lem->lem_mib_index = phy+INDEX_PORT ;
1416	lem->lem_phy_index = phy_index(smc,phy) ;
1417	lem->lem_pad2 = 0 ;
1418	lem->lem_cutoff = mib->fddiPORTLer_Cutoff ;
1419	lem->lem_alarm = mib->fddiPORTLer_Alarm ;
1420	/* long term bit error rate */
1421	lem->lem_estimate = mib->fddiPORTLer_Estimate ;
1422	/* # of rejected connections */
1423	lem->lem_reject_ct = mib->fddiPORTLem_Reject_Ct ;
1424	lem->lem_ct = mib->fddiPORTLem_Ct ;	/* total number of errors */
1425}
1426
1427/*
1428 * fill version parameter
1429 */
1430static void smt_fill_version(struct s_smc *smc, struct smt_p_version *vers)
1431{
1432	SK_UNUSED(smc) ;
1433	SMTSETPARA(vers,SMT_P_VERSION) ;
1434	vers->v_pad = 0 ;
1435	vers->v_n = 1 ;				/* one version is enough .. */
1436	vers->v_index = 1 ;
1437	vers->v_version[0] = SMT_VID_2 ;
1438	vers->v_pad2 = 0 ;
1439}
1440
1441#ifdef	SMT6_10
1442/*
1443 * fill frame status capabilities
1444 */
1445/*
1446 * note: this para 200B is NOT in swap table, because it's also set in
1447 * PMF add_para
1448 */
1449static void smt_fill_fsc(struct s_smc *smc, struct smt_p_fsc *fsc)
1450{
1451	SK_UNUSED(smc) ;
1452	SMTSETPARA(fsc,SMT_P_FSC) ;
1453	fsc->fsc_pad0 = 0 ;
1454	fsc->fsc_mac_index = INDEX_MAC ;	/* this is MIB ; MIB is NOT
1455						 * mac_index ()i !
1456						 */
1457	fsc->fsc_pad1 = 0 ;
1458	fsc->fsc_value = FSC_TYPE0 ;		/* "normal" node */
1459#ifdef	LITTLE_ENDIAN
1460	fsc->fsc_mac_index = smt_swap_short(INDEX_MAC) ;
1461	fsc->fsc_value = smt_swap_short(FSC_TYPE0) ;
1462#endif
1463}
1464#endif
1465
1466/*
1467 * fill mac counter field
1468 */
1469static void smt_fill_mac_counter(struct s_smc *smc, struct smt_p_mac_counter *mc)
1470{
1471	SMTSETPARA(mc,SMT_P_MAC_COUNTER) ;
1472	mc->mc_mib_index = INDEX_MAC ;
1473	mc->mc_index = mac_index(smc,1) ;
1474	mc->mc_receive_ct = smc->mib.m[MAC0].fddiMACCopied_Ct ;
1475	mc->mc_transmit_ct =  smc->mib.m[MAC0].fddiMACTransmit_Ct ;
1476}
1477
1478/*
1479 * fill mac frame not copied counter
1480 */
1481static void smt_fill_mac_fnc(struct s_smc *smc, struct smt_p_mac_fnc *fnc)
1482{
1483	SMTSETPARA(fnc,SMT_P_MAC_FNC) ;
1484	fnc->nc_mib_index = INDEX_MAC ;
1485	fnc->nc_index = mac_index(smc,1) ;
1486	fnc->nc_counter = smc->mib.m[MAC0].fddiMACNotCopied_Ct ;
1487}
1488
1489
1490/*
1491 * fill manufacturer field
1492 */
1493static void smt_fill_manufacturer(struct s_smc *smc,
1494				  struct smp_p_manufacturer *man)
1495{
1496	SMTSETPARA(man,SMT_P_MANUFACTURER) ;
1497	memcpy((char *) man->mf_data,
1498		(char *) smc->mib.fddiSMTManufacturerData,
1499		sizeof(man->mf_data)) ;
1500}
1501
1502/*
1503 * fill user field
1504 */
1505static void smt_fill_user(struct s_smc *smc, struct smp_p_user *user)
1506{
1507	SMTSETPARA(user,SMT_P_USER) ;
1508	memcpy((char *) user->us_data,
1509		(char *) smc->mib.fddiSMTUserData,
1510		sizeof(user->us_data)) ;
1511}
1512
1513/*
1514 * fill set count
1515 */
1516static void smt_fill_setcount(struct s_smc *smc, struct smt_p_setcount *setcount)
1517{
1518	SK_UNUSED(smc) ;
1519	SMTSETPARA(setcount,SMT_P_SETCOUNT) ;
1520	setcount->count = smc->mib.fddiSMTSetCount.count ;
1521	memcpy((char *)setcount->timestamp,
1522		(char *)smc->mib.fddiSMTSetCount.timestamp,8) ;
1523}
1524
1525/*
1526 * fill echo data
1527 */
1528static void smt_fill_echo(struct s_smc *smc, struct smt_p_echo *echo, u_long seed,
1529			  int len)
1530{
1531	u_char	*p ;
1532
1533	SK_UNUSED(smc) ;
1534	SMTSETPARA(echo,SMT_P_ECHODATA) ;
1535	echo->para.p_len = len ;
1536	for (p = echo->ec_data ; len ; len--) {
1537		*p++ = (u_char) seed ;
1538		seed += 13 ;
1539	}
1540}
1541
1542/*
1543 * clear DNA and UNA
1544 * called from CFM if configuration changes
1545 */
1546static void smt_clear_una_dna(struct s_smc *smc)
1547{
1548	smc->mib.m[MAC0].fddiMACUpstreamNbr = SMT_Unknown ;
1549	smc->mib.m[MAC0].fddiMACDownstreamNbr = SMT_Unknown ;
1550}
1551
1552static void smt_clear_old_una_dna(struct s_smc *smc)
1553{
1554	smc->mib.m[MAC0].fddiMACOldUpstreamNbr = SMT_Unknown ;
1555	smc->mib.m[MAC0].fddiMACOldDownstreamNbr = SMT_Unknown ;
1556}
1557
1558u_long smt_get_tid(struct s_smc *smc)
1559{
1560	u_long	tid ;
1561	while ((tid = ++(smc->sm.smt_tid) ^ SMT_TID_MAGIC) == 0)
1562		;
1563	return(tid & 0x3fffffffL) ;
1564}
1565
1566
1567/*
1568 * table of parameter lengths
1569 */
1570static const struct smt_pdef {
1571	int	ptype ;
1572	int	plen ;
1573	const char	*pswap ;
1574} smt_pdef[] = {
1575	{ SMT_P_UNA,	sizeof(struct smt_p_una) ,
1576		SWAP_SMT_P_UNA					} ,
1577	{ SMT_P_SDE,	sizeof(struct smt_p_sde) ,
1578		SWAP_SMT_P_SDE					} ,
1579	{ SMT_P_STATE,	sizeof(struct smt_p_state) ,
1580		SWAP_SMT_P_STATE				} ,
1581	{ SMT_P_TIMESTAMP,sizeof(struct smt_p_timestamp) ,
1582		SWAP_SMT_P_TIMESTAMP				} ,
1583	{ SMT_P_POLICY,	sizeof(struct smt_p_policy) ,
1584		SWAP_SMT_P_POLICY				} ,
1585	{ SMT_P_LATENCY,	sizeof(struct smt_p_latency) ,
1586		SWAP_SMT_P_LATENCY				} ,
1587	{ SMT_P_NEIGHBORS,sizeof(struct smt_p_neighbor) ,
1588		SWAP_SMT_P_NEIGHBORS				} ,
1589	{ SMT_P_PATH,	sizeof(struct smt_p_path) ,
1590		SWAP_SMT_P_PATH					} ,
1591	{ SMT_P_MAC_STATUS,sizeof(struct smt_p_mac_status) ,
1592		SWAP_SMT_P_MAC_STATUS				} ,
1593	{ SMT_P_LEM,	sizeof(struct smt_p_lem) ,
1594		SWAP_SMT_P_LEM					} ,
1595	{ SMT_P_MAC_COUNTER,sizeof(struct smt_p_mac_counter) ,
1596		SWAP_SMT_P_MAC_COUNTER				} ,
1597	{ SMT_P_MAC_FNC,sizeof(struct smt_p_mac_fnc) ,
1598		SWAP_SMT_P_MAC_FNC				} ,
1599	{ SMT_P_PRIORITY,sizeof(struct smt_p_priority) ,
1600		SWAP_SMT_P_PRIORITY				} ,
1601	{ SMT_P_EB,sizeof(struct smt_p_eb) ,
1602		SWAP_SMT_P_EB					} ,
1603	{ SMT_P_MANUFACTURER,sizeof(struct smp_p_manufacturer) ,
1604		SWAP_SMT_P_MANUFACTURER				} ,
1605	{ SMT_P_REASON,	sizeof(struct smt_p_reason) ,
1606		SWAP_SMT_P_REASON				} ,
1607	{ SMT_P_REFUSED, sizeof(struct smt_p_refused) ,
1608		SWAP_SMT_P_REFUSED				} ,
1609	{ SMT_P_VERSION, sizeof(struct smt_p_version) ,
1610		SWAP_SMT_P_VERSION				} ,
1611#ifdef ESS
1612	{ SMT_P0015, sizeof(struct smt_p_0015) , SWAP_SMT_P0015 } ,
1613	{ SMT_P0016, sizeof(struct smt_p_0016) , SWAP_SMT_P0016 } ,
1614	{ SMT_P0017, sizeof(struct smt_p_0017) , SWAP_SMT_P0017 } ,
1615	{ SMT_P0018, sizeof(struct smt_p_0018) , SWAP_SMT_P0018 } ,
1616	{ SMT_P0019, sizeof(struct smt_p_0019) , SWAP_SMT_P0019 } ,
1617	{ SMT_P001A, sizeof(struct smt_p_001a) , SWAP_SMT_P001A } ,
1618	{ SMT_P001B, sizeof(struct smt_p_001b) , SWAP_SMT_P001B } ,
1619	{ SMT_P001C, sizeof(struct smt_p_001c) , SWAP_SMT_P001C } ,
1620	{ SMT_P001D, sizeof(struct smt_p_001d) , SWAP_SMT_P001D } ,
1621#endif
1622
1623	{ SMT_P_SETCOUNT,0,	SWAP_SMT_P_SETCOUNT		} ,
1624	{ SMT_P1048,	0,	SWAP_SMT_P1048			} ,
1625	{ SMT_P208C,	0,	SWAP_SMT_P208C			} ,
1626	{ SMT_P208D,	0,	SWAP_SMT_P208D			} ,
1627	{ SMT_P208E,	0,	SWAP_SMT_P208E			} ,
1628	{ SMT_P208F,	0,	SWAP_SMT_P208F			} ,
1629	{ SMT_P2090,	0,	SWAP_SMT_P2090			} ,
1630#ifdef	ESS
1631	{ SMT_P320B, sizeof(struct smt_p_320b) , SWAP_SMT_P320B } ,
1632	{ SMT_P320F, sizeof(struct smt_p_320f) , SWAP_SMT_P320F } ,
1633	{ SMT_P3210, sizeof(struct smt_p_3210) , SWAP_SMT_P3210 } ,
1634#endif
1635	{ SMT_P4050,	0,	SWAP_SMT_P4050			} ,
1636	{ SMT_P4051,	0,	SWAP_SMT_P4051			} ,
1637	{ SMT_P4052,	0,	SWAP_SMT_P4052			} ,
1638	{ SMT_P4053,	0,	SWAP_SMT_P4053			} ,
1639} ;
1640
1641#define N_SMT_PLEN	(sizeof(smt_pdef)/sizeof(smt_pdef[0]))
1642
1643int smt_check_para(struct s_smc *smc, struct smt_header	*sm,
1644		   const u_short list[])
1645{
1646	const u_short		*p = list ;
1647	while (*p) {
1648		if (!sm_to_para(smc,sm,(int) *p)) {
1649			DB_SMT("SMT: smt_check_para - missing para %x\n",*p,0);
1650			return(-1) ;
1651		}
1652		p++ ;
1653	}
1654	return(0) ;
1655}
1656
1657void *sm_to_para(struct s_smc *smc, struct smt_header *sm, int para)
1658{
1659	char	*p ;
1660	int	len ;
1661	int	plen ;
1662	void	*found = NULL;
1663
1664	SK_UNUSED(smc) ;
1665
1666	len = sm->smt_len ;
1667	p = (char *)(sm+1) ;		/* pointer to info */
1668	while (len > 0 ) {
1669		if (((struct smt_para *)p)->p_type == para)
1670			found = (void *) p ;
1671		plen = ((struct smt_para *)p)->p_len + PARA_LEN ;
1672		p += plen ;
1673		len -= plen ;
1674		if (len < 0) {
1675			DB_SMT("SMT : sm_to_para - length error %d\n",plen,0) ;
1676			return NULL;
1677		}
1678		if ((plen & 3) && (para != SMT_P_ECHODATA)) {
1679			DB_SMT("SMT : sm_to_para - odd length %d\n",plen,0) ;
1680			return NULL;
1681		}
1682		if (found)
1683			return(found) ;
1684	}
1685	return NULL;
1686}
1687
1688
1689#ifdef	DEBUG
1690#define hextoasc(x)	"0123456789abcdef"[x]
1691
1692char *addr_to_string(struct fddi_addr *addr)
1693{
1694	int	i ;
1695	static char	string[6*3] = "****" ;
1696
1697	for (i = 0 ; i < 6 ; i++) {
1698		string[i*3] = hextoasc((addr->a[i]>>4)&0xf) ;
1699		string[i*3+1] = hextoasc((addr->a[i])&0xf) ;
1700		string[i*3+2] = ':' ;
1701	}
1702	string[5*3+2] = 0 ;
1703	return(string) ;
1704}
1705#endif
1706
1707#ifdef	AM29K
1708int smt_ifconfig(int argc, char *argv[])
1709{
1710	if (argc >= 2 && !strcmp(argv[0],"opt_bypass") &&
1711	    !strcmp(argv[1],"yes")) {
1712		smc->mib.fddiSMTBypassPresent = 1 ;
1713		return(0) ;
1714	}
1715	return(amdfddi_config(0,argc,argv)) ;
1716}
1717#endif
1718
1719/*
1720 * return static mac index
1721 */
1722static int mac_index(struct s_smc *smc, int mac)
1723{
1724	SK_UNUSED(mac) ;
1725#ifdef	CONCENTRATOR
1726	SK_UNUSED(smc) ;
1727	return(NUMPHYS+1) ;
1728#else
1729	return((smc->s.sas == SMT_SAS) ? 2 : 3) ;
1730#endif
1731}
1732
1733/*
1734 * return static phy index
1735 */
1736static int phy_index(struct s_smc *smc, int phy)
1737{
1738	SK_UNUSED(smc) ;
1739	return(phy+1);
1740}
1741
1742/*
1743 * return dynamic mac connection resource index
1744 */
1745static int mac_con_resource_index(struct s_smc *smc, int mac)
1746{
1747#ifdef	CONCENTRATOR
1748	SK_UNUSED(smc) ;
1749	SK_UNUSED(mac) ;
1750	return(entity_to_index(smc,cem_get_downstream(smc,ENTITY_MAC))) ;
1751#else
1752	SK_UNUSED(mac) ;
1753	switch (smc->mib.fddiSMTCF_State) {
1754	case SC9_C_WRAP_A :
1755	case SC5_THRU_B :
1756	case SC11_C_WRAP_S :
1757		return(1) ;
1758	case SC10_C_WRAP_B :
1759	case SC4_THRU_A :
1760		return(2) ;
1761	}
1762	return(smc->s.sas == SMT_SAS ? 2 : 3) ;
1763#endif
1764}
1765
1766/*
1767 * return dynamic phy connection resource index
1768 */
1769static int phy_con_resource_index(struct s_smc *smc, int phy)
1770{
1771#ifdef	CONCENTRATOR
1772	return(entity_to_index(smc,cem_get_downstream(smc,ENTITY_PHY(phy)))) ;
1773#else
1774	switch (smc->mib.fddiSMTCF_State) {
1775	case SC9_C_WRAP_A :
1776		return(phy == PA ? 3 : 2) ;
1777	case SC10_C_WRAP_B :
1778		return(phy == PA ? 1 : 3) ;
1779	case SC4_THRU_A :
1780		return(phy == PA ? 3 : 1) ;
1781	case SC5_THRU_B :
1782		return(phy == PA ? 2 : 3) ;
1783	case SC11_C_WRAP_S :
1784		return(2) ;
1785	}
1786	return(phy) ;
1787#endif
1788}
1789
1790#ifdef	CONCENTRATOR
1791static int entity_to_index(struct s_smc *smc, int e)
1792{
1793	if (e == ENTITY_MAC)
1794		return(mac_index(smc,1)) ;
1795	else
1796		return(phy_index(smc,e - ENTITY_PHY(0))) ;
1797}
1798#endif
1799
1800#ifdef	LITTLE_ENDIAN
1801static int smt_swap_short(u_short s)
1802{
1803	return(((s>>8)&0xff)|((s&0xff)<<8)) ;
1804}
1805
1806void smt_swap_para(struct smt_header *sm, int len, int direction)
1807/* int direction;	0 encode 1 decode */
1808{
1809	struct smt_para	*pa ;
1810	const  struct smt_pdef	*pd ;
1811	char	*p ;
1812	int	plen ;
1813	int	type ;
1814	int	i ;
1815
1816/*	printf("smt_swap_para sm %x len %d dir %d\n",
1817		sm,len,direction) ;
1818 */
1819	smt_string_swap((char *)sm,SWAP_SMTHEADER,len) ;
1820
1821	/* swap args */
1822	len -= sizeof(struct smt_header) ;
1823
1824	p = (char *) (sm + 1) ;
1825	while (len > 0) {
1826		pa = (struct smt_para *) p ;
1827		plen = pa->p_len ;
1828		type = pa->p_type ;
1829		pa->p_type = smt_swap_short(pa->p_type) ;
1830		pa->p_len = smt_swap_short(pa->p_len) ;
1831		if (direction) {
1832			plen = pa->p_len ;
1833			type = pa->p_type ;
1834		}
1835		/*
1836		 * note: paras can have 0 length !
1837		 */
1838		if (plen < 0)
1839			break ;
1840		plen += PARA_LEN ;
1841		for (i = N_SMT_PLEN, pd = smt_pdef; i ; i--,pd++) {
1842			if (pd->ptype == type)
1843				break ;
1844		}
1845		if (i && pd->pswap) {
1846			smt_string_swap(p+PARA_LEN,pd->pswap,len) ;
1847		}
1848		len -= plen ;
1849		p += plen ;
1850	}
1851}
1852
1853static void smt_string_swap(char *data, const char *format, int len)
1854{
1855	const char	*open_paren = NULL ;
1856	int	x ;
1857
1858	while (len > 0  && *format) {
1859		switch (*format) {
1860		case '[' :
1861			open_paren = format ;
1862			break ;
1863		case ']' :
1864			format = open_paren ;
1865			break ;
1866		case '1' :
1867		case '2' :
1868		case '3' :
1869		case '4' :
1870		case '5' :
1871		case '6' :
1872		case '7' :
1873		case '8' :
1874		case '9' :
1875			data  += *format - '0' ;
1876			len   -= *format - '0' ;
1877			break ;
1878		case 'c':
1879			data++ ;
1880			len-- ;
1881			break ;
1882		case 's' :
1883			x = data[0] ;
1884			data[0] = data[1] ;
1885			data[1] = x ;
1886			data += 2 ;
1887			len -= 2 ;
1888			break ;
1889		case 'l' :
1890			x = data[0] ;
1891			data[0] = data[3] ;
1892			data[3] = x ;
1893			x = data[1] ;
1894			data[1] = data[2] ;
1895			data[2] = x ;
1896			data += 4 ;
1897			len -= 4 ;
1898			break ;
1899		}
1900		format++ ;
1901	}
1902}
1903#else
1904void smt_swap_para(struct smt_header *sm, int len, int direction)
1905/* int direction;	0 encode 1 decode */
1906{
1907	SK_UNUSED(sm) ;
1908	SK_UNUSED(len) ;
1909	SK_UNUSED(direction) ;
1910}
1911#endif
1912
1913/*
1914 * PMF actions
1915 */
1916int smt_action(struct s_smc *smc, int class, int code, int index)
1917{
1918	int	event ;
1919	int	port ;
1920	DB_SMT("SMT: action %d code %d\n",class,code) ;
1921	switch(class) {
1922	case SMT_STATION_ACTION :
1923		switch(code) {
1924		case SMT_STATION_ACTION_CONNECT :
1925			smc->mib.fddiSMTRemoteDisconnectFlag = FALSE ;
1926			queue_event(smc,EVENT_ECM,EC_CONNECT) ;
1927			break ;
1928		case SMT_STATION_ACTION_DISCONNECT :
1929			queue_event(smc,EVENT_ECM,EC_DISCONNECT) ;
1930			smc->mib.fddiSMTRemoteDisconnectFlag = TRUE ;
1931			RS_SET(smc,RS_DISCONNECT) ;
1932			AIX_EVENT(smc, (u_long) FDDI_RING_STATUS, (u_long)
1933				FDDI_SMT_EVENT, (u_long) FDDI_REMOTE_DISCONNECT,
1934				smt_get_event_word(smc));
1935			break ;
1936		case SMT_STATION_ACTION_PATHTEST :
1937			AIX_EVENT(smc, (u_long) FDDI_RING_STATUS, (u_long)
1938				FDDI_SMT_EVENT, (u_long) FDDI_PATH_TEST,
1939				smt_get_event_word(smc));
1940			break ;
1941		case SMT_STATION_ACTION_SELFTEST :
1942			AIX_EVENT(smc, (u_long) FDDI_RING_STATUS, (u_long)
1943				FDDI_SMT_EVENT, (u_long) FDDI_REMOTE_SELF_TEST,
1944				smt_get_event_word(smc));
1945			break ;
1946		case SMT_STATION_ACTION_DISABLE_A :
1947			if (smc->y[PA].pc_mode == PM_PEER) {
1948				RS_SET(smc,RS_EVENT) ;
1949				queue_event(smc,EVENT_PCM+PA,PC_DISABLE) ;
1950			}
1951			break ;
1952		case SMT_STATION_ACTION_DISABLE_B :
1953			if (smc->y[PB].pc_mode == PM_PEER) {
1954				RS_SET(smc,RS_EVENT) ;
1955				queue_event(smc,EVENT_PCM+PB,PC_DISABLE) ;
1956			}
1957			break ;
1958		case SMT_STATION_ACTION_DISABLE_M :
1959			for (port = 0 ; port <  NUMPHYS ; port++) {
1960				if (smc->mib.p[port].fddiPORTMy_Type != TM)
1961					continue ;
1962				RS_SET(smc,RS_EVENT) ;
1963				queue_event(smc,EVENT_PCM+port,PC_DISABLE) ;
1964			}
1965			break ;
1966		default :
1967			return(1) ;
1968		}
1969		break ;
1970	case SMT_PORT_ACTION :
1971		switch(code) {
1972		case SMT_PORT_ACTION_ENABLE :
1973			event = PC_ENABLE ;
1974			break ;
1975		case SMT_PORT_ACTION_DISABLE :
1976			event = PC_DISABLE ;
1977			break ;
1978		case SMT_PORT_ACTION_MAINT :
1979			event = PC_MAINT ;
1980			break ;
1981		case SMT_PORT_ACTION_START :
1982			event = PC_START ;
1983			break ;
1984		case SMT_PORT_ACTION_STOP :
1985			event = PC_STOP ;
1986			break ;
1987		default :
1988			return(1) ;
1989		}
1990		queue_event(smc,EVENT_PCM+index,event) ;
1991		break ;
1992	default :
1993		return(1) ;
1994	}
1995	return(0) ;
1996}
1997
1998/*
1999 * canonical conversion of <len> bytes beginning form *data
2000 */
2001#ifdef  USE_CAN_ADDR
2002static void hwm_conv_can(struct s_smc *smc, char *data, int len)
2003{
2004	int i ;
2005
2006	SK_UNUSED(smc) ;
2007
2008	for (i = len; i ; i--, data++)
2009		*data = bitrev8(*data);
2010}
2011#endif
2012
2013#endif	/* no SLIM_SMT */
2014