]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_Timer.c
73012cb755a4423d4b8dbb1c66a31a1d831f4251
[apple/xnu.git] / bsd / netat / adsp_Timer.c
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * Copyright (c) 1990, 1996-1998 Apple Computer, Inc.
32 * All Rights Reserved.
33 */
34
35 /*
36 * Timer.c
37 *
38 * From v01.12 06/22/90 mbs
39 * Modified for MP, 1996 by Tuyen Nguyen
40 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
41 */
42
43 #include <sys/errno.h>
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <machine/spl.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/proc.h>
50 #include <sys/filedesc.h>
51 #include <sys/fcntl.h>
52 #include <sys/mbuf.h>
53 #include <sys/socket.h>
54 #include <sys/time.h>
55
56 #include <netat/sysglue.h>
57 #include <netat/appletalk.h>
58 #include <netat/at_pcb.h>
59 #include <netat/debug.h>
60 #include <netat/adsp.h>
61 #include <netat/adsp_internal.h>
62
63 void TimerTick();
64
65 /*
66 * TrashSession
67 *
68 * Cleanly abort a session that might be open. Called if probe timer expires,
69 * or from AppleTalk event handler (close or network gone away)
70 *
71 * Only call if the session is active (I.e. not for closed or listeners)
72 *
73 * INPUTS:
74 * session pointer
75 * OUTPUTS:
76 * none
77 */
78 void TrashSession(sp) /* (CCBPtr sp) */
79 CCBPtr sp;
80 {
81 int s;
82
83 ATDISABLE(s, sp->lock);
84 sp->userFlags |= eTearDown;
85 sp->removing = 1;
86 sp->state = sClosed;
87 ATENABLE(s, sp->lock);
88
89 DoClose(sp, errAborted, 1);
90 }
91
92
93 /*
94 * DoTimerElem
95 *
96 * INPUTS:
97 *
98 * OUTPUTS:
99 *
100 */
101 void DoTimerElem(t) /* (TimerElemPtr t) */
102 TimerElemPtr t;
103 {
104 CCBPtr sp;
105 int s;
106
107 sp = (CCBPtr)((Ptr)t - t->type); /* Recover stream pointer for this guy */
108 ATDISABLE(s, sp->lock);
109
110 if (t->type == kFlushTimerType) { /* flush write data time just fired */
111 if (sp->sData) { /* If there's any data, flush it. */
112 sp->writeFlush = 1;
113 goto send;
114 }
115 } else if (t->type == kRetryTimerType) {
116 if (sp->waitingAck) {
117
118 sp->waitingAck = 0;
119 sp->sendSeq = sp->firstRtmtSeq;
120 sp->pktSendCnt = 0;
121 sp->resentData = 1; /* Had to resend data */
122 sp->noXmitFlow = 1; /* Don't incr. max packets. */
123
124 if ((sp->pktSendMax /= 2) == 0) /* Back off on max # packets
125 * sent */
126 sp->pktSendMax = 1;
127
128 if ((sp->roundTrip *= 2) > sp->probeInterval)
129 sp->roundTrip = sp->probeInterval;
130 sp->rtmtInterval = sp->roundTrip + ((short)2 *
131 (short)sp->deviation);
132 goto send;
133 }
134 } else if (t->type == kAttnTimerType) {
135 if (sp->sapb) { /* Unacknowledged attn pkt */
136 sp->sendAttnData = 1;
137 goto send;
138 }
139 } else if (t->type == kResetTimerType) {
140 if (sp->frpb) { /* Unacknowledged forward reset */
141 sp->sendCtl |= B_CTL_FRESET;
142 goto send;
143 }
144 } else if (t->type == kProbeTimerType) {
145 if (sp->state == sOpen || sp->state == sClosing) {
146 if (--sp->probeCntr == 0) { /* Connection died */
147 ATENABLE(s, sp->lock);
148 TrashSession(sp);
149 return;
150 } else {
151 InsertTimerElem(&adspGlobal.slowTimers, &sp->ProbeTimer,
152 sp->probeInterval);
153 sp->sendCtl |= B_CTL_PROBE;
154 goto send;
155 }
156 } else if (sp->state == sOpening) {
157 if ((sp->openState == O_STATE_OPENWAIT) ||
158 (sp->openState == O_STATE_ESTABLISHED))
159 {
160 if (--sp->openRetrys == 0) { /* Oops, didn't open */
161 sp->state = sClosed;
162 ATENABLE(s, sp->lock);
163 DoClose(sp, errOpening, 1);
164 return;
165 } /* open failed */
166 else /* Send packet again */
167 {
168 sp->sendCtl |= (sp->openState == O_STATE_OPENWAIT) ?
169 B_CTL_OREQ : B_CTL_OREQACK;
170 goto send;
171 }
172 } /* we're opening */
173 }
174 }
175
176 else {
177 dPrintf(D_M_ADSP, D_L_ERROR, ("DoTimerElem:Unknown timer type!\n"));
178 }
179
180 ATENABLE(s, sp->lock);
181 return;
182
183 send:
184 ATENABLE(s, sp->lock);
185 CheckSend(sp);
186 }
187
188 void TimerTick_funnel(void *arg)
189 {
190 atalk_lock();
191 TimerTick();
192 atalk_unlock();
193 }
194
195 static StopTimer;
196
197 /*
198 * TimerTick
199 *
200 * Called 6 times per second
201 * INPUTS:
202 *
203 * OUTPUTS:
204 *
205 */
206 void TimerTick() /* (void) */
207 {
208
209 if (StopTimer) {
210 return;
211 }
212 TimerQueueTick(&adspGlobal.slowTimers);
213 TimerQueueTick(&adspGlobal.fastTimers);
214 timeout(TimerTick_funnel, (caddr_t)0, HZ/6);
215 }
216
217 void TimerStop()
218 {
219 StopTimer = 1;
220 untimeout(TimerTick_funnel, (caddr_t) 0);
221 }