]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
5d5c5d0d A |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. |
3 | * | |
8ad349bb | 4 | * @APPLE_LICENSE_OSREFERENCE_HEADER_START@ |
1c79356b | 5 | * |
8ad349bb A |
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@ | |
1c79356b A |
29 | */ |
30 | /* | |
31 | * Copyright (c) 1990, 1995-1998 Apple Computer, Inc. | |
32 | * All Rights Reserved. | |
33 | */ | |
34 | ||
35 | /* dspClose.c | |
36 | * From Mike Shoemaker v01.16 06/29/90 mbs | |
37 | */ | |
38 | /* | |
39 | * Change log: | |
40 | * 06/29/95 - Modified to handle flow control for writing (Tuyen Nguyen) | |
41 | * Modified for MP, 1996 by Tuyen Nguyen | |
42 | * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX. | |
43 | */ | |
44 | ||
45 | #include <sys/errno.h> | |
46 | #include <sys/types.h> | |
47 | #include <sys/param.h> | |
48 | #include <machine/spl.h> | |
49 | #include <sys/systm.h> | |
50 | #include <sys/kernel.h> | |
51 | #include <sys/proc.h> | |
52 | #include <sys/filedesc.h> | |
53 | #include <sys/fcntl.h> | |
54 | #include <sys/mbuf.h> | |
55 | #include <sys/socket.h> | |
56 | #include <sys/socketvar.h> | |
57 | #include <sys/time.h> | |
58 | ||
59 | #include <netat/sysglue.h> | |
60 | #include <netat/appletalk.h> | |
61 | #include <netat/ddp.h> | |
62 | #include <netat/at_pcb.h> | |
63 | #include <netat/debug.h> | |
64 | #include <netat/adsp.h> | |
65 | #include <netat/adsp_internal.h> | |
66 | ||
1c79356b A |
67 | |
68 | static void qRemove(CCBPtr, CCBPtr); | |
69 | ||
70 | ||
71 | /* | |
72 | * CheckOkToClose | |
73 | * | |
74 | * Check to see if it is OK to close this connection cleanly. | |
75 | * | |
76 | * INPUTS: | |
77 | * Stream pointer | |
78 | * OUTPUTS: | |
79 | * True if no outstanding transactions and we can close cleanly | |
80 | */ | |
81 | int CheckOkToClose(sp) /* (CCBPtr sp) */ | |
82 | CCBPtr sp; | |
83 | { | |
84 | ||
85 | if (sp->sData) /* Outstanding data ? */ | |
86 | return 0; | |
87 | ||
88 | if (sp->sapb) /* Outstanding send attention ? */ | |
89 | return 0; | |
90 | ||
91 | if (sp->frpb) /* Outstanding forward reset ? */ | |
92 | return 0; | |
93 | ||
94 | if (sp->sendAttnAck) | |
95 | return 0; | |
96 | ||
97 | if (sp->sendDataAck) | |
98 | return 0; | |
99 | ||
100 | /* | |
101 | * Must be OK to close | |
102 | */ | |
103 | sp->sendCtl |= B_CTL_CLOSE; /* So, need to send close advice */ | |
104 | sp->callSend = 1; | |
105 | ||
106 | return 1; /* It's OK to close */ | |
107 | } | |
108 | ||
109 | ||
110 | /* | |
111 | * CompleteQueue | |
112 | * | |
113 | * Given the address of the head of a queue of DSP parameter blocks, zero | |
114 | * the queue, and complete each item on the queue with the given result | |
115 | * code. | |
116 | * | |
117 | * INPUTS: | |
118 | * qhead Address of ptr to first queue element | |
119 | * code The result code | |
120 | * OUTPUTS: | |
121 | * none | |
122 | */ | |
123 | int CompleteQueue(qhead, code) /* (DSPPBPtr FPTR qhead, OSErr code) */ | |
124 | struct adspcmd **qhead; | |
125 | int code; | |
126 | { | |
127 | register struct adspcmd *p; | |
128 | register struct adspcmd *n; | |
129 | register gref_t *gref; | |
130 | register int total = 0; | |
131 | CCBPtr sp = 0; | |
1c79356b A |
132 | |
133 | n = *qhead; /* Get first item */ | |
134 | *qhead = 0; /* Zero out the queue */ | |
135 | if (n) { | |
136 | gref = n->gref; | |
137 | if (gref->info) { | |
138 | sp = (CCBPtr)gbuf_rptr(((gbuf_t *)gref->info)); | |
139 | atalk_flush(sp->gref); | |
1c79356b A |
140 | } |
141 | } | |
142 | ||
143 | while (p = n) { /* while items left */ | |
144 | n = (struct adspcmd *)(p->qLink); /* Save next guy */ | |
145 | p->ioResult = code; | |
146 | if (sp) { | |
147 | completepb(sp, p); /* complete the copy of the request */ | |
148 | total++; | |
149 | } else | |
150 | gbuf_freem(p->mp); | |
151 | } /* while */ | |
1c79356b A |
152 | return(total); |
153 | } | |
154 | ||
155 | /* | |
156 | * RemoveCCB | |
157 | * | |
158 | * Called from do close to free up the user's CCB. So, we remove the | |
159 | * CCB from the list of CCB's. | |
160 | * | |
161 | * INPUTS: | |
162 | * sp pointer to ccb | |
163 | * pb a remove param block to complete when done | |
164 | * OUTPUTS: | |
165 | * none | |
166 | */ | |
167 | ||
168 | void RemoveCCB(sp, pb) /* (CCBPtr sp, DSPPBPtr pb) */ | |
169 | CCBPtr sp; | |
170 | struct adspcmd *pb; | |
171 | { | |
172 | gref_t *gref; | |
173 | ||
174 | if (sp->gref == 0) | |
175 | return; | |
176 | /* | |
177 | * Unlink CCB from list | |
178 | */ | |
55e303ae | 179 | qRemove((CCB *)AT_ADSP_STREAMS, sp); /* remove sp from active streams queue */ |
1c79356b A |
180 | |
181 | if (pb) { | |
182 | pb->ioResult = 0; | |
183 | if (pb->ioc) /* is this a current or queued request */ | |
184 | adspioc_ack(0, pb->ioc, pb->gref); /* current */ | |
185 | else { | |
186 | completepb(sp, pb); /* queued */ | |
187 | } | |
188 | ||
189 | if (sp->opb && (pb != sp->opb)) { /* if the pb requested is not the */ | |
190 | pb = sp->opb; /* waiting open pb, complete it too */ | |
191 | sp->opb = 0; | |
192 | pb->ioResult = 0; | |
193 | completepb(sp, pb); | |
194 | } else { | |
195 | sp->opb = 0; | |
196 | } | |
197 | } | |
198 | gref = sp->gref; | |
199 | sp->gref = 0; | |
200 | if (gref->info == (char *)sp->sp_mp) { /* queue head is still valid */ | |
201 | unsigned char skt; | |
202 | ||
203 | if ((skt = sp->localSocket) != 0) { | |
204 | if (adspDeassignSocket(sp) == 0) | |
205 | ddp_notify_nbp(skt, sp->pid, DDP_ADSP); | |
206 | } | |
207 | ||
208 | if (gref->info) { | |
209 | gbuf_freem((gbuf_t *)gref->info); /* free the CCB */ | |
210 | gref->info = 0; | |
211 | } | |
212 | } else | |
213 | gbuf_freem(sp->sp_mp); /* our head is already gone, be sure | |
214 | * to release our resources too */ | |
215 | } | |
216 | ||
217 | int AbortIO(sp, err) | |
218 | CCBPtr sp; | |
219 | short err; | |
220 | { | |
221 | register int total; | |
222 | ||
223 | if (sp->gref == 0) | |
224 | return 0; | |
225 | /* | |
226 | * Complete all outstanding transactions. | |
227 | */ | |
91447636 | 228 | total = CompleteQueue(&sp->sapb, err); /* Abort outstanding send attentions */ |
1c79356b A |
229 | CompleteQueue(&sp->frpb, err); /* Abort outstanding forward resets */ |
230 | ||
231 | if (sp->sbuf_mb) { /* clear the send queue */ | |
232 | gbuf_freel(sp->sbuf_mb); | |
233 | sp->sbuf_mb = 0; | |
234 | } | |
235 | ||
236 | if (sp->csbuf_mb) { | |
237 | gbuf_freem(sp->csbuf_mb); | |
238 | sp->csbuf_mb = 0; | |
239 | } | |
240 | sp->sData = 0; | |
241 | ||
242 | return(total); | |
243 | } | |
244 | ||
245 | /* | |
246 | * DoClose | |
247 | * | |
248 | * Called from several places (probe timeout, recv close advice, | |
249 | * dspRemove, etc.) to change state of connection to closed and | |
250 | * complete all outstanding I/O. | |
251 | * | |
252 | * Will also remove the CCB if there is a dsp remove pending. | |
253 | * | |
254 | * INPUTS: | |
255 | * sp An ADSP stream | |
256 | * OUTPUTS: | |
257 | * none | |
258 | */ | |
259 | void DoClose(sp, err, force_abort) /* (CCBPtr sp, OSErr err) */ | |
260 | register CCBPtr sp; | |
261 | int err; | |
262 | { | |
263 | register struct adspcmd *pb, *np; | |
264 | register gbuf_t *mp; | |
265 | int aborted_count; | |
266 | ||
267 | dPrintf(D_M_ADSP, D_L_TRACE, ("DoClose: pid=%d,e=%d,a=%d,s=%d,r=%d\n", | |
268 | sp->pid, err, force_abort, sp->localSocket, sp->removing)); | |
269 | sp->userFlags |= eClosed; /* Set flag */ | |
270 | sp->state = sClosed; | |
271 | sp->openState = O_STATE_NOTHING; | |
272 | ||
273 | /* | |
274 | * Clean up any timer elements | |
275 | */ | |
276 | RemoveTimerElem(&adspGlobal.slowTimers, &sp->ProbeTimer); | |
277 | RemoveTimerElem(&adspGlobal.fastTimers, &sp->FlushTimer); | |
278 | RemoveTimerElem(&adspGlobal.fastTimers, &sp->RetryTimer); | |
279 | RemoveTimerElem(&adspGlobal.fastTimers, &sp->AttnTimer); | |
280 | RemoveTimerElem(&adspGlobal.fastTimers, &sp->ResetTimer); | |
281 | ||
282 | aborted_count = AbortIO(sp, err); | |
283 | np = sp->opb; /* Get list of close/removes to complete */ | |
284 | sp->opb = 0; /* set this list null */ | |
285 | ||
286 | while (pb = np) { /* Handle all of the close/remove param blks */ | |
287 | np = (struct adspcmd *)pb->qLink; /* Get next guy (if any) */ | |
288 | pb->qLink = 0; | |
289 | pb->ioResult = err; | |
290 | completepb(sp, pb); | |
291 | } | |
292 | if (sp->removing && (force_abort >= 0)) { /* Abort outstanding receives */ | |
293 | aborted_count += CompleteQueue(&sp->rpb, err); | |
294 | ||
295 | if (sp->deferred_mb) { | |
296 | gbuf_freel(sp->deferred_mb); | |
297 | sp->deferred_mb = 0; | |
298 | } | |
299 | if (sp->attn_mb) { | |
300 | gbuf_freem(sp->attn_mb); | |
301 | sp->attn_mb = 0; | |
302 | } | |
303 | if (sp->rbuf_mb) { /* clear the rcv queue */ | |
304 | gbuf_freem(sp->rbuf_mb); | |
305 | sp->rbuf_mb = 0; | |
306 | } | |
307 | if (sp->crbuf_mb) { | |
308 | gbuf_freem(sp->crbuf_mb); | |
309 | sp->crbuf_mb = 0; | |
310 | } | |
311 | sp->rData = 0; | |
312 | ||
313 | /* if our connection has been timed out */ | |
314 | /* and the user wasn't notified of the TearDown */ | |
315 | /* because of pending requests on this socket */ | |
316 | /* then fake a read completion to force the notification */ | |
317 | ||
318 | if (force_abort && aborted_count == 0) { | |
319 | if (mp = gbuf_alloc(sizeof(struct adspcmd), PRI_HI)) { | |
320 | pb = (struct adspcmd *)gbuf_rptr(mp); | |
321 | gbuf_wset(mp,sizeof(struct adspcmd)); | |
322 | ||
323 | bzero((caddr_t) pb, sizeof(struct adspcmd)); | |
324 | pb->mp = mp; | |
325 | pb->csCode = dspRead; | |
326 | pb->ioResult = errAborted; | |
327 | completepb(sp, pb); /* send fake read completion */ | |
328 | } | |
329 | } | |
330 | sp->removing = 0; | |
331 | RemoveCCB(sp, 0); /* Will call completion routine */ | |
332 | } | |
333 | sp->userFlags &= ~eClosed; | |
334 | } | |
335 | ||
336 | ||
337 | /* | |
338 | * dspClose | |
339 | * | |
340 | * Also called for dspRemove and dspCLRemove. | |
341 | * Must handle case of multiple close calls being issued (without | |
342 | * abort bit set) Can only allow one pending remove though. | |
343 | * | |
344 | * INPUTS: | |
345 | * --> ccbRefNum refnum of connection end | |
346 | * --> abort abort the connection | |
347 | * | |
348 | * OUTPUTS: | |
349 | * none | |
350 | * | |
351 | * ERRORS: | |
352 | * errRefNum Bad connection Refnum | |
353 | */ | |
354 | int adspClose(sp, pb) /* (DSPPBPtr pb) */ | |
355 | register CCBPtr sp; | |
356 | register struct adspcmd *pb; | |
357 | { | |
1c79356b A |
358 | register gbuf_t *mp; |
359 | ||
360 | /* Must execute nearly all of this with ints off because user could | |
361 | * be issuing a second dspRemove while the first is pending. Until | |
362 | * we can detect this, we must not allow interrupts. | |
363 | * Also, we can't handle the case where a close was issued earlier, | |
364 | * and now this is the remove. If the write completion for the | |
365 | * close advice packet occurs in the middle of this, we might | |
366 | * foul up. | |
367 | */ | |
368 | ||
369 | if (sp == 0) { | |
370 | pb->ioResult = errRefNum; | |
371 | return EINVAL; | |
372 | } | |
373 | ||
374 | /* | |
375 | * Handle dspCLRemove | |
376 | */ | |
377 | if (pb->csCode == (short)dspCLRemove) { /* Remove connection listener */ | |
378 | if (sp->state != (short)sListening) { /* But it's not a listener! */ | |
379 | pb->ioResult = errState; | |
380 | return EINVAL; | |
381 | } | |
382 | CompleteQueue(&sp->opb, errAborted); /* Complete all dspListens */ | |
383 | RemoveCCB(sp, pb); /* Will call completion routine */ | |
384 | return 0; | |
385 | } | |
386 | ||
387 | ||
388 | /* | |
389 | * Either dspClose or dspRemove | |
390 | */ | |
391 | ||
392 | if (sp->removing) { /* Don't allow dspRemove or dspClose */ | |
393 | /* after one dspRemove has been issued. */ | |
394 | pb->ioResult = errState; | |
395 | return EINVAL; | |
396 | } | |
397 | ||
398 | ||
399 | /* | |
400 | * The previous Macintosh ADSP allowed you to call close on a | |
401 | * connection that was in the process of opening or passively | |
402 | * waiting for an open request. It is also legal to close a | |
403 | * connection that is already closed. No error will be generated. | |
404 | * | |
405 | * It is also legal to issue a second close call while the first | |
406 | * is still pending. | |
407 | */ | |
408 | if (pb->csCode == (short)dspClose) { | |
1c79356b A |
409 | if ((sp->state == (short)sPassive) || (sp->state == (short)sOpening)) { |
410 | sp->state = sClosed; | |
1c79356b A |
411 | DoClose(sp, errAborted, 0); |
412 | pb->ioResult = 0; | |
413 | adspioc_ack(0, pb->ioc, pb->gref); | |
414 | return 0; | |
415 | } | |
416 | ||
417 | if (sp->state == (word)sClosed) { /* Ok to close a closed connection */ | |
1c79356b A |
418 | pb->ioResult = 0; |
419 | adspioc_ack(0, pb->ioc, pb->gref); | |
420 | return 0; | |
421 | } | |
422 | if ((sp->state != (word)sOpen) && (sp->state != (word)sClosing)) { | |
1c79356b A |
423 | pb->ioResult = errState; |
424 | return EINVAL; | |
425 | } | |
426 | ||
427 | sp->state = sClosing; /* No matter what, we're closing */ | |
1c79356b A |
428 | } /* dspClose */ |
429 | ||
430 | else { /* dspRemove */ | |
1c79356b A |
431 | sp->removing = 1; /* Prevent allowing another dspClose. */ |
432 | /* Tells completion routine of close */ | |
433 | /* packet to remove us. */ | |
434 | ||
435 | if (sp->state == sPassive || sp->state == sClosed || | |
436 | sp->state == sOpening) { | |
437 | sp->state = sClosed; | |
1c79356b A |
438 | DoClose(sp, errAborted, 0); /* Will remove CCB! */ |
439 | return 0; | |
5d5c5d0d | 440 | } else /* sClosing & sOpen */ |
1c79356b | 441 | sp->state = sClosing; |
1c79356b A |
442 | |
443 | } /* dspRemove */ | |
444 | ||
445 | if (pb->u.closeParams.abort || CheckOkToClose(sp)) /* going to close */ | |
446 | { | |
447 | AbortIO(sp, errAborted); | |
448 | sp->sendCtl = B_CTL_CLOSE; /* Send close advice */ | |
449 | } | |
450 | ||
451 | pb->ioResult = 1; | |
452 | if ( (mp = gbuf_copym(pb->mp)) ) { /* duplicate user request */ | |
453 | adspioc_ack(0, pb->ioc, pb->gref); /* release user */ | |
454 | pb = (struct adspcmd *)gbuf_rptr(mp); /* get new parameter block */ | |
455 | pb->ioc = 0; | |
456 | pb->mp = mp; | |
1c79356b | 457 | qAddToEnd(&sp->opb, pb); /* and save it */ |
1c79356b A |
458 | } else { |
459 | pb->ioResult = 0; | |
460 | adspioc_ack(0, pb->ioc, pb->gref); /* release user, and keep no copy | |
461 | * for kernel bookkeeping, yetch! | |
462 | */ | |
463 | } | |
464 | CheckSend(sp); | |
465 | ||
466 | return 0; | |
467 | } | |
468 | ||
469 | static void qRemove(qptr, elem) | |
470 | register CCBPtr qptr; | |
471 | register CCBPtr elem; | |
472 | { | |
1c79356b | 473 | |
1c79356b A |
474 | while(qptr->ccbLink) { |
475 | if ((DSPPBPtr)(qptr->ccbLink) == (DSPPBPtr)elem) { | |
476 | qptr->ccbLink = elem->ccbLink; | |
477 | elem->ccbLink = 0; | |
1c79356b A |
478 | return; |
479 | } | |
480 | qptr = qptr->ccbLink; | |
481 | } | |
1c79356b A |
482 | } |
483 | ||
484 | int RxClose(sp) | |
485 | register CCBPtr sp; | |
486 | { | |
487 | register gbuf_t *mp; | |
488 | register struct adspcmd *pb; | |
1c79356b | 489 | |
5d5c5d0d | 490 | if ((sp->state == sClosing) || (sp->state == sClosed)) |
1c79356b | 491 | return 0; |
5d5c5d0d | 492 | |
1c79356b | 493 | sp->state = sClosed; |
1c79356b A |
494 | CheckReadQueue(sp); /* try to deliver all remaining data */ |
495 | ||
496 | if ( (mp = gbuf_alloc(sizeof(struct adspcmd), PRI_HI)) ) { | |
497 | pb = (struct adspcmd *)gbuf_rptr(mp); | |
498 | gbuf_wset(mp,sizeof(struct adspcmd)); | |
499 | pb->ioc = 0; | |
500 | pb->mp = mp; | |
501 | ||
502 | pb->csCode = dspClose; | |
503 | pb->ioResult = 0; | |
504 | completepb(sp, pb); /* send close completion */ | |
505 | } | |
506 | ||
507 | if ((sp->userFlags & eClosed) == 0) | |
508 | DoClose(sp, errAborted, -1); /* abort send requests and timers */ | |
509 | ||
1c79356b A |
510 | return 0; |
511 | } |