]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_Close.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1995-1998 Apple Computer, Inc.
27 * All Rights Reserved.
31 * From Mike Shoemaker v01.16 06/29/90 mbs
35 * 06/29/95 - Modified to handle flow control for writing (Tuyen Nguyen)
36 * Modified for MP, 1996 by Tuyen Nguyen
37 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
40 #include <sys/errno.h>
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <machine/spl.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/filedesc.h>
48 #include <sys/fcntl.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
54 #include <netat/sysglue.h>
55 #include <netat/appletalk.h>
56 #include <netat/ddp.h>
57 #include <netat/at_pcb.h>
58 #include <netat/debug.h>
59 #include <netat/adsp.h>
60 #include <netat/adsp_internal.h>
62 extern atlock_t adspall_lock
;
64 static void qRemove(CCBPtr
, CCBPtr
);
70 * Check to see if it is OK to close this connection cleanly.
75 * True if no outstanding transactions and we can close cleanly
77 int CheckOkToClose(sp
) /* (CCBPtr sp) */
81 if (sp
->sData
) /* Outstanding data ? */
84 if (sp
->sapb
) /* Outstanding send attention ? */
87 if (sp
->frpb
) /* Outstanding forward reset ? */
99 sp
->sendCtl
|= B_CTL_CLOSE
; /* So, need to send close advice */
102 return 1; /* It's OK to close */
109 * Given the address of the head of a queue of DSP parameter blocks, zero
110 * the queue, and complete each item on the queue with the given result
114 * qhead Address of ptr to first queue element
115 * code The result code
119 int CompleteQueue(qhead
, code
) /* (DSPPBPtr FPTR qhead, OSErr code) */
120 struct adspcmd
**qhead
;
123 register struct adspcmd
*p
;
124 register struct adspcmd
*n
;
125 register gref_t
*gref
;
126 register int total
= 0;
130 n
= *qhead
; /* Get first item */
131 *qhead
= 0; /* Zero out the queue */
135 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
136 atalk_flush(sp
->gref
);
137 ATDISABLE(s
, sp
->lock
);
141 while (p
= n
) { /* while items left */
142 n
= (struct adspcmd
*)(p
->qLink
); /* Save next guy */
145 completepb(sp
, p
); /* complete the copy of the request */
151 ATENABLE(s
, sp
->lock
);
158 * Called from do close to free up the user's CCB. So, we remove the
159 * CCB from the list of CCB's.
163 * pb a remove param block to complete when done
168 void RemoveCCB(sp
, pb
) /* (CCBPtr sp, DSPPBPtr pb) */
177 * Unlink CCB from list
179 qRemove((CCB
*)AT_ADSP_STREAMS
, sp
); /* remove sp from active streams queue */
183 if (pb
->ioc
) /* is this a current or queued request */
184 adspioc_ack(0, pb
->ioc
, pb
->gref
); /* current */
186 completepb(sp
, pb
); /* queued */
189 if (sp
->opb
&& (pb
!= sp
->opb
)) { /* if the pb requested is not the */
190 pb
= sp
->opb
; /* waiting open pb, complete it too */
200 if (gref
->info
== (char *)sp
->sp_mp
) { /* queue head is still valid */
203 if ((skt
= sp
->localSocket
) != 0) {
204 if (adspDeassignSocket(sp
) == 0)
205 ddp_notify_nbp(skt
, sp
->pid
, DDP_ADSP
);
209 gbuf_freem((gbuf_t
*)gref
->info
); /* free the CCB */
213 gbuf_freem(sp
->sp_mp
); /* our head is already gone, be sure
214 * to release our resources too */
226 * Complete all outstanding transactions.
228 total
+= CompleteQueue(&sp
->sapb
, err
); /* Abort outstanding send attentions */
229 CompleteQueue(&sp
->frpb
, err
); /* Abort outstanding forward resets */
231 if (sp
->sbuf_mb
) { /* clear the send queue */
232 gbuf_freel(sp
->sbuf_mb
);
237 gbuf_freem(sp
->csbuf_mb
);
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.
252 * Will also remove the CCB if there is a dsp remove pending.
259 void DoClose(sp
, err
, force_abort
) /* (CCBPtr sp, OSErr err) */
263 register struct adspcmd
*pb
, *np
;
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 */
271 sp
->openState
= O_STATE_NOTHING
;
274 * Clean up any timer elements
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
);
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 */
286 while (pb
= np
) { /* Handle all of the close/remove param blks */
287 np
= (struct adspcmd
*)pb
->qLink
; /* Get next guy (if any) */
292 if (sp
->removing
&& (force_abort
>= 0)) { /* Abort outstanding receives */
293 aborted_count
+= CompleteQueue(&sp
->rpb
, err
);
295 if (sp
->deferred_mb
) {
296 gbuf_freel(sp
->deferred_mb
);
300 gbuf_freem(sp
->attn_mb
);
303 if (sp
->rbuf_mb
) { /* clear the rcv queue */
304 gbuf_freem(sp
->rbuf_mb
);
308 gbuf_freem(sp
->crbuf_mb
);
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 */
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
));
323 bzero((caddr_t
) pb
, sizeof(struct adspcmd
));
325 pb
->csCode
= dspRead
;
326 pb
->ioResult
= errAborted
;
327 completepb(sp
, pb
); /* send fake read completion */
331 RemoveCCB(sp
, 0); /* Will call completion routine */
333 sp
->userFlags
&= ~eClosed
;
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.
345 * --> ccbRefNum refnum of connection end
346 * --> abort abort the connection
352 * errRefNum Bad connection Refnum
354 int adspClose(sp
, pb
) /* (DSPPBPtr pb) */
356 register struct adspcmd
*pb
;
361 /* Must execute nearly all of this with ints off because user could
362 * be issuing a second dspRemove while the first is pending. Until
363 * we can detect this, we must not allow interrupts.
364 * Also, we can't handle the case where a close was issued earlier,
365 * and now this is the remove. If the write completion for the
366 * close advice packet occurs in the middle of this, we might
371 pb
->ioResult
= errRefNum
;
378 if (pb
->csCode
== (short)dspCLRemove
) { /* Remove connection listener */
379 if (sp
->state
!= (short)sListening
) { /* But it's not a listener! */
380 pb
->ioResult
= errState
;
383 CompleteQueue(&sp
->opb
, errAborted
); /* Complete all dspListens */
384 RemoveCCB(sp
, pb
); /* Will call completion routine */
390 * Either dspClose or dspRemove
393 if (sp
->removing
) { /* Don't allow dspRemove or dspClose */
394 /* after one dspRemove has been issued. */
395 pb
->ioResult
= errState
;
401 * The previous Macintosh ADSP allowed you to call close on a
402 * connection that was in the process of opening or passively
403 * waiting for an open request. It is also legal to close a
404 * connection that is already closed. No error will be generated.
406 * It is also legal to issue a second close call while the first
409 if (pb
->csCode
== (short)dspClose
) {
410 ATDISABLE(s
, sp
->lock
);
411 if ((sp
->state
== (short)sPassive
) || (sp
->state
== (short)sOpening
)) {
413 ATENABLE(s
, sp
->lock
);
414 DoClose(sp
, errAborted
, 0);
416 adspioc_ack(0, pb
->ioc
, pb
->gref
);
420 if (sp
->state
== (word
)sClosed
) { /* Ok to close a closed connection */
421 ATENABLE(s
, sp
->lock
);
423 adspioc_ack(0, pb
->ioc
, pb
->gref
);
426 if ((sp
->state
!= (word
)sOpen
) && (sp
->state
!= (word
)sClosing
)) {
427 ATENABLE(s
, sp
->lock
);
428 pb
->ioResult
= errState
;
432 sp
->state
= sClosing
; /* No matter what, we're closing */
433 ATENABLE(s
, sp
->lock
);
436 else { /* dspRemove */
437 ATDISABLE(s
, sp
->lock
);
438 sp
->removing
= 1; /* Prevent allowing another dspClose. */
439 /* Tells completion routine of close */
440 /* packet to remove us. */
442 if (sp
->state
== sPassive
|| sp
->state
== sClosed
||
443 sp
->state
== sOpening
) {
445 ATENABLE(s
, sp
->lock
);
446 DoClose(sp
, errAborted
, 0); /* Will remove CCB! */
448 } else { /* sClosing & sOpen */
449 sp
->state
= sClosing
;
450 ATENABLE(s
, sp
->lock
);
455 if (pb
->u
.closeParams
.abort
|| CheckOkToClose(sp
)) /* going to close */
457 AbortIO(sp
, errAborted
);
458 sp
->sendCtl
= B_CTL_CLOSE
; /* Send close advice */
462 if ( (mp
= gbuf_copym(pb
->mp
)) ) { /* duplicate user request */
463 adspioc_ack(0, pb
->ioc
, pb
->gref
); /* release user */
464 pb
= (struct adspcmd
*)gbuf_rptr(mp
); /* get new parameter block */
467 ATDISABLE(s
, sp
->lock
);
468 qAddToEnd(&sp
->opb
, pb
); /* and save it */
469 ATENABLE(s
, sp
->lock
);
472 adspioc_ack(0, pb
->ioc
, pb
->gref
); /* release user, and keep no copy
473 * for kernel bookkeeping, yetch!
481 static void qRemove(qptr
, elem
)
482 register CCBPtr qptr
;
483 register CCBPtr elem
;
487 ATDISABLE(s
, adspall_lock
);
488 while(qptr
->ccbLink
) {
489 if ((DSPPBPtr
)(qptr
->ccbLink
) == (DSPPBPtr
)elem
) {
490 qptr
->ccbLink
= elem
->ccbLink
;
492 ATENABLE(s
, adspall_lock
);
495 qptr
= qptr
->ccbLink
;
497 ATENABLE(s
, adspall_lock
);
504 register struct adspcmd
*pb
;
507 ATDISABLE(l
, sp
->lockClose
);
508 ATDISABLE(s
, sp
->lock
);
509 if ((sp
->state
== sClosing
) || (sp
->state
== sClosed
)) {
510 ATENABLE(s
, sp
->lock
);
511 ATENABLE(l
, sp
->lockClose
);
515 ATENABLE(s
, sp
->lock
);
516 CheckReadQueue(sp
); /* try to deliver all remaining data */
518 if ( (mp
= gbuf_alloc(sizeof(struct adspcmd
), PRI_HI
)) ) {
519 pb
= (struct adspcmd
*)gbuf_rptr(mp
);
520 gbuf_wset(mp
,sizeof(struct adspcmd
));
524 pb
->csCode
= dspClose
;
526 completepb(sp
, pb
); /* send close completion */
529 if ((sp
->userFlags
& eClosed
) == 0)
530 DoClose(sp
, errAborted
, -1); /* abort send requests and timers */
532 ATENABLE(l
, sp
->lockClose
);