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